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