SceneView.onSceneGUIDelegate += view =>

I’m using this kind of architecture to create hotkeys for when I’m in the scene view, it works really well but I would like these hotkeys to also work when I’m in the hierarchy window, how can I do that, is there an easy way from where I am?

[InitializeOnLoad]
public static class EditorHotkeysTracker
    {
        static EditorHotkeysTracker()
        {
            SceneView.onSceneGUIDelegate += view =>
            {
                var e = Event.current;
                if (e.type == EventType.KeyDown)
                {
                    if (e.keyCode == KeyCode.G)
                    {
                        AddSelection();
                        Flip(e.shift);
                    }
                    if (e.keyCode == KeyCode.L)
                    {
                        AddSelection();
                        CreateParent();
                    }
                    if (e.keyCode == KeyCode.P)
                    {
                        AddSelection();
                        ParentInheritsPositionValue();
                    }
                    if (e.keyCode == KeyCode.R)
                        if(e.shift)
                          RandomizerIsOn = !RandomizerIsOn;

That’s what I’m using as code but I don’t really know about delegates or lambdas yet so I don’t understand this line “SceneView.onSceneGUIDelegate += view =>” I’ll learn it soon but right now I just want to get this to work.

Thanks for helping me out!

1 Like

The way this is usually done is via menu items, they can be easily be given keyboard shortcuts.

Okay I’ll try your way instead. Thanks

I’ve done it like you suggested but some things did not work as predicted. For example:

[MenuItem("Hotkeys/Unhide All #h")]
        static void UnhideSceneObjects()
        {
            UnhideAll();
        }

This code makes it so that I cannot write ‘H’ for things like renaming a folder. Do you have a solution for this? Maybe it has to detect that I’m on the scene view but I don’t know how to code that.

I’ve disabled some of my buttons like they mention in the documents, with return Selection.activeTransform != null;. However some of my hotkeys do not require a transform to be active, like the UnhideAll() shown here.

Well actually, bummer, it seems that even if they’re greyed out I still cannot use any of the keyboard shortcut to rename anything. Imgur: The magic of the Internet

Wow, that is pretty weird and unexpected behavior from Unity :eyes:. I guess that route will only work for you then if you can use more complex key combinations for your actions, containing at least ctrl as a modifier.

Okay, let’s try this again… I think you can use EditorApplication.hierarchyWindowItemOnGUI to achieve what you’re after.

This should do it:

using UnityEngine;
using UnityEditor;

[InitializeOnLoad]
public static class EditorHotkeys
{
    static EditorHotkeys()
    {
        EditorApplication.hierarchyWindowItemOnGUI += DetectInput;
    }

    static void DetectInput(int instanceID, Rect selectionRect)
    {
        if(instanceID == Selection.activeInstanceID)
        {
            var e = Event.current;
            if(e.type == EventType.KeyDown)
            {
                if(e.keyCode == KeyCode.G)
                {
                    Debug.Log("G pressed");
                }
            }
        }
    }
}
2 Likes

Hehe ok, I’ll try again with that method!
Too bad though, I was really liking it when I could see the hotkeys under the MenuItems.

So here’s what I did, I copy pasted everything I had in the OnSceneGuiDelegate into the Detect input method you provided.
And it all works now, but with some qwirks, when I’m moving the sprites with my hotkeys, they move much much faster when I selected them in the window editor. I think it’s because
EditorApplication.hierarchyWindowItemOnGUI gets called more frequently than OnSceneGUIDelegate.

I checked around for the MenuItem hotkeys and it’s a known issue but somehow Unity doesn’t want to deal with it.

To get rid of that quirk you need to make your movement logic be time relative instead of moving things a fixed amount on every call.

static float timeLast;

static void DetectInput()
{
    float timeDelta = EditorApplication.timeSinceStartup - timeLast;
    timeLast = EditorApplication.timeSinceStartup;
    ...
    float moveAmount = 10 * timeDelta;
    ...

Thanks a bunch for all your help!

Just realized that EditorApplication.hierarchyWindowItemOnGUI += DetectInput; is also being detected when renaming in the hierarchy ^^. Tough luck. If you’ve got any idea around the issue, as always I’m all ears!

Actually, in my testing I did not detect any KeyDown events in EditorApplication.hierarchyWindowItemOnGUI when renaming GameObjects in the hierarchy view, and I could rename them without any problems. Are you sure you don’t still have the old menu item shortcuts in place, blocking renaming?

There’s also EditorGUIUtility.editingTextField which I think you could use to igore KeyDown events when renaming GameObjects, but again, I don’t think this should be necessary in your case.

I’ve been looking thoroughly through my code and for me without EditorGUIUtility.editingTextField it detects input at all time in the hierarchy. I don’t think I’m missing something and I removed all hotkeys from the MenuItems yes.

The only thing I did now is add in

static void DetectInput(int instanceID, Rect selectionRect)
{
if (EditorGUIUtility.editingTextField) return;
}

Isn’t that proof that it does detect even when renaming?

Thanks for your help, I think all has been fixed! :wink:

1 Like

No problem, glad I could help :slight_smile:

Not sure why we had different results with input detection when renaming; maybe it works differently in different versions of Unity or something. Oh well :smile: