unity-game-engine - 在预制件中实例化后无法更改点光源的颜色

标签 unity-game-engine

我有一个点光源预制件,可以在球体周围产生辉光。根据“类型”(健康、个性、天赋),发光的颜色必须改变。这是我的代码来做到这一点 -

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显示灯光发生了变化,但在游戏中,它仍然是白色的。 有趣的是,当它运行时,场景会显示五彩灯光 -

enter image description here

但是,在游戏中,颜色是白色 -

enter image description here

当我单击单个灯光时,颜色为白色 -

enter image description here

即使我更改预制件颜色,灯光的颜色仍然是白色。

enter image description here

如何检查颜色发生变化的位置?是否有一个事件可以用于记录?

谢谢!

最佳答案

那是因为您传递了错误的颜色数据。 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/

相关文章:

unity-game-engine - Unity HDRP 地形上的阴影太暗

c# - 如何使用单个图像统一创建二维 map ?

java - 如果在另一个进程中编辑,sharedPreferences 不会更新

c# - 在 Unity 中使用触发器得分

google-maps - 我们可以在Unity 3D中使用google map 的API吗?

c# - Unity 访问非静态成员需要对象引用 C#

c# - 如何在 Unity3D 的 OnDrawGizmos 中以屏幕像素坐标绘制某些内容?

c# - Unity - 未检测到命名空间

xcode - 在终端中使用 codesign 命令签名后,使用 Unity 构建的 macOS 应用程序在启动时挂起

java - Unity3D - 由于 Firebase Messaging SDK 设置失败而导致 Android 崩溃