- Home /
Use Predefined Preprocessor Symbols for shaders.
I´d like to optimize the shaders in my project for different platforms. In C#, I make a lot use of preprocessor symbols, and I´m wondering if theres any way to use them in shader code. What I´d like to do is something like
#if UNITY_IOS
//some specific stuff here
#else
//regular stuff here
#endif
I have figured out that it is possible to exclude a build target from the shader and use a fallback to get to an alternative version of it, but since the project is quiet big and has a lot of shaders, it would be quiet messy to always create multiple files of every shader. Most times, i just want to change one line of code, since some console versions of unity don´t support unlit shaders while unlit is necessary for the performance on mobile, so I really just need to do something like
#if UNITY_Console
Lighting On
#else
Lighting Off
#endif
in every shader of the project.,
Answer by namark · Oct 03, 2016 at 05:59 AM
For anyone who stumbles upon this, like I did, here's a potential solution.
In short you can add a line like this to the shader
#pragma multi_compile FANCY_STUFF_OFF FANCY_STUFF_ON
which will cause two(or more) version of the shader to be created with appropriate symbols/keywords defined. You can then select the symbol/keyword you need from C#
material.EnableKeyword("FANCY_STUFF_ON"); // for a single material
Shader.EnableKeyword("FANCY_STUFF_ON"); // globally, for all materials
This is not an exact equivalent of C#'s preprocessors symbols, since variant selection happens at runtime, but it is close enough for all practical purposes. If you want to squeeze out even more performance you can use #pragme shader_feature which apparently will not include unused variants in the build... how exactly it does that, I'm not sure, haven't tested it myself.
Your answer
![](https://koobas.hobune.stream/wayback/20220613150802im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Generating a texture through Graphics.Blit with shadergraph 0 Answers
How to replicate renderer.worldToLocalMatrix 0 Answers
Black Texture on Initial Load 1 Answer
Fake PS1 "jittery movement" effect in Unity? 1 Answer
Greyscale shading 0 Answers