你是否曾经在Unity游戏中工作过,想知道如何为你正在开发的系统创建一个自定义类型?好吧,在这个博客中,我们将回顾什么是枚举,以及如何使用它们。然后我们将使用enums来指定一些UI输入。

Enum是什么?

简单地说,enum是您可以在脚本中创建的自定义类型。微软在他们的文档中使用的例子是在一周内创建一个枚举。所以,你创建了一个叫做天数的enum,你可以在你的程序中使用7个不同的日子:Sat,Sun,Mon,Tue,结婚,图,星期五,你可以通过这些日子来调用这些。坐或Days.Mon。

要声明上面提到的枚举,你可以这样做:
 

  1. enum Days {Sat, Sun, Mon, Tue, Wed, Thu, Fri};


·向UI面板添加4个按钮。将对象重命名为向上、向下、左和右。将每个文本子的文本更改为与父对象的名称相同。E。G,Up按钮的文本应该读取“向上”。

132839rvixpky6i76czx6x.png
·在你的面板上按这样的方式组织按钮,每个对象都相对于它的名字。例如,Up按钮应该位于面板的上部。

132840j1o0zc41j8xpxove.png
·在创建脚本之前,我们需要设置输入。去编辑项目设置输入。

·在输入管理器中,展开“轴”部分,并将“大小”从18增加到22。在创建的每个新按钮上,将它们的名称更改为上、下、左和右。对于每个按钮,根据所修改的输入,将“正按钮”更改为上、下、左或右。

132840kwqbrzqqc4llx5lq.png
每个按钮都需要这样做。到最后,你应该有一个上,下,左,右的输入。每个都应该有一个对应其名字的正按钮。这将使我们的输入检测到键盘上的箭头键输入。

现在,单击您的层次结构中的Panel条目,并添加一个组件。添加一个cscript,并调用它。在您选择的IDE中打开这个脚本。

