Unity代码动态改变unity物体材质球颜色变白问题

今天碰到另外一种改变材质球颜色的代码,试验后发现出现物体变白问题,代码如下:
 

private void Start()
{
   renderer = GetComponent<Renderer>();
   StartCoroutine(ChangeColor());
}
IEnumerator ChangeColor()
{
    int i = 0, j = 100, k = 100;
    //变红
    for (i = 100; i < 255; i = i + 5)
    {
        Color c = new Color(i / 255.0f, j / 255.0f, k / 255.0f);
        renderer.material.color = c;
        yield return new WaitForSeconds(0.1f);
    }
}

多方尝试后跳转回Color构造函数查看,发现Unity官方注释如下:

//
        // 摘要:
        //     Constructs a new Color with given r,g,b components and sets a to 1.
        //
        // 参数:
        //   r:
        //     Red component.
        //
        //   g:
        //     Green component.
        //
        //   b:
        //     Blue component.
        public Color(float r, float g, float b);
        //
        // 摘要:
        //     Constructs a new Color with given r,g,b,a components.
        //
        // 参数:
        //   r:
        //     Red component.
        //
        //   g:
        //     Green component.
        //
        //   b:
        //     Blue component.
        //
        //   a:
        //     Alpha component.
        public Color(float r, float g, float b, float a);

使用new Color进行设置时传入的数值应该为0~1范围内

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章