【问题标题】:Most efficient way to script toggle buttons on and off functionality脚本切换按钮打开和关闭功能的最有效方法
【发布时间】:2016-12-27 10:58:14
【问题描述】:

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

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

    }
}

【问题讨论】:

  • 您了解 Toggle Group 组件吗?
  • 我知道@Aizen,但我以前没用过。
  • 切换组件仅选择一个组件(在您的情况下为按钮),您可以更改组件的外观或颜色。并且自动地,其他人将被设置为默认状态。偏离您自己的自定义定义。这应该是您了解组件的最佳时间,因此您将节省大量时间。重新创建 Unity3D 已经为您提供的组件。
  • @Aizen a ButtonToggle 不同。 .Toggle Group 用于Toggle 组件。 Toggle 就像一个开关。 Button 用于检测对图像的简单点击。两者的行为不同,Toggle Group 不适合这种情况。
  • “按钮与切换按钮不同”嗯,呵呵! “切换组用于切换组件” DUH DUH!? “按钮用于检测对图像的简单点击”不...... DUH....不......它是按钮而不是图像。检查代码。他想将按钮从非活动切换到活动,同时影响其他按钮。再试一次。显而易见的船长。

标签: c# button unity3d


【解决方案1】:

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

您可以为此使用Dictionary。创建一个Button--GameObject 对,然后手动添加每个Button 以匹配每个图标/游戏对象。然后可以在点击Button时循环遍历,将点击的ButtonDictionary中的key进行比较,然后根据比较结果分配activeColorinactiveColor

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

有人想知道为什么我没有在 Dictionary 上使用 foreach 循环,那是因为它在 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 和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()
    {

    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-24
    • 2018-01-30
    • 1970-01-01
    • 2013-01-19
    相关资源
    最近更新 更多