Search Unity

Handling input in Editor Game View

Discussion in 'Scripting' started by Yerendi, Mar 16, 2019.

  1. Yerendi

    Yerendi

    Joined:
    Mar 11, 2016
    Posts:
    18
    Hi, I've faced a strange problem. I'm using this code to handle any editor input:

    Code (CSharp):
    1.  
    2. [InitializeOnLoadMethod]
    3.     static void EditorInit ()
    4.     {
    5.         System.Reflection.FieldInfo info = typeof (EditorApplication).GetField ("globalEventHandler", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic);
    6.         EditorApplication.CallbackFunction value = (EditorApplication.CallbackFunction)info.GetValue (null);
    7.         value += EditorGlobalKeyPress;
    8.         info.SetValue (null, value);
    9.     }
    10.     static void EditorGlobalKeyPress ()
    11.     {
    12.         Debug.Log ("KEY CHANGE " + Event.current.keyCode);
    13.     }
    14.  
    and it works perfectly on every editor window except Game (when Game window is "last clicked" it does not show debug, debugger not hitting breakpoint too). Anyone has idea why?
     
  2. Yerendi

    Yerendi

    Joined:
    Mar 11, 2016
    Posts:
    18
    If someone needs - I've found a workaround.

    Code (CSharp):
    1.  
    2. System.Reflection.FieldInfo info = typeof(Event).GetField("s_MasterEvent", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic);
    3. Event value = (Event)info.GetValue(null);
    4.  
    5. if(value != null)
    6. {
    7.       Debug.Log(value.keyCode);
    8. }
    9.  
    It returns events from ALL windows. It has 0ms overhead so performance is good.
     
    dimmduh1 likes this.
  3. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,620
    Can I ask for what you need this for? I've used Event.current for my editor extensions so far, which worked.
     
  4. Yerendi

    Yerendi

    Joined:
    Mar 11, 2016
    Posts:
    18
    I have editor script running in background (in [InitializeOnLoadMethod] I subscribe EditorApplication.update). In EditorApplication.update Event.current is always null. I have no editor window for my extension because I need it to run on every active editor tab so I can't use OnGUI where events are processed.