·将以下代码复制到脚本中:

 

 

  1. using UnityEngine;
    • using System.Collections;
      • using UnityEngine.UI;
        •  
        • public class SkillInput : MonoBehaviour
          • {
            •  
            • [SerializeField]
              • float fadeRate = 4f; //Used to adjust image fade speed
                •  
                • enum Selection { None, Up, Down, Left, Right }; //Will be used to keep track of whats selected
                  • Selection currentSel; // Create a Selection object that will be used throughout script
                    •  
                    • Image imgUp, imgDown, imgLeft, imgRight; //These variables will be used for fading the buttons when selected
                      • Button buttonUp, buttonDown, buttonLeft, buttonRight; //Will be used to invoke Button functions
                        •  
                        • void Start()
                          • {
                            • currentSel = Selection.None; //assign currentSel to None.
                              •  
                              • //Grab the Image components of all our buttons
                                • imgUp = transform.FindChild(Up).GetComponent<Image>();
                                  • imgDown = transform.FindChild(Down).GetComponent<Image>();
                                    • imgLeft = transform.FindChild(Left).GetComponent<Image>();
                                      • imgRight = transform.FindChild(Right).GetComponent<Image>();
                                        •  
                                        • //Grab the Button components of all our buttons
                                          • buttonUp = transform.FindChild(Up).GetComponent<Button>();
                                            • buttonDown = transform.FindChild(Down).GetComponent<Button>();
                                              • buttonLeft = transform.FindChild(Left).GetComponent<Button>();
                                                • buttonRight = transform.FindChild(Right).GetComponent<Button>();
                                                  • }
                                                    •  
                                                    • void Update()
                                                      • {
                                                        • //Standard input calls.
                                                          • if (Input.GetButtonDown(Up))
                                                            • {
                                                              • if (currentSel == Selection.Up)
                                                                • {
                                                                  • //Executes if we already have up selected and user presses up again
                                                                    • buttonUp.onClick.Invoke(); //Call up buttons OnClick() function
                                                                      • currentSel = Selection.None; //set currentSel back to None
                                                                        • }
                                                                          • else
                                                                            • {
                                                                              • currentSel = Selection.Up; // changes currentSel to Up.
                                                                                • StartCoroutine(FadeIcon(imgUp, currentSel)); //Begins fading the icon
                                                                                  • }
                                                                                    • }
                                                                                      • //The same code pattern from above is repeated for the rest of the inputs
                                                                                        • else if (Input.GetButtonDown(Down))
                                                                                          • {
                                                                                            • if (currentSel == Selection.Down)
                                                                                              • {
                                                                                                • buttonDown.onClick.Invoke();
                                                                                                  • currentSel = Selection.None;
                                                                                                    • }
                                                                                                      • else
                                                                                                        • {
                                                                                                          • currentSel = Selection.Down;
                                                                                                            • StartCoroutine(FadeIcon(imgDown, currentSel));
                                                                                                              • }
                                                                                                                • }
                                                                                                                  • else if (Input.GetButtonDown(Left))
                                                                                                                    • {
                                                                                                                      • if (currentSel == Selection.Left)
                                                                                                                        • {
                                                                                                                          • buttonLeft.onClick.Invoke();
                                                                                                                            • currentSel = Selection.None;
                                                                                                                              • }
                                                                                                                                • else
                                                                                                                                  • {
                                                                                                                                    • currentSel = Selection.Left;
                                                                                                                                      • StartCoroutine(FadeIcon(imgLeft, currentSel));
                                                                                                                                        • }
                                                                                                                                          • }
                                                                                                                                            • else if (Input.GetButtonDown(Right))
                                                                                                                                              • {
                                                                                                                                                • if (currentSel == Selection.Right)
                                                                                                                                                  • {
                                                                                                                                                    • buttonRight.onClick.Invoke();
                                                                                                                                                      • currentSel = Selection.None;
                                                                                                                                                        • }
                                                                                                                                                          • else
                                                                                                                                                            • {
                                                                                                                                                              • currentSel = Selection.Right;
                                                                                                                                                                • StartCoroutine(FadeIcon(imgRight, currentSel));
                                                                                                                                                                  • }
                                                                                                                                                                    • }
                                                                                                                                                                      • }
                                                                                                                                                                        •  
                                                                                                                                                                        • IEnumerator FadeIcon(Image img, Selection sel)
                                                                                                                                                                          • {
                                                                                                                                                                            • //basic Fade Coroutine. For more Information:
                                                                                                                                                                              • //https://www.studica.com/blog/create-a-fading-splash-screen-using-coroutines-in-unity-3d
                                                                                                                                                                                • float alpha = 1f;
                                                                                                                                                                                  •  
                                                                                                                                                                                  • while (currentSel == sel)
                                                                                                                                                                                    • {
                                                                                                                                                                                      • while (img.color.a > 0)
                                                                                                                                                                                        • {
                                                                                                                                                                                          • alpha -= Time.deltaTime * fadeRate;
                                                                                                                                                                                            • img.color = new Color(img.color.r, img.color.g, img.color.b, alpha);
                                                                                                                                                                                              • yield return null;
                                                                                                                                                                                                • }
                                                                                                                                                                                                  • while (img.color.a < 1)
                                                                                                                                                                                                    • {
                                                                                                                                                                                                      • alpha += Time.deltaTime * fadeRate;
                                                                                                                                                                                                        • img.color = new Color(img.color.r, img.color.g, img.color.b, alpha);
                                                                                                                                                                                                          • yield return null;
                                                                                                                                                                                                            • }
                                                                                                                                                                                                              • yield return null;
                                                                                                                                                                                                                • }
                                                                                                                                                                                                                  • img.color = new Color(img.color.r, img.color.g, img.color.b, 1f);
                                                                                                                                                                                                                    • }
                                                                                                                                                                                                                      • }
                                                                                                                                                                                                                        •  
                                                                                                                                                                                                                        • Now, we need to setup some OnClick functionality for our buttons. First thing’s first, create a new C# Script called “TestMessage”. Open up the script and copy this code into it:
                                                                                                                                                                                                                          • using UnityEngine;
                                                                                                                                                                                                                            • using System.Collections;
                                                                                                                                                                                                                              •  
                                                                                                                                                                                                                              • public class TestMessage : MonoBehaviour {
                                                                                                                                                                                                                                •  
                                                                                                                                                                                                                                • void Start ()
                                                                                                                                                                                                                                  • {
                                                                                                                                                                                                                                    •  
                                                                                                                                                                                                                                    • }
                                                                                                                                                                                                                                      •  
                                                                                                                                                                                                                                      • void Update ()
                                                                                                                                                                                                                                        • {
                                                                                                                                                                                                                                          •  
                                                                                                                                                                                                                                          • }
                                                                                                                                                                                                                                            •  
                                                                                                                                                                                                                                            • public void Testing()
                                                                                                                                                                                                                                              • {
                                                                                                                                                                                                                                                • Debug.Log(Test Succeeded!);
                                                                                                                                                                                                                                                  • }
                                                                                                                                                                                                                                                    • }
                                                                                                                                                                                                                                                      •  
                                                                                                                                                                                                                                                      •  
                                                                                                                                                                                                                                                      • //Now, we need to setup some OnClick functionality for our buttons. First thing’s first, create a new C# Script called “TestMessage”. Open up the script and copy this code //into it:
                                                                                                                                                                                                                                                        • using UnityEngine;
                                                                                                                                                                                                                                                          • using System.Collections;
                                                                                                                                                                                                                                                            •  
                                                                                                                                                                                                                                                            • public class TestMessage : MonoBehaviour {
                                                                                                                                                                                                                                                              •