|
你是否曾经在Unity游戏中工作过,想知道如何为你正在开发的系统创建一个自定义类型?好吧,在这个博客中,我们将回顾什么是枚举,以及如何使用它们。然后我们将使用enums来指定一些UI输入。
Enum是什么?
简单地说,enum是您可以在脚本中创建的自定义类型。微软在他们的文档中使用的例子是在一周内创建一个枚举。所以,你创建了一个叫做天数的enum,你可以在你的程序中使用7个不同的日子:Sat,Sun,Mon,Tue,结婚,图,星期五,你可以通过这些日子来调用这些。坐或Days.Mon。
要声明上面提到的枚举,你可以这样做:
- enum Days {Sat, Sun, Mon, Tue, Wed, Thu, Fri};
复制代码
每个枚举类型(E。G:Sat,Sun,Mon)有它自己的底层类型,默认情况下是int类型。所以,从技术上讲,Sat,Sun和Mon都是0 1 2。可以指定枚举类型的底层类型,但我不会详细说明。如果您感兴趣的话,请参阅“微软文档”。
为什么使用Enum ?
这似乎没有必要,为什么我要使用枚举呢?我承认,在他们的使用中,枚举似乎是相当具体的。很难看出枚举在您的游戏中是有用的。直到最近,我才发现自己使用枚举来克服Unity的问题。对于我来说,当我意识到我想要创建的系统需要我创建5个独立的bools来跟踪我脚本的状态时,我就决定使用enum。很明显,在我的脚本中,有5个bools指示某种东西的状态,我的if-语句可能会导致一些奇怪的bug和行为,而这需要更多的时间来进行故障排除。我意识到,我可以通过使用enum来跟踪脚本中的状态,从而纠正这种情况。
让我们用枚举来做些什么吧!
我尝试创建的上述系统实际上非常简单;用户可以使用键盘上的箭头键选择4个项目的UI。每个项目都是向上、向下、左或右在UI面板上。我想要它,所以如果用户按下,向上的项目就被选中了。这个选择将在向上的方向上显示一个逐渐消失的图标。
例如,在上面的截图中,如果用户按下向上的箭头,剑图标就会淡入淡出,显示它被选中。此时,如果用户再次按下按钮,将执行与up按钮相关的命令。否则,如果用户按下其他箭头键,那么这些图标就会高亮显示,并被认为是活动的选择。
现在让我们重新创建这个系统,这样你就可以理解使用枚举了。首先,创建一个新的unity项目。为了简单起见,我们把它变成2D。我假设你们对统一有一个普遍的认识,所以我不会解释某些步骤。
·创建一个新的画布对象并在其中添加一个面板。
·不管你想要怎样调整面板的大小,我的是这样的:
·向UI面板添加4个按钮。将对象重命名为向上、向下、左和右。将每个文本子的文本更改为与父对象的名称相同。E。G,Up按钮的文本应该读取“向上”。
·在你的面板上按这样的方式组织按钮,每个对象都相对于它的名字。例如,Up按钮应该位于面板的上部。
·在创建脚本之前,我们需要设置输入。去编辑项目设置输入。
·在输入管理器中,展开“轴”部分,并将“大小”从18增加到22。在创建的每个新按钮上,将它们的名称更改为上、下、左和右。对于每个按钮,根据所修改的输入,将“正按钮”更改为上、下、左或右。
每个按钮都需要这样做。到最后,你应该有一个上,下,左,右的输入。每个都应该有一个对应其名字的正按钮。这将使我们的输入检测到键盘上的箭头键输入。
现在,单击您的层次结构中的Panel条目,并添加一个组件。添加一个cscript,并调用它。在您选择的IDE中打开这个脚本。
·将以下代码复制到脚本中:
- 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 what's 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 button's 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 {
-
- void Start ()
- {
-
- }
-
- void Update ()
- {
-
- }
-
- public void Testing()
- {
- Debug.Log("Test Succeeded!");
- }
- }
复制代码
·现在,在您的场景中创建一个空的配子,并将TestMessage脚本附加到它。
·转到每个按钮,在OnClick部分中,单击+图标添加一个新的OnClick功能。在OnClick()列表中,将新的GameObject拖放到一个没有(Object)的部分中
·然后,点击“No函数”,选择TestMessage测试()
这确保在调用时按钮将调用我们的“测试”函数。
每个按钮都要这样做。
现在,试着运行你的场景,按下你的箭头键。当你按下相应的箭头键时,你应该注意到图像的消失。如果您有一个被选中的图像,例如向上的图像,并且您再次按下箭头,测试函数应该运行,您会注意到在您的控制台中有一条消息说“测试成功了!”
结论
希望这个练习演示了枚举是如何有用的。想象一下,如果您使用了bools而不是enum来尝试指定在任何给定时间选择对象,该系统将会是什么样子。它会很快变得难看。if语句会变得非常冗长和混乱。你会把所有的bools设置为真和假。通过这样做,你就能够以一种清晰而简明的方式记录你的选择。枚举的命名很简单,您用一个变量来控制您的选择。
via:游戏蛮牛
声明:游资网登载此文出于传递信息之目的,绝不意味着游资网赞同其观点或证实其描述。
|
|