最有效的方式来切换按钮打开和关闭功能的脚本

本文关键字:功能 脚本 按钮 方式 有效 | 更新日期: 2023-09-27 18:03:10

我编写了一段代码来更改按钮的视觉状态(单击哪个按钮将突出显示为蓝色,并将所有其他按钮返回为默认颜色)。它似乎工作得很好,但感觉很麻烦。是否有更有效/简洁的方法来重写我的代码?很多谢谢!

using UnityEngine ;
using System.Collections ;
using UnityEngine.UI ;
public class ToolButtons : MonoBehaviour
{
    public Color activeColor ;
    public Color inactiveColor ;
    public GameObject iconBG ;
    public Button ink, brush, crayon, pencil, spray, eraser, chnageColor, brushSize, undo, redo, clear, newAnimal ;
    public GameObject inkIconBG, brushIconBG, crayonIconBG, pencilIconBG, sprayIconBG, eraserIconBG, changeColorIconBG, brushSizeIconBG ;
    void Start ()
    {
        inactiveColor = iconBG.GetComponent <Image> ().color ;
    }
    // Use this for initialization
    void buttonCallBack (Button buttonClicked)
    {
        //Change Color Palette Button clicked
        if (buttonClicked == ink)
        {
            inkIconBG.GetComponent <Image> ().color = activeColor ;
        } else if (buttonClicked != ink)
        {
            inkIconBG.GetComponent <Image> ().color = inactiveColor ;
        }
        if (buttonClicked == brush)
        {
            brushIconBG.GetComponent <Image> ().color = activeColor ;
        } else if (buttonClicked != brush)
        {
            brushIconBG.GetComponent <Image> ().color = inactiveColor ;
        }
        if (buttonClicked == crayon)
        {
            crayonIconBG.GetComponent <Image> ().color = activeColor ;
        } else if (buttonClicked != crayon)
        {
            crayonIconBG.GetComponent <Image> ().color = inactiveColor ;
        }
        if (buttonClicked == pencil)
        {
            pencilIconBG.GetComponent <Image> ().color = activeColor ;
        } else if (buttonClicked != pencil)
        {
            pencilIconBG.GetComponent <Image> ().color = inactiveColor ;
        }
        if (buttonClicked == spray)
        {
            sprayIconBG.GetComponent <Image> ().color = activeColor ;
        } else if (buttonClicked != spray)
        {
            sprayIconBG.GetComponent <Image> ().color = inactiveColor ;
        }
        if (buttonClicked == eraser)
        {
            eraserIconBG.GetComponent <Image> ().color = activeColor ;
        } else if (buttonClicked != eraser)
        {
            eraserIconBG.GetComponent <Image> ().color = inactiveColor ;
        }
    }
    void OnEnable ()
    {
        ink.onClick.AddListener (() => buttonCallBack (ink)) ;
        brush.onClick.AddListener (() => buttonCallBack (brush)) ;
        crayon.onClick.AddListener (() => buttonCallBack (crayon)) ;
        pencil.onClick.AddListener (() => buttonCallBack (pencil)) ;
        spray.onClick.AddListener (() => buttonCallBack (spray)) ;
        eraser.onClick.AddListener (() => buttonCallBack (eraser)) ;
    }

    void OnDisable ()
    {
    }
}

最有效的方式来切换按钮打开和关闭功能的脚本

用数组做这个会更好,但问题是你会丢失你的按钮和GameObjects的名称,这使得以后很难修改你的代码。

您可以使用Dictionary。制作ButtonGameObject对,然后手动添加每个Button来匹配每个图标/GameObject。然后,您可以在单击Button时循环它,将单击的ButtonDictionary中的键进行比较,然后根据比较结果分配activeColorinactiveColor

注意:如果您添加了更多的按钮和图标,您必须将它们也添加到pairButtonIcon()函数中。

