日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Unity3D研究院之Inspector面板枚举的别名与排序

發布時間:2025/3/21 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Unity3D研究院之Inspector面板枚举的别名与排序 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

雖然mono是支持unicode的。可以在枚舉里寫中文,但是我還是覺得寫英文好一些。可是在編輯器上策劃是希望看到的是中文的,還有就是枚舉的展示排序功能,策劃在編輯的時候為了方便希望把常用的枚舉排上前面。

把如下代碼放到你的工程里就可以直接用了。

C#
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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 using UnityEngine; using System; #if UNITY_EDITOR using UnityEditor; using System.Collections.Generic; using System.Linq; using System.Reflection; #endif [AttributeUsage(AttributeTargets.Enum | AttributeTargets.Field)] public class EnumLabelAttribute : PropertyAttribute { ????public string label; ????public int[] order = new int[0] ; ????public EnumLabelAttribute(string label) ????{ ????????this.label = label; ????} ????public EnumLabelAttribute(string label,params int[] order) ????{ ????????this.label = label; ????????this.order = order; ????} } #if UNITY_EDITOR [CustomPropertyDrawer(typeof(EnumLabelAttribute))] public class EnumLabelDrawer : PropertyDrawer { ????private Dictionary<string, string> customEnumNames = new Dictionary<string, string>(); ????public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) ????{ ????????SetUpCustomEnumNames(property, property.enumNames); ????????if (property.propertyType == SerializedPropertyType.Enum) ????????{ ????????????EditorGUI.BeginChangeCheck(); ????????????string[] displayedOptions = property.enumNames ????????????????????.Where(enumName => customEnumNames.ContainsKey(enumName)) ????????????????????.Select<string, string>(enumName => customEnumNames[enumName]) ????????????????????.ToArray(); ????????????int[] indexArray = GetIndexArray (enumLabelAttribute.order); ????????????if(indexArray.Length != displayedOptions.Length) ????????????{ ????????????????indexArray = new int[displayedOptions.Length]; ????????????????for(int i =0; i< indexArray.Length; i++){ ????????????????????indexArray[i] = i; ????????????????} ????????????} ????????????string[] items = new string[displayedOptions.Length]; ????????????items[0] = displayedOptions[0];?????? ????????????for (int i=0; i<displayedOptions.Length; i++) { ????????????????items[i] =??displayedOptions[indexArray[i]]; ????????????} ????????????int index = -1; ????????????for (int i=0; i<indexArray.Length; i++) { ????????????????if (indexArray[i] == property.enumValueIndex) { ????????????????????index = i; ????????????????????break; ????????????????} ????????????} ????????????if ( (index == -1) && (property.enumValueIndex != -1) ) { SortingError (position,property,label); return; } ????????????index = EditorGUI.Popup(position, enumLabelAttribute.label,index, items); ????????????if (EditorGUI.EndChangeCheck()) ????????????{ ????????????????if (index >= 0) ????????????????????property.enumValueIndex = indexArray[index]; ????????????} ????????} ????} ????private EnumLabelAttribute enumLabelAttribute ????{ ????????get ????????{ ????????????return (EnumLabelAttribute)attribute; ????????} ????} ????public void SetUpCustomEnumNames(SerializedProperty property, string[] enumNames) ????{ ????????????object[] customAttributes = fieldInfo.GetCustomAttributes(typeof(EnumLabelAttribute), false); ????????????foreach (EnumLabelAttribute customAttribute in customAttributes) ????????????{ ????????????????Type enumType = fieldInfo.FieldType; ????????????????foreach (string enumName in enumNames) ????????????????{ ????????????????????FieldInfo field = enumType.GetField(enumName); ????????????????????if (field == null) continue; ????????????????????EnumLabelAttribute[] attrs = (EnumLabelAttribute[])field.GetCustomAttributes(customAttribute.GetType(), false); ????????????????????if (!customEnumNames.ContainsKey(enumName)) ????????????????????{ ????????????????????????foreach (EnumLabelAttribute labelAttribute in attrs) ????????????????????????{ ????????????????????????????customEnumNames.Add(enumName, labelAttribute.label); ????????????????????????} ????????????????????} ????????????????} ????????????} ????} ????int[] GetIndexArray (int[] order) ????{ ????????int[] indexArray = new int[order.Length]; ????????for (int i = 0; i < order.Length; i++) { ????????????int index = 0; ????????????for (int j = 0; j < order.Length; j++) {???????????????? ????????????????if (order[i] > order[j]) {?????????????????? ????????????????????index++;???????????????? ????????????????}?????????????? ????????????} ????????????indexArray[i] = index; ????????} ????????return (indexArray); ????} ????void SortingError (Rect position, SerializedProperty property, GUIContent label) ????{ ????????EditorGUI.PropertyField(position, property, new GUIContent(label.text + " (sorting error)")); ????????EditorGUI.EndProperty(); ????} } public class EnumLabel { ????static public object GetEnum(Type type, SerializedObject serializedObject, string path) ????{ ????????SerializedProperty property =??GetPropety(serializedObject,path); ????????return??System.Enum.GetValues(type).GetValue(property.enumValueIndex); ????} ????static public object DrawEnum(Type type, SerializedObject serializedObject, string path) ????{ ????????return DrawEnum(type,serializedObject, GetPropety(serializedObject,path)); ????} ????static public object DrawEnum(Type type, SerializedObject serializedObject,SerializedProperty property) ????{ ????????serializedObject.Update(); ????????EditorGUILayout.PropertyField(property); ????????serializedObject.ApplyModifiedProperties(); ????????return??System.Enum.GetValues(type).GetValue(property.enumValueIndex); ????} ????static public SerializedProperty GetPropety(SerializedObject serializedObject, string path) ????{ ????????string []contents = path.Split('/'); ????????SerializedProperty property =??serializedObject.FindProperty(contents[0]); ????????for(int i=1; i< contents.Length; i++){ ????????????property = property.FindPropertyRelative(contents[i]); ????????} ????????return property; ????} } #endif

