Unity中的枚舉和標誌

譯林軍 宿學龍|2014-04-10 08:56|9007次瀏覽| Unity(377) 0

枚舉和標誌php

今天的主題是枚舉,它是C#語言中的一個頗有幫助的工具,能夠加強代碼的清晰度以及準確性。html

枚舉一系列值ios

C#中最常常使用枚舉的地方就是用來定義一系列可能值的時候。例如在製做一個角色類遊戲中,角色有多個不一樣的狀態效果。能夠用一些基本的方式來定義玩家可能的狀態效果:web

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
using UnityEngine;
  
public class Player : MonoBehaviour
{
     public string status;
  
     void Update()
     {
         if (status == "Poison" )
         {
             //Apply poison effect
         }
         else if (status == "Slow" )
         {
             //Apply slow effect
         }
         else if (status == "Mute" )
         {
             //Apply mute effect
         }
     }
}

此處咱們用一個簡單的if-else檢測來查看應用的是哪一個狀態效果,哪一個運行良好。可是若是你想應用一個狀態效果,可是忽然寫成這樣:編輯器

1
2
//Oops, I spelled "Poison" wrong!
status = "Pioson" ;

忽然,就不會有什麼狀態效果了。咱們會利用if-else棧檢測來查看咱們的變量是否被設置成一個非法值,可是若是使用枚舉,則是一種更好的方式:工具

定義一個枚舉this

此次咱們使用一個枚舉來編寫咱們的狀態效果:編碼

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
using UnityEngine;
  
public class Player : MonoBehaviour
{
     //This is the "set" of all possible status effects
     public enum StatusEffect
     {
         None,
         Poison,
         Slow,
         Mute
     }
  
     //Now we can make a variable using that set as its type!
     public StatusEffect status;
  
     void Update()
     {
         if (status == StatusEffect.Poison)
         {
             //Apply poison effect
         }
         else if (status == StatusEffect.Slow)
         {
             //Apply slow effect
         }
         else if (status == StatusEffect.Mute)
         {
             //Apply mute effect
         }
     }
}

這裏咱們定義了一個叫StatusEffect的枚舉,經過給狀態賦值,咱們強制編譯器只等於StatusEffect中的一個值,若是還像以前賦值:url

1
2
//This type doesn't even exist, ERROR TIME!
status = StatusEffect.Pioson;

遊戲就不可能運行,編輯器會拋出一個錯誤,告訴你正在使用一個非法的枚舉值,並指出你須要修復的確切的代碼行。spa

在上述代碼中,我沒有制定狀態的默認值,一般來講,一個枚舉都是指定集合中的第一個值做爲默認值的,所以在此例中,我已經說過,StatusEffect變量的任意默認值都是StatusEffect.None。

若是你發現Unity中有專門爲UI自定義的枚舉,那就更好了,當我打開Player組件的查看器時,看看發生什麼了。

enum_dropdown

一個乾淨整潔的下拉菜單用來選擇定義的類型!

枚舉位標誌

StatusEffect枚舉,以下:

1
2
3
4
5
6
7
8
[System.Flags]
public enum StatusEffect
{
     None    = 1,
     Poison    = 2,
     Slow    = 4,
     Mute    = 8
}

經過如此,咱們告知編譯器將此枚舉看作是位標記,表示每一個不一樣的枚舉值表明一個不一樣的位。這意味着咱們必須經過給它們用2的冪來賦值,以此告知每一個枚舉值。另外一種就是經過獲取兩個值的冪,而後經過移位來實現:

1
2
3
4
5
6
7
8
[System.Flags]
public enum StatusEffect
{
     None   = 1 << 0, // 1
     Poison = 1 << 1, // 2
     Slow   = 1 << 2, // 4
     Mute   = 1 << 3  // 8
}

如今能夠經過日常的位操做來控制這些枚舉域了。若是你對位操做不熟悉,我會簡單講解下如何使用。

枚舉標誌和位操做符

如今咱們的枚舉值是經過System.Flags標記的,咱們能夠像以前同樣,以下來作:

1
2
//Character is poisoned!
status = StatusEffect.Poison;

但若是玩家中毒或者行動緩慢怎麼辦?咱們可使用或|操做符來將多個狀態值合併一塊兒:

1
2
3
4
5
6
7
8
9
//Now we are poisoned *and* slowed!
status = StatusEffect.Poison | StatusEffect.Slow;
  
//We could also do this:
//First we are poisoned, like so
status = StatusEffect.Poison;
  
//Then we become *also* slowed later
status |= StatusEffect.Slow;

若是咱們想移除一個狀態效果,咱們可使用&操做符和取反~。

1
2
3
4
5
//First, we are poisoned and muted
status = StatusEffect.Poison | StatusEffect.Mute;
  
//But now we want to remove just the poison, and leave him mute!
status &= ~StatusEffect.Poison;

最後若是咱們檢測發現若是一個玩家在if語句中,咱們可使用&操做符來查看一個特定的位值:

1
2
3
4
5
//Let's check if we are poisoned:
if ((status & StatusEffect.Poison) == StatusEffect.Poison)
{
     //Yep, definitely poisoned!
}

擴展閱讀

我不想此文全講解位標誌和位操做,所以我只是簡要解釋了下。可是若是你不瞭解位操做,我強烈建議你弄懂它們並制定如何使用。

我我的喜歡維基百科的這個網頁:

http://en.wikipedia.org/wiki/Bitwise_operation

同時,System.Flags標記的枚舉不會直接在Unity的查看其中正確顯示,它們不會讓你選擇設置某個標記的,以後以非標記的枚舉類型顯示。但慶幸的是,你能夠本身寫一個自定義的屬性着色器來解決,以後我會在此文追加的!

https://docs.unity3d.com/Documentation/ScriptReference/PropertyDrawer.html

快樂編碼!

原文連接:Enums and Flags

相關文章
相關標籤/搜索