Hey, I’m guessing that the answer is no, but figured I’d ask here anyways. Is there a way to have a shader read a static variable from a C# script? I would much rather have a value set and have the shaders automagically update their values by just altering the static variable, and not have to manually update each shader by setting values to it. Is this possible?
You can use Shader.SetGlobalXXX to put a global value up to the shader system.
You simply reference it into the shader like Unity’s built-in values, as in, it doesn’t need a slot in the shader Properties.
i.e.
in code;
Shader.SetGlobalFloat(_“MyFloat”, myVariable );
in your shader;
float _MyFloat;
Thanks for the info Farfarer!
Question, as I haven’t had a chance to test this yet . . . Say I have 10 shaders and they all have a variable defined called “WindSpeed”. Can I just use that single command to SetGlobalFloat and all the shaders will be updated? Is that how this works?
Yep, that function will set that shader value globally and all shaders can access it the same way.
Yes, it’s global and updates for all shaders.
Alternatively, you can also specifically set float values of materials if you have the reference to them in the shader by writing: MaterialRef.SetFloat or SetColor, etc…
edit: damn seems I was seconds too late
INTERESTING! . . . Thats actually pretty awesome, and I think it will work quite well. Thanks guys!