?

使用是這樣的,第二個參數就是排序。接收int的不固定參數。

C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 using UnityEngine; using System.Collections; public class NewBehaviourScript : MonoBehaviour { ????[EnumLabel("我是的類型",10,1,5,2)] ????public NewType newType = NewType.One; } public enum NewType : byte { ????[EnumLabel("我是1")] ????One = 10, ????[EnumLabel("我是2")] ????Two = 1, ????[EnumLabel("我是3")] ????Three = 5, ????[EnumLabel("我是4")] ????Four = 2 }

OK 中文與排序都OK了。

但是,有時候我們做編輯器的時候是自己調用OnInspectorGUI來繪制面板的。而且我的枚舉對象可能會在另外一個子對象里,或者在子對象里的一個List<T>里面的子對象里。

比如這樣, class.data.newType 就是這個類對象的結構,你可以按照你自己類的結構去拼這個字符串。

C#
1 2 3 4 5 6 7 8 9 10 ????????public override void OnInspectorGU() ????????{ ????????????NewType oldType = (NewType)EnumLabel.GetEnum(typeof(NewType),serializedObject,"class/data/newType"); ????????????NewType newType = (NewType)EnumLabel.DrawEnum(typeof(NewType),serializedObject,"class/data/newType"); ????????????if(oldType != newType) ????????????{ ????????????????//類型發生改變 ????????????} ????????}

還有一種特殊的就是可能枚舉在list<T>里,這樣在繪制的時候是需要遍歷的。

C#
1 2 3 4 5 6 7 8 9 10 ?????? public override void OnInspectorGU() ????????{ ????????????SerializedProperty property = EnumLabel.GetPropety(serializedObject,"class/datas"); ????????????for(int i =0; i<??count; i++) ????????????{ ????????????????SerializedProperty eProperty = property.GetArrayElementAtIndex(i); ????????????????NewType newType = (NewType)EnumLabel.DrawEnum(typeof(NewType),serializedObject ????????????????????????????,eProperty.FindPropertyRelative("newType")); ????????????} ????????}

OK大功告成。

參考文章:

?https://github.com/anchan828/property-drawer-collection/blob/master/EnumLabel/EnumLabelAttribute.cs

http://forum.unity3d.com/threads/enum-inspector-sorting-attribute.357558/

  • 本文固定鏈接: http://www.xuanyusong.com/archives/4213
  • 轉載請注明: 雨松MOMO 2016年07月13日 于 雨松MOMO程序研究院 發表
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

總結

以上是生活随笔為你收集整理的Unity3D研究院之Inspector面板枚举的别名与排序的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。