我有一个点光源预制件,可以在球体周围产生辉光。根据“类型”(健康、个性、天赋),发光的颜色必须改变。这是我的代码来做到这一点 -
GameObject glow = (GameObject)Instantiate(glowPrefab,new Vector3(0,0,0),Quaternion.identity);
glow.transform.parent = child.transform;
glow.transform.localPosition = new Vector3(0,0,0);
switch(type)
{
case "health":
child.renderer.material = health;
glow.GetComponent<Light>().color = new Color(254f,137f,96f,255f);
//Debug.Log ("Health Color" + glow.light.color );
break;
case "personality":
child.renderer.material = personality;
glow.GetComponent<Light>().color = new Color(137f,254f,96f,255f);
//Debug.Log ("Personality Color" + glow.GetComponent<Light>().color );
break;
case "talent":
child.renderer.material = talent;
glow.GetComponent<Light>().color = new Color(137f,96f,254f,255f);
//Debug.Log ("Talent Color" + glow.GetComponent<Light>().color );
break;
}
虽然debug.log显示灯光发生了变化,但在游戏中,它仍然是白色的。 有趣的是,当它运行时,场景会显示五彩灯光 -
但是,在游戏中,颜色是白色 -
当我单击单个灯光时,颜色为白色 -
即使我更改预制件颜色,灯光的颜色仍然是白色。
如何检查颜色发生变化的位置?是否有一个事件可以用于记录?
谢谢!
最佳答案
那是因为您传递了错误的颜色数据。 UnityEngine.Color RGBA 值介于 0-1 之间。您可以将您的值转换为正确的 RGBA 值或使用 UnityEngine.Color32到特定的 0-255 RGBA 值。
glow.GetComponent<Light>().color = new Color32(254,137,96,255);
// OR
glow.GetComponent<Light>().color = new Color(1, 0.92, 0.016, 1); // Yellow
// OR
glow.GetComponent<Light>().color = new Color.yellow;
关于unity-game-engine - 在预制件中实例化后无法更改点光源的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20136537/