随时间改变材料的颜色 [英] Changing colour of material over time

查看:94
本文介绍了随时间改变材料的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以随着时间改变材料的颜色?我希望2D游戏的背景能够从白色变为红色,并且平滑过渡.

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.

在Unity 5中,您可以设置反照率"颜色.这是我要更改的属性.我可以使用以下方法做到这一点:

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?

推荐答案

未经测试,但可以尝试这种方式:

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;
    }
}

这篇关于随时间改变材料的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