Last active
July 2, 2019 03:31
-
-
Save vertxxyz/8f0f73251cfad898407ceff3a2a2a432 to your computer and use it in GitHub Desktop.
Better KeyCode Property Drawer - Select and hit the key you wish to use instead of scrolling an endless enum field.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
public class KeyCodeAttribute : PropertyAttribute | |
{ | |
public KeyCodeAttribute() { } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEditor; | |
using UnityEngine; | |
namespace Alchemy | |
{ | |
[CustomPropertyDrawer(typeof(KeyCodeAttribute))] | |
public class KeyCodeAttributeDrawer : PropertyDrawer | |
{ | |
private GUIStyle objectFieldStyle; | |
private GUIStyle ObjectFieldStyle => objectFieldStyle ?? (objectFieldStyle = "IN ObjectField"); | |
const float widthInput = 18; | |
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) | |
{ | |
//KeyCodeAttribute a = attribute as KeyCodeAttribute; | |
position = EditorGUI.PrefixLabel(position, new GUIContent(property.displayName)); | |
position.width -= widthInput; | |
Rect pickerRect = new Rect(position.x + position.width, position.y, widthInput, position.height); | |
int id = GUIUtility.GetControlID((int) pickerRect.x, FocusType.Keyboard, pickerRect); | |
GUI.color = GUIUtility.keyboardControl == id ? Color.green : Color.white; | |
EditorGUI.PropertyField(position, property, GUIContent.none); | |
position.x += position.width; | |
position.width = widthInput; | |
if (Event.current.type == EventType.MouseDown && position.Contains(Event.current.mousePosition)) | |
{ | |
GUIUtility.keyboardControl = id; | |
Event.current.Use(); | |
} | |
position.y -= 2; | |
position.x += 1; | |
GUI.Label(position, GUIContent.none, ObjectFieldStyle); | |
if (GUIUtility.keyboardControl == id && Event.current.type == EventType.KeyUp) | |
{ | |
if (Event.current.keyCode != KeyCode.Escape) | |
property.enumValueIndex = KeyCodeToEnumIndex(property, Event.current.keyCode); | |
Event.current.Use(); | |
GUIUtility.keyboardControl = -1; | |
} | |
else if (GUIUtility.keyboardControl == id && Event.current.isKey) | |
Event.current.Use(); | |
GUI.color = Color.white; | |
} | |
private static int KeyCodeToEnumIndex(SerializedProperty keyCodeProperty, KeyCode keyCode) | |
{ | |
string[] keyCodeNames = keyCodeProperty.enumNames; | |
string query = keyCode.ToString(); | |
for (int i = 0; i < keyCodeNames.Length; i++) | |
{ | |
if (keyCodeNames[i].Equals(query)) | |
return i; | |
} | |
return 0; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment