Unity C# 用枚舉(enum)製作複選框

來源:互聯網
上載者:User

標籤:...   label   mes   code   log   position   應用   test   not   

 

 

最近在項目中做測試指令碼用到一些布爾值做方法的開關,突然想到可以製作一個複選框控制開關。

首先搜集網上的資料,基本大同小異,這裡就不多做解釋了,代碼附上:

1 public class EnumFlagsAttribute : PropertyAttribute{}
 1 [CustomPropertyDrawer(typeof(EnumFlagsAttribute))] 2 public class EnumFlagsDrawer:PropetyDrawer 3 { 4     public override void OnGUI(Rect position,SerializedProperty property,GUIContent label) 5     { 6         /*繪製枚舉複選框 , 0-Nothing,-1-Everything,其他是枚舉之和 7         枚舉值(2的x次冪):2的0次冪=1,2的1次冪=2,2的2次冪=4,8,16... 8         */ 9         property.intValue = EditorGUI.MaskField(position,label,property.intValue,property.enumNames);10     }11 }

下面是應用:

 1 [System.Flags] 2 public enum TestEnum 3 { 4     One=1, 5     Two=2, 6     Three=4, 7     Four=8, 8 } 9 10 public class testEnum11 {12     [EnumFlags]13     public TestEnum _testEnum;14     15     if((int)(_testEnum&TestEnum.One)==1)16         DoSomething1();17     if((int)(_testEnum&TestEnum.Two)==2)18         DoSomething2();19     if((int)(_testEnum&TestEnum.Three)==4)20         DoSomething3();21     if((int)(_testEnum&TestEnum.Four)==8)22         DoSomething4();

 

Unity C# 用枚舉(enum)製作複選框

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.