Version: 2021.1
Material Inspector reference

Shader compilation


Every time you build your project, the Unity Editor compiles all the shaders that your build requires: every required shader variant, for every required graphics API.

When you’re working in the Unity Editor, the Editor does not compile everything upfront. This is because compiling every variant for every graphics API can take a very long time.

Instead, Unity Editor does this:

  • When it imports a shader asset, it performs some minimal processing (such as Surface Shader generation).
  • When it needs to show a shader variant, it checks the Library/ShaderCache folder.
  • If it finds a previously compiled shader variant that uses identicial source code, it uses that.
  • If it does not find a match, it compiles the required shader variant and saves the result to the cache.

Shader compilation is carried out using a process called UnityShaderCompiler. Multiple UnityShaderCompiler processes can be started (generally one per CPU core in your machine), so that at player build time shader compilation can be done in parallel. While the Editor is not compiling shaders, the compiler processes do nothing and do not consume computer resources.

The shader cache folder can become quite large, if you have a lot of shaders that are changed often. It is safe to delete this folder; it just causes Unity to recompile the shader variants.

At player build time, all the “not yet compiled” shader variants are compiled, so that they are in the game data even if the editor did not happen to use them.

Different shader compilers


  • Platforms that use DirectX use Microsoft’s FXC HLSL compiler.
  • Platforms that use OpenGL (Core & ES) use Microsoft’s FXC HLSL compiler, followed by bytecode translation into GLSL using HLSLcc.
  • Platforms that use Metal use Microsoft’s FXC HLSL compiler, followed by bytecode translation into Metal, using HLSLcc.
  • Platforms that use Vulkan use Microsoft’s FXC HLSL compiler, followed by bytecode translation into SPIR-V, using HLSLcc.
  • Other platforms, such as console platforms, use their respective compilers.
  • 表面着色器使用 HLSL 和 MojoShader 来完成代码生成分析步骤。

You can use predefined shader macros to identify which compiler Unity is using. You might want to use this if you use HLSL syntax that is supported by only one compiler, or to work around a compiler bug.

You can configure various shader compiler settings using pragma directives.

The Caching Shader Preprocessor

Shader compilation involves several steps. One of the first steps is preprocessing the shader source. By default, Unity uses the platform compiler’s preprocessor to perform this step; however, you can choose to override this and use Unity’s Caching Shader Preprocessor to perform preprocesing. The Caching Shader Preprocessor is up to 25% faster than the default preprocessors used by platform compilers.

The Caching Shader Preprocessor caches intermediate preprocessing data to speed up shader import and compilation time. The Editor reuses this cached data, and only needs to parse include files when their contents change. This makes compiling multiple variants of the same shader more efficient. Enabling the Caching Shader Preprocessor has the most noticeable effect when shaders within a project use a large set of common include files.

Note that the Caching Shader Preprocessor is experimental; it is still in active development. You can provide feedback on this experimental feature in the Unity forum.

You can enable or disable the Caching Shader Preprocessor using the Caching Shader Preprocessor checkbox in the Shader Compilation section of the Editor settings window. You can also enable or disable this feature in a C# script, using the EditorSettings.cachingShaderPreprocessor API.


在构建游戏时,Unity 可能检测到游戏不使用某些内部着色器变体,并从构建数据中排除(“剥离”)它们。构建时剥离将用于以下各项:

  • 对于使用 #pragma shader_feature 的着色器,Unity 会自动检查是否使用了变体。如果构建中的材质都不使用某个变体,则该变体不会包含在构建中。请参阅内部着色器变体文档。标准着色器会使用此功能。
  • 任何场景未使用的可处理雾效和光照贴图模式的着色器变体不会包含在游戏数据中。如果要覆盖此行为,请参阅 Graphics 窗口。
  • 还可以手动识别变体并使用 OnProcessShader API 告诉 Unity 将这些变体从构建中排除。


Material Inspector reference