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 configure various shader compiler settings using pragma directives.
Shader compilation involves several steps. One of the first steps is preprocessing. During this step, a program called a preprocessor prepares the shader source code for the compiler.
In previous versions of Unity, the Editor used the preprocessor provided by the shader compiler for the current platform. Now, you can choose whether to use Unity’s Caching Shader Preprocessor, or revert to the previous behavior. Unless you experience problems, you should use the Caching Shader Preprocessor.
The Caching Shader Preprocessor is optimized for faster shader import and compilation; it is up to 25% faster. It works by caching intermediate preprocessing data, so the Editor 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.
As well as improved performance, the Caching Shader Preprocessor adds the following features:
#pragmadirectives inside conditionals.
#include_with_pragmasdirective, which allows you to put
#pragmadirectives in include files.
For detailed information on the differences between the Caching Shader Preprocessor and the previous behavior, see the Unity forum: New shader preprocessor.
You can enable or disable the Caching Shader Preprocessor with the Caching Shader Preprocessor checkbox in the Shader Compilation section of the Editor settings window, or with the EditorSettings.cachingShaderPreprocessor API.
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. For more information, see Shader variantsA verion of a shader program that Unity generates according to a specific combination of shader keywords and their status. A Shader object can contain multiple shader variants. More info
See in Glossary.