Changing colour of material over time

2019-09-19 00:06发布

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条回答
神经病院院长
2楼-- · 2019-09-19 00:29

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;
    }
}
查看更多
登录 后发表回答