【问题标题】:Unity Button.Image.Color won't "refresh" [duplicate]Unity Button.Image.Color 不会“刷新” [重复]
【发布时间】:2018-09-12 14:27:26
【问题描述】:

今天早上我的 Button.Image.Color 出现问题

这是我非常简单的代码:

this.gameObject.GetComponent<Image>().color = Color.Red;

所以这条线完美地工作......现在,我正在尝试使用我自己的颜色到那个按钮上,再次,简单的代码行:

this.gameObject.GetComponent<Image>().color = new Color(106, 174, 23);

嗯,颜色实际上会改变......没有改变......让我们在游戏运行时引用对象本身:

My button / my color

如您所见,颜色发生了变化,但仍保持白色。

我在这里查看了很多帖子,但似乎我做得对,显然有错误,否则它会正常工作,但即使在阅读了很多帖子之后,我似乎也看不到解决方案我自己。

【问题讨论】:

    标签: image unity3d button colors rgb


    【解决方案1】:

    Color 的构造函数接受范围从 0 到 1 的浮点数。

    您将它们设置为 1(高于 1 的任何值都设置为 1)。 1,1,1 = 白色。

    this.gameObject.GetComponent<Image>().color = new Color(0.415f, 0.682f, 0.09f);
    

    这应该会给你你正在寻找的结果。

    这些数字是通过将您的值除以 255 生成的

    或者你也可以解析你的十六进制值:

    if(Color.TryParseHtmlString("#75BF1A", out myColor))
    {
        this.gameObject.GetComponent<Image>().color = myColor;
    }
    

    【讨论】:

      【解决方案2】:

      您好,感谢您的回答,确实颜色需要从 0 到 1 的数字。

      只是为了补充您的答案,我发现 Color32 是我应该使用的,因为它需要 0 - 255 值。

      再次感谢,祝你有美好的一天!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-10-14
        • 1970-01-01
        • 2012-06-03
        • 1970-01-01
        • 2013-04-01
        • 2019-05-16
        • 1970-01-01
        • 2012-07-23
        相关资源
        最近更新 更多