Follow

How to use Shader Features with Asset Bundles?

Symptoms

  • Shaders do not look correct when loaded from AssetBundles.
  • Shader Features do not work with AssetBundles

Cause

  • A shader that uses “shader features” and is included in an AssetBundle does not work on versions earlier than 5.4.4p4, 5.5.1p4, and 5.6.0b8.

Resolution

In versions prior to 5.4.4p4, 5.5.1p4, and 5.6.0b8 the only way to use shader features with AssetBundles was to include all the materials that use a specific shader in the same AssetBundle.

From 5.4.4p4, 5.5.1p4, and 5.6.0b8 and onwards to use the Shader Features with Asset Bundles follow the steps below.

Use a ShaderVariantCollection to pack the shader feature with the collection:

    1. Create a ShaderVariantCollection
    2. Add the Shader to the collection
    3. Add variant tags to the collection
    4. Assign both collection and shader to the same Bundle.

Watch the video demonstration of this here.

More Information

https://docs.unity3d.com/Manual/BuildingAssetBundles.html

https://docs.unity3d.com/Manual/SL-MultipleProgramVariants.html

https://docs.unity3d.com/ScriptReference/ShaderVariantCollection.html

Was this article helpful?
1 out of 2 found this helpful
Have more questions? Submit a request

2 Comments

  • 0
    Avatar
    cpxnet

    I made a test and it's still not working in 5.6.1f1

    Given a shader with

    #pragma shader_feature _ RED BLUE GREEN

    and a variant collection containing all four variants.

    only _(white) and RED is included in the AssetBundle.

     

    I have a small testcase

    Can someone explain?

  • 0
    Avatar
    Marcus Goitowski

    I've tested this in Unity 2019.3.15f1 and the ShaderVariantCollection is simply ignored. Not even calling Warmup manually on it works. The problem probably is that the AssetBundle build another version of the shader inside of the bundle which is not correctly referenced by the ShaderVariantCollection in the AssetBundle. 

    What works is cloning all external shaders to the loader Unity project, copying the ShaderVariantCollection there and assign it in the Graphics settings.

    Edited by Marcus Goitowski
Please sign in to leave a comment.