今天碰到另外一種改變材質球顏色的代碼,試驗後發現出現物體變白問題,代碼如下:
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範圍內