最有效的脚本切换按钮打开和关闭功能的方法(Most efficient way to script toggle buttons on and off functionality)

编程入门 行业动态 更新时间:2024-10-07 12:27:58
最有效的脚本切换按钮打开和关闭功能的方法(Most efficient way to script toggle buttons on and off functionality)

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

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

I wrote a bit of code that changes the visual state of a button (whichever button is clicked gets highlighted blue and returns all other buttons back to their default color). It seems to work fine but it feels cumbersome. Is there a more efficient/succinct way to re-write my code? Many thanks!

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

最满意答案

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

您可以使用Dictionary进行此操作。 创建一个Button - GameObject对,然后手动添加每个Button以匹配每个Icon / GameObject。 然后,您可以在单击Button时循环浏览它,将单击的Button与Dictionary的键进行比较,然后根据比较结果指定activeColor或inactiveColor 。

注意 :如果添加更多按钮和图标,则必须将它们添加到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然后在每个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() { } }

Doing this with array would have been better but the problem is that you would lose the names of your Buttons and GameObjects making it hard to modify your code later on.

You can use Dictionary for this. Make a Button--GameObject pair then manually add each Button to match each Icon/GameObject. You can then loop through it when Button is clicked, compare the the Button that was clicked with the key in the Dictionary, then assign activeColor or inactiveColor based on the comparison result.

Note: If you add more Buttons and Icons, you must add them too to the pairButtonIcon() function.

Anyone wondering why I didn't use foreach loop over the Dictionary, that's because it allocates memory in 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: You can also use multiple Lists then store Button and GameObject/Icon on each one.

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

更多推荐

本文发布于:2023-08-07 12:57:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464056.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:最有效   脚本   按钮   功能   方法

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!