Control how much memory shaders use
Troubleshooting shader duplication from AssetBundles
If you use AssetBundles, Unity might compile duplicate shaders if you reference one shaderA program that runs on the GPU. More info
See in Glossary in two or more objects. For example:
- A material in an AssetBundle and a material in a built sceneA Scene contains the environments and menus of your game. Think of each unique Scene file as a unique level. In each Scene, you place your environments, obstacles, and decorations, essentially designing and building your game in pieces. More info
See in Glossary reference the same shader.
- Multiple AssetBundles contain materials that reference the same shader outside an AssetBundle.
This can increase the memory and storage space shaders use, and break draw call batching.
To avoid this, you can use the following approaches:
- Load an AssetBundle that contains all your shaders first, then load and instantiate AssetBundle assets that reference the shaders. See AssetBundle Dependencies for more information.
- Structure your AssetBundles to minimise duplication. See Asset Duplication for more information.
If you add shader variant collections, use one of the following approaches:
- Add all your shader assets and shader variant collections to one AssetBundle you use in all scenes.
- Add shader variant collections to their own AssetBundles, for example one AssetBundle for each scene. Then add the shader assets they all reference to a separate single AssetBundle, which you must load and instantiate first.
Note: If you use the BuildPipeline.BuildAssetBundles API to build, add each shader variant collection to the same AssetBundle as the shaders it references. Otherwise Unity might not build all the shader variants.
If you create a single AssetBundle, some shaders might stay in memory even if they’re no longer needed, because you cannot partially unload an AssetBundle. You can avoid this by creating a separate AssetBundle for each group of shaders you use together, for example a ‘forest’ AssetBundle and a ‘desert’ AssetBundle. See Managing loaded AssetBundles, or Memory management in the Addressables system if you use Addressables.
Control how much memory shaders use