Search Unity

SceneView.onSceneGUIDelegate += view =>

Discussion in 'Immediate Mode GUI (IMGUI)' started by FeastSC2, Apr 12, 2017.

  1. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
    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?

    Code (CSharp):
    1. [InitializeOnLoad]
    2. public static class EditorHotkeysTracker
    3.     {
    4.         static EditorHotkeysTracker()
    5.         {
    6.             SceneView.onSceneGUIDelegate += view =>
    7.             {
    8.                 var e = Event.current;
    9.                 if (e.type == EventType.KeyDown)
    10.                 {
    11.                     if (e.keyCode == KeyCode.G)
    12.                     {
    13.                         AddSelection();
    14.                         Flip(e.shift);
    15.                     }
    16.                     if (e.keyCode == KeyCode.L)
    17.                     {
    18.                         AddSelection();
    19.                         CreateParent();
    20.                     }
    21.                     if (e.keyCode == KeyCode.P)
    22.                     {
    23.                         AddSelection();
    24.                         ParentInheritsPositionValue();
    25.                     }
    26.                     if (e.keyCode == KeyCode.R)
    27.                         if(e.shift)
    28.                           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!
     
    dval likes this.
  2. sisus_co

    sisus_co

    Joined:
    May 1, 2011
    Posts:
    14
  3. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
    Okay I'll try your way instead. Thanks
     
  4. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
    I've done it like you suggested but some things did not work as predicted. For example:


    Code (CSharp):
    1.  
    2. [MenuItem("Hotkeys/Unhide All #h")]
    3.         static void UnhideSceneObjects()
    4.         {
    5.             UnhideAll();
    6.         }
    7.  
    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.
     
    Last edited: Apr 13, 2017
  5. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
    Well actually, bummer, it seems that even if they're greyed out I still cannot use any of the keyboard shortcut to rename anything. http://i.imgur.com/EG9DH9w.png
     
  6. sisus_co

    sisus_co

    Joined:
    May 1, 2011
    Posts:
    14
    Wow, that is pretty weird and unexpected behavior from Unity o_O. 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.
     
  7. sisus_co

    sisus_co

    Joined:
    May 1, 2011
    Posts:
    14
    Okay, let's try this again... I think you can use EditorApplication.hierarchyWindowItemOnGUI to achieve what you're after.

    This should do it:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3.  
    4. [InitializeOnLoad]
    5. public static class EditorHotkeys
    6. {
    7.     static EditorHotkeys()
    8.     {
    9.         EditorApplication.hierarchyWindowItemOnGUI += DetectInput;
    10.     }
    11.  
    12.     static void DetectInput(int instanceID, Rect selectionRect)
    13.     {
    14.         if(instanceID == Selection.activeInstanceID)
    15.         {
    16.             var e = Event.current;
    17.             if(e.type == EventType.KeyDown)
    18.             {
    19.                 if(e.keyCode == KeyCode.G)
    20.                 {
    21.                     Debug.Log("G pressed");
    22.                 }
    23.             }
    24.         }
    25.     }
    26. }
     
    sandolkakos and dval like this.
  8. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
    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.
     
  9. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
    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.
    https://issuetracker.unity3d.com/issues/custom-menuitem-hotkey-takes-priority-over-input-field
     
    Last edited: Apr 14, 2017
  10. sisus_co

    sisus_co

    Joined:
    May 1, 2011
    Posts:
    14
    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.

    Code (CSharp):
    1. static float timeLast;
    2.  
    3. static void DetectInput()
    4. {
    5.     float timeDelta = EditorApplication.timeSinceStartup - timeLast;
    6.     timeLast = EditorApplication.timeSinceStartup;
    7.     ...
    8.     float moveAmount = 10 * timeDelta;
    9.     ...
     
  11. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
    Thanks a bunch for all your help!
     
  12. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
    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!
     
  13. sisus_co

    sisus_co

    Joined:
    May 1, 2011
    Posts:
    14
    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.
     
  14. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
    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
    Code (CSharp):
    1. static void DetectInput(int instanceID, Rect selectionRect)
    2. {
    3. if (EditorGUIUtility.editingTextField) return;
    4. }
    Isn't that proof that it does detect even when renaming?

    Thanks for your help, I think all has been fixed! ;-)
     
    dval likes this.
  15. sisus_co

    sisus_co

    Joined:
    May 1, 2011
    Posts:
    14
    No problem, glad I could help :)

    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 :D