How to gradually change color of sprite during runtime
Hello,
I'm developing a 2D game, and I want to place a semi-transparent color layer covering the full screen, to slightly change the tone of everything beneath (bluish during morning, orange during sunset, etc), just like I would do in Photoshop.
I've placed a game object with a simple plain white jpg sprite on it, and then I'm trying to change the "color" property of the Sprite Renderer component with a script like this:
public float duration;
private Color colorSunrise = new Color (88f, 165f, 209f);
private Color colorSunset = new Color (251f, 138f, 7f);
private float t = 0f;
void Start()
{
GetComponent<SpriteRenderer> ().color = colorSunrise;
}
void Update()
{
Color color = Color.Lerp (colorSunrise, colorSunset, t);
t += Time.deltaTime / duration;
GetComponent<SpriteRenderer> ().color = color;
}
But when I run the game, it simply shows the original plain white sprite, and its color doesn't seem to be changed in the Inspector. However if, while running, I click in the color box property inside the Inspector, in the color wheel I see that it's actually gradually changing its color perfectly, but the change is not applied in the game nor in the preview of the Inspector, which remains white.
For what I read, maybe the problem has to do with materials, but I'm not an expert and I did not expect to have to use materials in a simple 2D game with plain colors. I tried this:
myMaterial = new Material(Shader.Find("Diffuse"));
myMaterial.color = colorSunrise;
GetComponent<SpriteRenderer> ().material = myMaterial;
But it didn't work, or maybe I'm doing something wrong. Any help will be greatly appreciated, as well as any other approaches to solve this.
Thank you very much.