Is there a way to bring up some kind of selector for KeyCodes in an editor window in Unity 5?
I have tried searching for quite a while and was unable to find anything beyond the usualy Component public KeyCode selector, which doesn’t display in editor windows.
This is what I am doing:
public KeyCode KeyToUse;
Are you saying that does not work for you?
This is completely counter to my experience, dating back to Unity 5.2.2f1
I still have a few old games in Unity 5.6.6f2 and this works fine there too.
In fact, it works fine in an array of them as well, as seen in this code in Unity5.6.6f2, line 54:
This does provide a visual selector in components (normal scripts) but not in editor windows.
I found a workaround by using an enum popup cast as a KeyCode.
someKeyCode = (KeyCode)EditorGUILayout.EnumPopup("Some Key:", someKeyCode);
It’s not perfect but better than nothing.
Ah, I didn’t grasp this distinction in your original post. Gotcha.
Pasting your handy workaround into my gist pile for Unity… thanks!
Best of all, your snippet above works flawlessly, even in more recent Unity:
Yep, pretty sure there’s some better way of doing it, maybe something undocumented in the editor, but this seems to work for my purposes.
I just wish the editor had some built-in drawer for creating one-click KeyCodes (click a button, then the key you want on the keyboard) but the list also works, just slower.
yeah… now THAT would be a seriously nice thing…
Extremely early christmas present!
Found this on a 2-year old Unity forum, did a small mod to it to make it clearer when it is active.
I don’t think I’ve ever been this happy to find a piece of code. I guess that’s my reward for staying up past 2am searching.
public static KeyCode KeyCodeField(Rect controlRect, KeyCode keyCode)
{
int controlID = GUIUtility.GetControlID(FocusType.Keyboard);
KeyCode retVal = keyCode;
Event evt = Event.current;
switch (evt.GetTypeForControl(controlID))
{
case EventType.Repaint:
{
GUIStyle style = GUI.skin.GetStyle("TextField");
//GUIStyle style = EditorStyles.toolbar;
if (style == GUIStyle.none)
{
//Debug.Log("GUI Style not found");
}
if (GUIUtility.keyboardControl == controlID)
{
style.Draw(controlRect, new GUIContent("Press a key"), controlID);
}
else
{
style.Draw(controlRect, new GUIContent(keyCode.ToString()), controlID);
}
break;
}
case EventType.MouseDown:
{
if (controlRect.Contains(Event.current.mousePosition) && Event.current.button == 0 && GUIUtility.hotControl == 0)
{
//Debug.Log("mouse down event, control id: " + controlID + ", hot control: " + GUIUtility.hotControl);
GUIUtility.hotControl = controlID;
GUIUtility.keyboardControl = controlID;
evt.Use();
}
break;
}
case EventType.MouseUp:
{
if (GUIUtility.hotControl == controlID)
{
GUIUtility.hotControl = 0;
evt.Use();
}
break;
}
case EventType.KeyDown:
{
//Debug.Log("key down, control id: " + controlID + ", hot control: " + GUIUtility.hotControl);
if (GUIUtility.keyboardControl == controlID)
{
//Debug.Log("hotcontrol");
retVal = Event.current.keyCode;
GUIUtility.hotControl = 0;
GUIUtility.keyboardControl = 0;
evt.Use();
}
break;
}
case EventType.KeyUp:
{
break;
}
}
return retVal;
}
public static KeyCode KeyCodeFieldLayout(KeyCode keyCode)
{
return KeyCodeField(EditorGUILayout.GetControlRect(), keyCode);
}
Original can be found here
Dude!