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

时间:2016-08-20 10:54:17

标签: c# button unity3d

我写了一些改变按钮视觉状态的代码(无论点击哪个按钮都会突出显示为蓝色,并将所有其他按钮恢复为默认颜色)。它似乎工作正常,但感觉很麻烦。有没有更有效/简洁的方法来重写我的代码?非常感谢!

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 ()
    {

    }
}

1 个答案:

答案 0 :(得分:1)

使用数组执行此操作会更好,但问题是您将丢失按钮和游戏对象的名称,这使得以后很难修改代码。

您可以使用Dictionary。创建一个Button - GameObject对,然后手动添加每个Button以匹配每个Icon / 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()
    {

    }
}

编辑:您还可以使用多个Lists,然后存储Button和每个游戏对象/图标。

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()
    {

    }
}