FishPlayer

一个喜欢摸鱼的废物

0%

Unity编辑器中以enum的方式绘制int

项目里的UI框架有非常方便实用的数据绑定功能。各种UI表现件也可以根据数据集里的某个int改变UI表现。但是比较逆天的是我们经常把enum转int然后再到表现件上去调对应的各种状态。
对着magic number调配置确实很让人苦恼。

思路就是再需要根据int来调偶配置的component上添加一个编辑器里专用的Type对象,然后根据这个对象类型去画int。基于Odin做的,但我觉得这个思路应该还不错。

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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190

// use a string to temply store the type

[AttributeUsage(AttributeTargets.Field)]
public class EnumTypeQualifiedName: Attribute { }

[DrawerPriority(DrawerPriorityLevel.WrapperPriority)]
public class EnumTypeQualifiedNameAttributeDrawer : OdinAttributeDrawer<EnumTypeQualifiedName, string>
{
class EditingState
{
public static IEnumerable<Type> tempQuery;

public bool wannaSet = false;
public Type selectedType = null;
}

protected override void DrawPropertyLayout(GUIContent label)
{
string currentName = this.ValueEntry.SmartValue;
Type enumType = string.IsNullOrEmpty(currentName) ? null : Type.GetType(currentName);
string enumTypeNiceName = enumType == null ? "invalid!!!" : enumType.GetNiceName();

int controlId = GUIUtility.GetControlID(FocusType.Passive);
EditingState state = GUIUtility.GetStateObject(typeof(EditingState), controlId) as EditingState;
if (state.wannaSet)
{
currentName = state.selectedType.AssemblyQualifiedName;
state.selectedType = null;
state.wannaSet = false;
this.ValueEntry.SmartValue = currentName;
}
EditorGUILayout.BeginVertical();
GUIStyle guiStyle = new GUIStyle(EditorStyles.label);
guiStyle.alignment = TextAnchor.MiddleLeft;
EditorGUILayout.LabelField(this.Property.NiceName, guiStyle);

guiStyle = new GUIStyle(EditorStyles.popup);
guiStyle.alignment = TextAnchor.MiddleLeft;
if (EditorGUILayout.DropdownButton(new GUIContent($"Preview Enum Type ({enumTypeNiceName})"), FocusType.Keyboard, guiStyle))
{
if (EditingState.tempQuery == null)
{
EditingState.tempQuery = GetTargetEnumTypes();
}

TypeSelector selector = new TypeSelector(EditingState.tempQuery, false);
selector.EnableSingleClickToSelect();
selector.ShowInPopup();
selector.SelectionConfirmed += selection =>
{
var resultTypeArray = selection.ToArray();
if (resultTypeArray.Length > 0)
{
Type selectedType = resultTypeArray.GetValue(0) as Type;
EditingState state = GUIUtility.GetStateObject(typeof(EditingState), controlId) as EditingState;
state.selectedType = selectedType;
state.wannaSet = true;
}
};
}
EditorGUILayout.EndVertical();
}

private static IEnumerable<Type> GetTargetEnumTypes()
{
// TODO @Hiko here to get actual enum types
TypeCache.TypeCollection cache = TypeCache.GetTypesDerivedFrom<Enum>();
var query = cache.Where(t => t.IsEnum && t.IsPublic);
return query;
}

}

// put temply data into a sturct

[Serializable]
public struct IntAsEnumEditorPack
{
public bool showIntAsEnumForTaggedField;
[EnumTypeQualifiedName]
public string enumTypeQualifiedName;
}

public interface IIntAsEnumPackHolder
{
#if UNITY_EDITOR
IntAsEnumEditorPack holderData { get; }
#endif
}

// draw enum popup for int

[AttributeUsage(AttributeTargets.Field)]
public class IntAsEnumAttribute : Attribute { }

[DrawerPriority(DrawerPriorityLevel.WrapperPriority)]
public class IntAsEnumAttributeDrawer : OdinAttributeDrawer<IntAsEnumAttribute, int>
{
protected override void DrawPropertyLayout(GUIContent label)
{
int nextValue, prevValue = this.ValueEntry.SmartValue;
InspectorProperty containerProterty = this.ValueEntry.Property;
Type holderType = typeof(IIntAsEnumPackHolder);
Type containerType = containerProterty.ParentType;
bool doContinue = true;
while (doContinue)
{
Type parentType = containerProterty.ParentType;
if (parentType == null)
{
break;
}
containerType = parentType;
containerProterty = containerProterty.Parent;
if (holderType.IsAssignableFrom(containerType))
{
break;
}
}

GUIStyle guiStyle = new GUIStyle(EditorStyles.label);
guiStyle.alignment = TextAnchor.MiddleLeft;
if (containerType != null && holderType.IsAssignableFrom(containerType))
{
IIntAsEnumPackHolder holder = containerProterty.ValueEntry.WeakSmartValue as IIntAsEnumPackHolder;
IntAsEnumEditorPack packData = holder.holderData;
if (packData.showIntAsEnumForTaggedField)
{
EditorGUILayout.BeginVertical();
EditorGUILayout.LabelField(this.Property.NiceName, guiStyle);
Type enumType = string.IsNullOrEmpty(packData.enumTypeQualifiedName) ? null : Type.GetType(packData.enumTypeQualifiedName);
if (enumType != null && enumType.IsEnum)
{
Enum prevEnumValue;
if (!enumType.IsEnumDefined(prevValue))
{
prevEnumValue = Enum.GetValues(enumType).GetValue(0) as Enum;
}
else
{
prevEnumValue = (Enum)Enum.ToObject(enumType, prevValue);
}
guiStyle = new GUIStyle(EditorStyles.popup);
guiStyle.stretchWidth = false;
guiStyle.alignment = TextAnchor.MiddleLeft;
Enum nextEnumValue = EditorGUILayout.EnumPopup(prevEnumValue, guiStyle);
nextValue = Convert.ToInt32(nextEnumValue);
this.ValueEntry.SmartValue = nextValue;
guiStyle = new GUIStyle(EditorStyles.label);
guiStyle.alignment = TextAnchor.MiddleLeft;
guiStyle.stretchWidth = false;
EditorGUILayout.LabelField($"int value: ({nextValue})", guiStyle);
}
EditorGUILayout.EndVertical();
return;
}

}
// can not find the target preview enum type, draw normal int field
EditorGUILayout.BeginHorizontal();
EditorGUILayout.LabelField(this.Property.NiceName);
nextValue = EditorGUILayout.IntField(prevValue);
this.ValueEntry.SmartValue = nextValue;
EditorGUILayout.EndHorizontal();
}
}

// test case
public class TesterA : MonoBehaviour, IIntAsEnumPackHolder
{
#if UNITY_EDITOR
[SerializeField]
private IntAsEnumEditorPack intAsEnumEditorPack;
public IntAsEnumEditorPack holderData => intAsEnumEditorPack;
#endif

[Serializable]
public struct TempStructA
{
[IntAsEnum]
public int tempInt;
public Color tempColor;
}

public TempStructA[] tempArray;

[IntAsEnum]
public int tempIntA;
}

效果如下

用起来是方便了,但是也引入了一个问题。这个做法会需要序列化一这两个小小的字段到物体上。导致Prefab变肥了,但感觉打包的时候打出来的prefab里应该不会包含这俩字段,应该还凑合啦。