1+ using System ;
2+ using System . Collections . Generic ;
3+
4+ using UnityEngine ;
5+ using UnityEditor ;
6+
7+ using BigasTools . Editor ;
8+ [ CustomPropertyDrawer ( typeof ( KeyCode ) ) ]
9+ [ CanEditMultipleObjects ]
10+ public class KeyCodeDrawer : PropertyDrawer
11+ {
12+ SerializedProperty p ;
13+
14+ public override void OnGUI ( Rect position , SerializedProperty property , GUIContent label )
15+ {
16+ p = property ;
17+ EditorGUI . BeginProperty ( position , label , property ) ;
18+
19+ // Display the property name.
20+ //EditorGUI.LabelField(position, ObjectNames.NicifyVariableName(property.name));
21+
22+ // Unity SerializedProperty has no direct way to set an enum, it uses an index value
23+ // which is unrelated to the KeyCode enumeration. The index value is the order of the enum
24+ // in the default pop-up list. KeyCode enumeration might follow something like ASCII (unsure if 100% true).
25+ //
26+ // To bypass this, we can get the names of the KeyCode enums as a string array which are in the same order
27+ // as the popup list order, this order is the SerializedProperty.enumValueIndex.
28+
29+ // To get the true KeyCode from a serialized property we do:
30+
31+ // Get access to the KeyCode enums as a string array of enum names.
32+ string [ ] enums = property . enumNames ;
33+
34+ // Get the current KeyCode name held by the serialized property by accessing the array value at enumValueIndex.
35+ string propEnumName = enums [ property . enumValueIndex ] ;
36+
37+ // Get the KeyCode enum by converting the name to KeyCode.
38+ var propKey = ( KeyCode ) Enum . Parse ( typeof ( KeyCode ) , propEnumName ) ;
39+
40+ string keyName = Enum . GetName ( typeof ( KeyCode ) , propKey ) ;
41+
42+ // Offset the button from the label.
43+ position . x += 120f ;
44+ position . width = 80f ;
45+
46+ // Display the button that activates the selection window.
47+ if ( EditorGUI . DropdownButton ( position , new GUIContent ( keyName ) , FocusType . Keyboard ) ) {
48+ selectKey ( ) ;
49+ }
50+
51+ // Apply changes if necessary, this way, the default _selectedKey value
52+ // does not incorrectly override the property enum value.
53+ /*if (_bChangeKey) {
54+
55+ // Since we cannot set the enum value of the serialized property directly,
56+ // we need to get the enumValueIndex associated to the KeyCode name.
57+ //
58+ // To do this, we convert the selected key code to its string name and
59+ // then find its index position in SerializedProperty.enumNames (variable string[] enums)
60+ string _selectedKeyName = Enum.GetName(typeof(KeyCode), _selectedKey);
61+
62+ int index = 0;
63+
64+ // Find the index of the selected key name.
65+ foreach (string enumName in enums) {
66+
67+ if (enumName == _selectedKeyName) {
68+ break;
69+ }
70+
71+ index++;
72+ }
73+
74+ // Set the index which in turn sets the correct key code enum.
75+ property.enumValueIndex = index;
76+
77+ _bChangeKey = false;
78+ return;
79+ }*/
80+
81+ EditorGUI . EndProperty ( ) ;
82+ }
83+ public void Test ( ) {
84+ }
85+
86+ // Display a menu to select key codes.
87+ private void selectKey ( )
88+ {
89+ // Get all the keycodes
90+ var keyCodes = Enum . GetValues ( typeof ( KeyCode ) ) ;
91+ var keys = new KeyCode [ keyCodes . Length ] ;
92+ var drawer = new DrawerOption [ keyCodes . Length ] ;
93+
94+
95+
96+ // Set all the keycode values in the array in order to feed it into the selection window.
97+ int i = 0 ;
98+ foreach ( KeyCode k in keyCodes ) {
99+ drawer [ i ] = new DrawerOption < KeyCode > ( ( ) => keys , k . ToString ( ) , key => {
100+ //_selectedKey = key;
101+ p . enumValueIndex = 4 ; } , p , k ) ;
102+ keys [ i ++ ] = k ;
103+ }
104+
105+ DrawerMenu . ShowWindow ( drawer ) ;
106+
107+ // Display the selection window to pick a keycode.
108+ /*SelectionWindow.Show(new Tab<KeyCode>(
109+
110+ getValues: () => keys,
111+ getCurrent: () => _selectedKey,
112+ setTarget: key => { _selectedKey = key; },
113+
114+ getValueName: key => Enum.GetName(typeof(KeyCode), key),
115+ title: "Keys"
116+ ));*/
117+ }
118+ }
0 commit comments