@@ -18,32 +18,28 @@ namespace GameLoversEditor
1818 /// {
1919 /// }
2020 /// </summary>
21- public abstract class EnumSelectorPropertyDrawer < T > : PropertyDrawer
21+ public abstract class EnumSelectorPropertyDrawer < T > : PropertyDrawer
2222 where T : Enum
2323 {
24- private static readonly Dictionary < Type , string [ ] > _sortedEnums = new Dictionary < Type , string [ ] > ( ) ;
25-
24+ private static readonly Dictionary < Type , GUIContent [ ] > _sortedEnums = new Dictionary < Type , GUIContent [ ] > ( ) ;
25+
2626 private bool _errorFound ;
27-
27+
2828 /// <inheritdoc />
2929 public override void OnGUI ( Rect position , SerializedProperty property , GUIContent label )
3030 {
3131 EditorGUI . BeginProperty ( position , label , property ) ;
3232
3333 var enumType = typeof ( T ) ;
3434 var enumValues = GetSortedEnumConstants ( enumType ) ;
35- var selectionWidth = Mathf . Clamp ( EditorGUIUtility . labelWidth , EditorGUIUtility . labelWidth , position . width * 0.5f ) ;
36- var selectionRect = new Rect ( position . x + position . width - selectionWidth , position . y , selectionWidth , position . height ) ;
3735 var selectionProperty = property . FindPropertyRelative ( "_selection" ) ;
3836 var currentString = selectionProperty . stringValue ;
39- var currentIndex = Array . IndexOf ( enumValues , currentString ) ;
40-
41- EditorGUI . LabelField ( position , label ) ;
42-
37+ var currentIndex = string . IsNullOrWhiteSpace ( currentString ) ? 0 : Array . FindIndex ( enumValues , s => s . text == currentString ) ;
38+
4339 if ( currentIndex != - 1 )
4440 {
45- selectionProperty . stringValue = enumValues [ EditorGUI . Popup ( selectionRect , currentIndex , enumValues ) ] ;
46-
41+ selectionProperty . stringValue = enumValues [ EditorGUI . Popup ( position , label , currentIndex , enumValues ) ] . text ;
42+
4743 _errorFound = false ;
4844 }
4945 else
@@ -52,37 +48,46 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten
5248 if ( ! _errorFound )
5349 {
5450 var targetObject = selectionProperty . serializedObject . targetObject ;
55-
51+
5652 Debug . LogError ( $ "Invalid enum constant: { enumType . Name } .{ currentString } in object { targetObject . name } of type: { targetObject . GetType ( ) . Name } ") ;
57-
53+
5854 _errorFound = true ;
5955 }
60-
56+
6157 var color = GUI . contentColor ;
62- var finalArray = new [ ] { "Invalid: " + currentString } . Concat ( enumValues ) . ToArray ( ) ;
63-
58+ var finalArray = new [ ] { new GUIContent ( "Invalid: " + currentString ) } . Concat ( enumValues ) . ToArray ( ) ;
59+
6460 GUI . contentColor = Color . red ;
65- var newSelection = EditorGUI . Popup ( selectionRect , 0 , finalArray ) ;
61+ var newSelection = EditorGUI . Popup ( position , label , 0 , finalArray ) ;
6662 GUI . contentColor = color ;
67-
63+
6864 if ( newSelection > 0 )
6965 {
70- selectionProperty . stringValue = finalArray [ newSelection ] ;
66+ selectionProperty . stringValue = finalArray [ newSelection ] . text ;
7167 }
7268 }
73-
69+
7470 EditorGUI . EndProperty ( ) ;
7571 }
76-
77- private string [ ] GetSortedEnumConstants ( Type enumType )
72+
73+ private GUIContent [ ] GetSortedEnumConstants ( Type enumType )
7874 {
79- if ( ! _sortedEnums . TryGetValue ( enumType , out var values ) )
75+ if ( ! _sortedEnums . TryGetValue ( enumType , out var content ) )
8076 {
81- values = Enum . GetNames ( enumType ) ;
77+ var values = Enum . GetNames ( enumType ) ;
78+
79+ content = new GUIContent [ values . Length ] ;
80+
8281 Array . Sort ( values ) ;
83- _sortedEnums . Add ( enumType , values ) ;
82+
83+ for ( var i = 0 ; i < values . Length ; i ++ )
84+ {
85+ content [ i ] = new GUIContent ( values [ i ] ) ;
86+ }
87+
88+ _sortedEnums . Add ( enumType , content ) ;
8489 }
85- return values ;
90+ return content ;
8691 }
8792 }
8893}
0 commit comments