|How do you use documentation throughout your workflow? Share your experience with us by taking this survey.|
Every time you build your project, the Unity Editor compiles all the shaders that your build requires: every required shaderA program that runs on the GPU. More info
See in Glossary 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:
Shader compilation is carried out using a process called
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 platforms use different shader compilers for shader program compilation as follows:
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.
While building the game, Unity can detect that some of the internal shader variants are not used by the game, and exclude (“strip”) them from build data. Build-time stripping is done for:
#pragma shader_feature, Unity automatically checks whether variants are used. If none of the Materials in a build use a variant, that variant it is not included into the build. See internal shader variants documentation. The Standard shader uses this.
Combination of the above often substantially cuts down on shader data size. For example, a fully compiled Standard shader would take several hundred megabytes, but in typical projects it often ends up taking just a couple megabytes (and is often compressed further by the application packaging process).