Switch Shader Graph Material Between Lit and Unlit at Runtime?

Plain and simple: Is there a way for me to switch a shader graph material between Lit and Unlit at runtime?

I tried this: material.SetOverrideTag("UniversalMaterialType", isUnlit ? "Unlit" : "Lit"); but it didn’t work.

Is there something I’m missing?
Thanks!

As far as I know you need 2 shaders for this

That’s what I’ve been doing so far, but I find it kinda clunky. That’s why I was hoping there was a way to use a single material.
Thanks!

Shader graph generates hlsl shader code. There are too many differences between unlit and lit to compile it in real time (not to mention if statements being bad for performance on the GPU)

That’s very interesting, thank you for the info.
I suppose I didn’t realize how complex it would be to recompile at runtime, I thought it was more of a flag I could toggle.
Oh well!

Thanks again for the info!

1 Like

Hi!

I’m having essentially the same problem:
I have a custom URP shader (not shader graph), where I want to switch the “UniversalMaterialType” between “Lit” and “ComplexLit” based on the properties set by the user in the material GUI.
If I change the tag in the shader directly, it works.
But if I call material.SetOverrideTag("UniversalMaterialType", isComplex ? "ComplexLit" : "Lit"); in the material GUI, this does not have an effect.

Are there any other workarounds than maintaining two copies of the shader?

Doing your own lighting and a branch works