gpt4 book ai didi

c# - Unity 新 UI 图像更改颜色不起作用

转载 作者:太空狗 更新时间:2023-10-30 00:31:14 32 4
gpt4 key购买 nike

我正在尝试使用统一的新 UI 图像系统为我的播放器实现健康。但它不起作用。任何人都可以帮助我。谢谢。

    using UnityEngine.UI;

if (health_value == 3) {
GameObject.Find("health").GetComponent<Image>().color.a = 1;
GameObject.Find("health1").GetComponent<Image>().color.a = 1;
GameObject.Find("health2").GetComponent<Image>().color.a = 1;

}

我遇到了这个错误。

  error CS1612: Cannot modify a value type return value of `UnityEngine.UI.Graphic.color'. Consider storing the value in a temporary variable

最佳答案

因为 Color 是 Image 的结构体(我认为这是正确的术语?如果我错了请纠正我),你不能直接编辑它的颜色,你必须创建一个新的 Color var,改变它的 vars ,然后将其分配给 Image。

Image healthImage = GameObject.Find("health").GetComponent<Image>();
Color newColor = healthImage.color;
newColor.a = 1;
healthImage.color = newColor;

关于c# - Unity 新 UI 图像更改颜色不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28869780/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com