unity3d 通过脚本更改UI按钮的颜色

bfnvny8b  于 2023-04-21  发布在  其他
关注(0)|答案(3)|浏览(365)

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

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

但我收到了这个错误
Assets/_Scripts/OptionSwitch.cs(28,53):错误CS 1612:无法修改“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>();

        }

    }

}
swvgeqrz

swvgeqrz1#

您必须更改colors而不是normalColorGetComponent<Button>().colors返回ColorBlock
因此,创建一个新的ColorBlock示例。从ColorBlock修改normalColor,然后将ColorBlock分配给GetComponent<Button>().colors
完整示例:

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

prev.GetComponent<Button>().colors = colorBlock;

这将覆盖其他颜色设置。要保留它们,请从prev.GetComponent<Button>().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);
44u64gxh

44u64gxh2#

ColorBlock colorBlock = new ColorBlock();

colorBlock.normalColor = Color.yellow;
colorBlock.colorMultiplier = 1;
gameManager.setCurrentLevelBoardRef.currentButtonArray[numbersToColor[i]]
    .GetComponent<Button>().colors= colorBlock;
wqlqzqxt

wqlqzqxt3#

我不知道所有的.GetComponents是怎么回事,但也有另一种方法!
这种方法适用于当你有多个按钮,只想改变它们的颜色或使它们消失。

Color.red , Color.grey , ...

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

}

相关问题