有人想知道为什么我不使用foreach循环在Dictionary,这是因为它分配内存在Unity

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine.UI;
public class ToolButtons : MonoBehaviour
{
    public Color activeColor;
    public Color inactiveColor;
    public GameObject iconBG;
    public Button ink, brush, crayon, pencil, spray, eraser, chnageColor, brushSize, undo, redo, clear, newAnimal;
    public GameObject inkIconBG, brushIconBG, crayonIconBG, pencilIconBG, sprayIconBG, eraserIconBG, changeColorIconBG, brushSizeIconBG;
    Dictionary<Button, GameObject> buttonIconPair = new Dictionary<Button, GameObject>();
    void pairButtonIcon()
    {
        buttonIconPair.Add(ink, inkIconBG);
        buttonIconPair.Add(brush, brushIconBG);
        buttonIconPair.Add(crayon, crayonIconBG);
        buttonIconPair.Add(pencil, pencilIconBG);
        buttonIconPair.Add(spray, sprayIconBG);
        buttonIconPair.Add(eraser, eraserIconBG);
        buttonIconPair.Add(chnageColor, changeColorIconBG);
        buttonIconPair.Add(brushSize, brushSizeIconBG);
    }
    void Start()
    {
        pairButtonIcon();
        inactiveColor = iconBG.GetComponent<Image>().color;
    }
    // Use this for initialization
    void buttonCallBack(Button buttonClicked)
    {
        //My Code
        for (int i = 0; i < buttonIconPair.Count; i++)
        {
            var item = buttonIconPair.ElementAt(i);
            var itemKey = item.Key;
            var itemValue = item.Value;
            if (buttonClicked == itemKey)
            {
                itemValue.GetComponent<Image>().color = activeColor;
            }
            else
            {
                itemValue.GetComponent<Image>().color = inactiveColor;
            }
        }
    }
    void OnEnable()
    {
        ink.onClick.AddListener(() => buttonCallBack(ink));
        brush.onClick.AddListener(() => buttonCallBack(brush));
        crayon.onClick.AddListener(() => buttonCallBack(crayon));
        pencil.onClick.AddListener(() => buttonCallBack(pencil));
        spray.onClick.AddListener(() => buttonCallBack(spray));
        eraser.onClick.AddListener(() => buttonCallBack(eraser));
    }

    void OnDisable()
    {
    }
}

EDIT:你也可以使用多个Lists,然后在每个Button和GameObject/Icon上存储。

public class ToolButtons : MonoBehaviour
{
    public Color activeColor;
    public Color inactiveColor;
    public GameObject iconBG;
    public Button ink, brush, crayon, pencil, spray, eraser, chnageColor, brushSize, undo, redo, clear, newAnimal;
    public GameObject inkIconBG, brushIconBG, crayonIconBG, pencilIconBG, sprayIconBG, eraserIconBG, changeColorIconBG, brushSizeIconBG;
    List<Button> button = new List<Button>();
    List<GameObject> iconGameObjects = new List<GameObject>();
    void pairButtonIcon()
    {
        button.Add(ink);
        iconGameObjects.Add(inkIconBG);
        button.Add(brush);
        iconGameObjects.Add(brushIconBG);
        button.Add(crayon);
        iconGameObjects.Add(crayonIconBG);
        button.Add(pencil);
        iconGameObjects.Add(pencilIconBG);
        button.Add(spray);
        iconGameObjects.Add(sprayIconBG);
        button.Add(eraser);
        iconGameObjects.Add(eraserIconBG);
        button.Add(chnageColor);
        iconGameObjects.Add(changeColorIconBG);
        button.Add(brushSize);
        iconGameObjects.Add(brushSizeIconBG);
    }
    void Start()
    {
        pairButtonIcon();
        inactiveColor = iconBG.GetComponent<Image>().color;
    }
    // Use this for initialization
    void buttonCallBack(Button buttonClicked)
    {
        //My Code
        for (int i = 0; i < button.Count; i++)
        {
            if (buttonClicked == button[i])
            {
                iconGameObjects[i].GetComponent<Image>().color = activeColor;
            }
            else
            {
                iconGameObjects[i].GetComponent<Image>().color = inactiveColor;
            }
        }
    }
    void OnEnable()
    {
        ink.onClick.AddListener(() => buttonCallBack(ink));
        brush.onClick.AddListener(() => buttonCallBack(brush));
        crayon.onClick.AddListener(() => buttonCallBack(crayon));
        pencil.onClick.AddListener(() => buttonCallBack(pencil));
        spray.onClick.AddListener(() => buttonCallBack(spray));
        eraser.onClick.AddListener(() => buttonCallBack(eraser));
    }

    void OnDisable()
    {
    }
}