- Home /
Using replacement shader at runtime?
My dilemma:
When building levels, I like all my scene assets to use a surface shader, so I can preview within the Editor how the lighting will look like after setting up all my lights, without having to bake all lightmaps to see how the lights are affecting the objects.
But I am working on an mobile game, so using surface shaders is definitively not the best for speed. Before making a build, I assign a cheaper, vertex lit shader to the scene objects, and if I haven't rendered any lightmaps yet, the scene looks like it's being lit by only one light, my main directional light.
Having to manually switch shaders everytime I change the lighting setup or make changes to the scene is a time waster and a pain :)
What I would like to do:
I want tell Unity to use surface shaders when the game is being run in the editor, and use vertex lit shaders when it's running on a mobile platform. I could do that using replacement shaders, or writing the logic directly within a shader (if isEditor use surface else use vertex lit).
Which would be the better approach, and is there any other way of doing this? Also, is using replacement shaders at runtime have an impact on performance?
Thanks for your time!
Stephane
Answer by Meltdown · Mar 13, 2012 at 08:13 AM
You can use a variety of preprocessor directives to achieve what you want..
#if UNITY_EDITOR
// Change your material's shader here when it's being run in the editor
#endif
#if UNITY_IPHONE
// Change your material' shader here when being run on iOS
#endif
#if UNITY_ANDROID
// Change your material' shader here when being run on Android
#endif
Exactly what I was looking for! Would it be better to do this in script - like your above example - or to do this within the shader? Doing it within the shader means i would only need 1 shader, smart enough to know which SubShader to use depending on the platform, but doing in script is easier and less prone to errors. Can you even use #if UNITY_EDITOR/UNITY_IPHONE in a shader?
Thanks a lot for your help!
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Replacing Shader At Runtime. 1 Answer
Replacement Shaders and Tags 0 Answers
Material doesn't have a color property '_Color' 4 Answers
Runtime javascript 1 Answer