【问题标题】:Change colors of UI button via script通过脚本更改 UI 按钮的颜色
【发布时间】:2016-07-23 20:26:55
【问题描述】:

我正在尝试使用这行代码更改 UI 按钮的颜色。

prev.GetComponent<Button>().colors.normalColor = new Color(0.0f, 0.0f, 0.0f, 1.0f);

但我收到此错误

Assets/_Scripts/OptionSwitch.cs(28,53):错误 CS1612:无法修改“UnityEngine.UI.Selectable.colors”的值类型返回值。考虑将值存储在临时变量中

我尝试在调用按钮和颜色之前将它们存储为变量,但它不会更改错误代码。

编辑

using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using UnityEngine.Sprites;

public class OptionSwitch : MonoBehaviour {

    ColorBlock colorBlock = new ColorBlock();
    colorBlock.normalColor = new Color(0.0f, 0.0f, 0.0f, 1.0f);

    [MenuItem ("GameObject/UI/Switch")]
    static void Switch(){

        if (GameObject.FindObjectOfType (typeof(Canvas)) != null) {

            Canvas canvas = (Canvas)GameObject.FindObjectOfType (typeof(Canvas));

            // Define Previous Button
            GameObject prev = new GameObject ("Previous", typeof(Button));
            prev.layer = 5;
            prev.AddComponent<Image> ();
            prev.transform.parent = canvas.transform;

            prev.GetComponent<Image> ().sprite = AssetDatabase.GetBuiltinExtraResource<Sprite>("UI/Skin/UISprite.psd");
            prev.GetComponent<Button>().colors = buttonColors;

            // Define Previous Button Image
            GameObject previm = new GameObject("Previous Image", typeof(RawImage));
            previm.layer = 5;
            previm.transform.parent = prev.transform;

        } else {

            // Create Canvas
            GameObject canvas = new GameObject("Canvas", typeof(Canvas));
            canvas.AddComponent<CanvasScaler> ();
            canvas.AddComponent<GraphicRaycaster> ();
            canvas.layer = 5;
            canvas.GetComponent<Canvas> ().renderMode = RenderMode.ScreenSpaceOverlay;
            canvas.transform.localPosition = Vector3.zero;

            // Create Event System
            GameObject eventsystem = new GameObject("EventSystem", typeof(EventSystem));
            eventsystem.AddComponent<StandaloneInputModule>();
            eventsystem.AddComponent<TouchInputModule>();

        }

    }

}

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    您必须更改 colors 而不是 normalColorGetComponent&lt;Button&gt;().colors 返回 ColorBlock

    因此,创建ColorBlock 的新实例。从 ColorBlock 修改 normalColor,然后将 ColorBlock 分配给 GetComponent&lt;Button&gt;().colors

    完整示例:

    ColorBlock colorBlock = new ColorBlock();
    colorBlock.normalColor = new Color(0.0f, 0.0f, 0.0f, 1.0f);
    
    prev.GetComponent<Button>().colors = colorBlock;
    

    这将覆盖您的其他颜色设置。要保护它们,请从 prev.GetComponent&lt;Button&gt;().colors; 创建您的 ColorBlock

    ColorBlock colorBlock = prev.GetComponent<Button>().colors;
    colorBlock.normalColor = new Color(0.0f, 0.0f, 0.0f, 1.0f);
    
    prev.GetComponent<Button>().colors = colorBlock;
    

    您还可以在下面修改颜色属性:

    colorBlock.pressedColor = new Color(1f, 0.0f, 0.0f, 1.0f);
    colorBlock.highlightedColor = new Color(0f, 1f, 0.0f, 1.0f);
    colorBlock.disabledColor = new Color(0f, 0f, 1, 1.0f);
    

    【讨论】:

    • 我收到错误 Assets/_Scripts/OptionSwitch.cs(10,28): error CS1519: Unexpected symbol =' in class, struct, or interface member declaration and Assets/_Scripts/OptionSwitch.cs(10,43): error CS1519: Unexpected symbol 0' in class, struct, or interface member declaration,
    • 哪一行代码?我上面发布的代码在我这边工作得很好。你做错了什么。我建议您创建一个新脚本以确保您做对了。如果可能,直接从答案中复制代码。
    • 我确实直接复制了它的第 10 行,您是否使用非标准命名空间来定义色块?
    • “这是第 10 行” 因为我可以从这里看到第 10 行。将 Edit 放入您的问题并添加您拥有的新代码。这是找出你做错了什么的唯一方法。
    • 对不起,我错了,是这一行 "colorBlock.normalColor = new Color(0.0f, 0.0f, 0.0f, 1.0f);"我已经在我的问题中添加了整个代码
    【解决方案2】:

    我不知道所有.GetComponents 是怎么回事,但还有另一种方法!

    当您有多个按钮并且只想更改它们的颜色或使它们消失时,此方法适用。

    Color.red , Color.grey , ...
    
    for (int i = 0; i < 5; i++) {
        alchemistVoScript.gatedButtons [i].image.color = Color.clear;
        alchemistVoScript.gatedButtons [i].interactable = false;
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-02-07
      • 2016-09-11
      • 2011-05-22
      • 2014-09-03
      • 2015-08-13
      • 2016-12-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多