问题
Is there a way to change the colour of a material over time? I want the background of my 2D game to change from white to red in a nice smooth transition.
In Unity 5, you can set the "Albedo" colour. This is the property I am looking to change. I can do this through using this:
back.SetColor("_Color", new Color(255, 0, 0, 0.5f));
However, how can the colour slowly change instead of being so instant?
回答1:
not tested but try it this way:
var threshold = 0.001f;
var speed = 5.0f;
function Start()
{
// Execution
StartCoroutine("ChangeToColor", new Color(1.0f, 0f, 0f));
}
IEnumerator ChangeToColor(Color newColor)
{
var getColor = back.GetColor("_Color");
while(((Vector4)getColor - (Vector4)newColor).magnitude < threshold)
{
back.SetColor("_Color", Vector4.Lerp(getColor, newColor, Time.deltaTime * speed));
yield return null;
}
}
来源:https://stackoverflow.com/questions/32211837/changing-colour-of-material-over-time