Version: 2021.2
Using shader keywords with the material Inspector
着色器变体集合

Shader variant stripping

You can prevent shader variants from being compiled. This is called stripping. Stripping unneeded variants can greatly reduce build times, file size, shader loading times, and runtime memory usage. In larger projects, or projects with complex shaders, this is a very important consideration.

Limiting shader variants when you declare shader keywords

The way that you declare shader keywords can limit the number of variants that they produce:

  • Use shader_feature instead of multi_compile where possible.
  • Ensure that you don’t define unused keywords with multi_compile.
  • Indicate when shader keywords only affect a given shader stage.

For information on doing this in hand-coded shaders, see Declaring and using shader keywords in HLSL. For information on doing this in Shader Graph, see Shader Graph: Blackboard.

Stripping shader variants in the Editor UI

There are several places in the Unity Editor UI where you can configure shader stripping:

  • In the Graphics Settings window, configure the settings in the Shader stripping section:
  • Ensure that no unneeded shaders are included in the Always-included shaders setting.
  • Strip variants relating to GPU instancing, lightmapping, and fog.
  • In the Built-in Render Pipeline, if it is not important that your Tier settings are different, ensure that they are identical to each other. For more information, see Graphics tiers.
  • In the Universal Render Pipeline (URP), disable unused features in the URP Asset. For more information, see Shader stripping.

Stripping shader variants using Editor scripts

For shader variants that you can’t strip in other ways, you can use the following APIs in an Editor script to perform build-time stripping:

For more information on this subject, see the Unity blog post Stripping scriptable shader variants

Using shader keywords with the material Inspector
着色器变体集合