Unity editor: Is it possible to create easily editable shortcuts?

2.7k Views Asked by At

I'm trying to create a custom editor script on Unity, and I'd like to have quite a few shortcuts for different reasons. Is there any way to create shortcuts that trigger an event in the Editor? Creating a custom editor just to edit the shortcuts of the first editor is impossible for me since I don't have that much time, but it would really help me to be able to change my own shortcuts easily.

As of now this is my code:

static void EditorGlobalKeyPress()
    {
        Event current = Event.current;
        if ((!current.alt && !current.control)|| current.type != EventType.KeyUp) return;
        switch (Event.current.keyCode)
        {
            case KeyCode.L:
                // Load stuff
                break;
            case KeyCode.U:
                // Unload stuff
                break;
            default:
                break;
        }
    }

And while it works fine, having editable shortcuts would be great. Does anyone know if it's possible in some way?

1

There are 1 best solutions below

0
On BEST ANSWER

Yes you can do this using a MenuItem

To create a hotkey you can use the following special characters:

  • % (ctrl on Windows and Linux, cmd on macOS),
  • ^ (ctrl on Windows, Linux, and macOS),
  • # (shift),
  • & (alt).

If no special modifier key combinations are required the key can be given after an underscore. For example to create a menu with hotkey shift-alt-g use "MyMenu/Do Something #&g". To create a menu with hotkey g and no key modifiers pressed use "MyMenu/Do Something _g".

Some special keyboard keys are supported as hotkeys, for example "#LEFT" would map to shift-left. The keys supported like this are: LEFT, RIGHT, UP, DOWN, F1 .. F12, HOME, END, PGUP, PGDN, INS, DEL, TAB, SPACE.

A hotkey text must be preceded with a space character ("MyMenu/Do_g" won't be interpreted as hotkey, while "MyMenu/Do _g" will).

For example

[MenuItem("Test/example %g")]
private static void TEEEEST()
{
    Debu.Log("Example!");
}

This also adds a global shortcut

enter image description here

Also from a UX perspective this is better so a user can not only do this via the shortcut but also via the menu. And you can look up why things are happening when you press the shortcut buttons (see below) ;)


And then any user can still freely edit and reassign them later via the Shortcut Manager (Edit -> Shortcuts...)

enter image description here

enter image description here