- Home /
Accessing Unity 5 Environmental Lighting Skybox from C# script
I'm trying to rotate a cube map skybox at runtime. Since the 'Skybox/Cubemap' shader already exposes a 'Rotation' uniform, I thought I could just write a script along the lines of:
public class Clouds : MonoBehaviour {
int step = 1;
void FixedUpdate () {
step += 1;
step %= 360;
Material skybox = RenderSettings.skybox;
skybox.SetInt ("Rotation", step);
}
}
The idea being that it should be like manually moving the 'Rotation' slider for the cubemap shader from the inspector. But when doing it thru the code above, the parameter doesn't seem to update. Any thoughts on what am I doing wrong?
Answer by jenci1990 · Jun 05, 2015 at 09:25 AM
Material skybox = RenderSettings.skybox;
skybox.SetInt ("Rotation", step);
RenderSettings.skybox = skybox;
That seems to do it. Thanks. But, why do I have to reassign the material? And, if it is indeed resigning the material, isn't that super inefficient? As in, the whole idea was that I could just tweak a shader uniform and now it looks like I have to constantly reassign the whole material which seems like it would entail reloading the shader program. Am I wrong?
Answer by djon801 · Sep 30, 2015 at 08:50 AM
step += 1;
step %= 360;
Material skybox = RenderSettings.skybox;
skybox.SetInt ("_Rotation", step);
"Rotation" = " _Rotation"