- Home /
Initialize UV offset with UV scrolling script
Hi! I made this UV scrolling script
void Light(List<Material> matList, float Xspeed, float Yspeed )
{
Xoffset += (Xspeed * Time.deltaTime);
Yoffset += (Yspeed * Time.deltaTime);
foreach (Material Mat in matList)
{
Mat.SetTextureOffset("_MainTex", new Vector2(Xoffset, Yoffset));
}
}
I find that the change of texture offset will remain after turning off play button, eventually changing the original material. I would like that when I turn off the play button, the texture offset automatically set back to 0, so I add these line but they don't work.
private void OnApplicationPause(bool pause)
{
Xoffset = 0;
Yoffset = 0;
}
private void OnApplicationQuit()
{
Xoffset = 0;
Yoffset = 0;
}
I know that if I change material through renderer this problem can be avoid, but this method doesn't work for my case because the object contain multiple materials, including some that don't need UV scrolling.
Any advice will be greatly appreciated.
Answer by Mrpxl · Jul 29, 2021 at 08:15 AM
What you do here in OnApplicationQuit()
is that you reset the Xoffset
and Yoffset
variable but you never set it back in your material after changing these values.
What you should do is set back these floats back in your texture after you change them:
private void OnApplicationPause(bool pause)
{
Xoffset = 0;
Yoffset = 0;
foreach (Material Mat in matList)
{
Mat.SetTextureOffset("_MainTex", new Vector2(Xoffset, Yoffset));
}
}
private void OnApplicationQuit()
{
Xoffset = 0;
Yoffset = 0;
foreach (Material Mat in matList)
{
Mat.SetTextureOffset("_MainTex", new Vector2(Xoffset, Yoffset));
}
}
Ah! And I thought my problem was misunderstanding the function of these two function. Thanks a lot!