今天碰到另外一种改变材质球颜色的代码,试验后发现出现物体变白问题,代码如下:
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范围内