生活随笔
收集整理的這篇文章主要介紹了
Unity教程:如何使用枚举来帮助简化游戏开发
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
你是否曾經(jīng)在Unity游戲中工作過,想知道如何為你正在開發(fā)的系統(tǒng)創(chuàng)建一個自定義類型?好吧,在這個博客中,我們將回顧什么是枚舉,以及如何使用它們。然后我們將使用enums來指定一些UI輸入。 Enum是什么? 簡單地說,enum是您可以在腳本中創(chuàng)建的自定義類型。微軟在他們的文檔中使用的例子是在一周內(nèi)創(chuàng)建一個枚舉。所以,你創(chuàng)建了一個叫做天數(shù)的enum,你可以在你的程序中使用7個不同的日子:Sat,Sun,Mon,Tue,結(jié)婚,圖,星期五,你可以通過這些日子來調(diào)用這些。坐或Days.Mon。 要聲明上面提到的枚舉,你可以這樣做: ?
enum Days {Sat, Sun, Mon, Tue, Wed, Thu, Fri}; 復(fù)制代碼
每個枚舉類型(E。G:Sat,Sun,Mon)有它自己的底層類型,默認(rèn)情況下是int類型。所以,從技術(shù)上講,Sat,Sun和Mon都是0 1 2。可以指定枚舉類型的底層類型,但我不會詳細(xì)說明。如果您感興趣的話,請參閱“微軟文檔”。 為什么使用Enum ? 這似乎沒有必要,為什么我要使用枚舉呢?我承認(rèn),在他們的使用中,枚舉似乎是相當(dāng)具體的。很難看出枚舉在您的游戲中是有用的。直到最近,我才發(fā)現(xiàn)自己使用枚舉來克服Unity的問題。對于我來說,當(dāng)我意識到我想要創(chuàng)建的系統(tǒng)需要我創(chuàng)建5個獨立的bools來跟蹤我腳本的狀態(tài)時,我就決定使用enum。很明顯,在我的腳本中,有5個bools指示某種東西的狀態(tài),我的if-語句可能會導(dǎo)致一些奇怪的bug和行為,而這需要更多的時間來進行故障排除。我意識到,我可以通過使用enum來跟蹤腳本中的狀態(tài),從而糾正這種情況。 讓我們用枚舉來做些什么吧! 我嘗試創(chuàng)建的上述系統(tǒng)實際上非常簡單;用戶可以使用鍵盤上的箭頭鍵選擇4個項目的UI。每個項目都是向上、向下、左或右在UI面板上。我想要它,所以如果用戶按下,向上的項目就被選中了。這個選擇將在向上的方向上顯示一個逐漸消失的圖標(biāo)。 ?
例如,在上面的截圖中,如果用戶按下向上的箭頭,劍圖標(biāo)就會淡入淡出,顯示它被選中。此時,如果用戶再次按下按鈕,將執(zhí)行與up按鈕相關(guān)的命令。否則,如果用戶按下其他箭頭鍵,那么這些圖標(biāo)就會高亮顯示,并被認(rèn)為是活動的選擇。 現(xiàn)在讓我們重新創(chuàng)建這個系統(tǒng),這樣你就可以理解使用枚舉了。首先,創(chuàng)建一個新的unity項目。為了簡單起見,我們把它變成2D。我假設(shè)你們對統(tǒng)一有一個普遍的認(rèn)識,所以我不會解釋某些步驟。 · 創(chuàng)建一個新的畫布對象并在其中添加一個面板。 · 不管你想要怎樣調(diào)整面板的大小,我的是這樣的: ?
· 向UI面板添加4個按鈕。將對象重命名為向上、向下、左和右。將每個文本子的文本更改為與父對象的名稱相同。E。G,Up按鈕的文本應(yīng)該讀取“向上”。 ?
· 在你的面板上按這樣的方式組織按鈕,每個對象都相對于它的名字。例如,Up按鈕應(yīng)該位于面板的上部。 ?
· 在創(chuàng)建腳本之前,我們需要設(shè)置輸入。去編輯項目設(shè)置輸入。 · 在輸入管理器中,展開“軸”部分,并將“大小”從18增加到22。在創(chuàng)建的每個新按鈕上,將它們的名稱更改為上、下、左和右。對于每個按鈕,根據(jù)所修改的輸入,將“正按鈕”更改為上、下、左或右。 ?
每個按鈕都需要這樣做。到最后,你應(yīng)該有一個上,下,左,右的輸入。每個都應(yīng)該有一個對應(yīng)其名字的正按鈕。這將使我們的輸入檢測到鍵盤上的箭頭鍵輸入。 現(xiàn)在,單擊您的層次結(jié)構(gòu)中的Panel條目,并添加一個組件。添加一個cscript,并調(diào)用它。在您選擇的IDE中打開這個腳本。 · 將以下代碼復(fù)制到腳本中: ?
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!"); } } 復(fù)制代碼
· 現(xiàn)在,在您的場景中創(chuàng)建一個空的配子,并將TestMessage腳本附加到它。 · 轉(zhuǎn)到每個按鈕,在OnClick部分中,單擊+圖標(biāo)添加一個新的OnClick功能。 QQ號出售平臺 在OnClick()列表中,將新的GameObject拖放到一個沒有(Object)的部分中
· 然后,點擊“No函數(shù)”,選擇TestMessage測試() ?
這確保在調(diào)用時按鈕將調(diào)用我們的“測試”函數(shù)。 每個按鈕都要這樣做。 現(xiàn)在,試著運行你的場景,按下你的箭頭鍵。當(dāng)你按下相應(yīng)的箭頭鍵時,你應(yīng)該注意到圖像的消失。如果您有一個被選中的圖像,例如向上的圖像,并且您再次按下箭頭,測試函數(shù)應(yīng)該運行,您會注意到在您的控制臺中有一條消息說“測試成功了!” 結(jié)論 希望這個練習(xí)演示了枚舉是如何有用的。想象一下,如果您使用了bools而不是enum來嘗試指定在任何給定時間選擇對象,該系統(tǒng)將會是什么樣子。它會很快變得難看。if語句會變得非常冗長和混亂。你會把所有的bools設(shè)置為真和假。通過這樣做,你就能夠以一種清晰而簡明的方式記錄你的選擇。枚舉的命名很簡單,您用一個變量來控制您的選擇。
總結(jié)
以上是生活随笔 為你收集整理的Unity教程:如何使用枚举来帮助简化游戏开发 的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔 推薦給好友。