Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved EditorWindow.GetWindow() takes away the focus from other controls

Discussion in 'Editor & General Support' started by Rowlan, Jul 29, 2021.

  1. Rowlan

    Rowlan

    Joined:
    Aug 4, 2016
    Posts:
    3,819
    I wanted to get the SceneView window dimensions in order to paint debug images on it. However there was a problem that as soon as my tool was active the transform input fields didn't work anymore, I couldn't select the cells, nor enter any value. Moreover alt+tab to Visual Studio didn't work anymore, it only worked as soon as Gizmos were deactivated. Basically in the Editor I did this:

    Code (CSharp):
    1. public void OnSceneGUI(){
    2.      float height = EditorWindow.GetWindow<SceneView>().position.height;
    3. }
    Tracking the source of the problem led me to this:

    Code (CSharp):
    1. // Returns the first EditorWindow of type /t/ which is currently on the screen.
    2. static EditorWindow GetWindowPrivate(System.Type t, bool utility, string title, bool focus)
    3. {
    4.     UnityEngine.Object[] wins = Resources.FindObjectsOfTypeAll(t);
    5.     EditorWindow win = wins.Length > 0 ? (EditorWindow)(wins[0]) : null;
    6.  
    7.     if (!win)
    8.     {
    9.         win = ScriptableObject.CreateInstance(t) as EditorWindow;
    10.         if (title != null)
    11.             win.titleContent = new GUIContent(title);
    12.         if (utility)
    13.             win.ShowUtility();
    14.         else
    15.             win.Show();
    16.     }
    17.     else if (focus)
    18.     {
    19.         win.Show();  // For some corner cases in saved layouts, the window can be in an unvisible state.
    20.                      // Since the caller asked for focus, it's fair to assume he wants it always to be visible. (case 586743)
    21.         win.Focus();
    22.     }
    23.  
    24.     return win;
    25. }
    26.  
    27. ...
    28.  
    29. public static T GetWindow<T>() where T : EditorWindow
    30. {
    31.    return GetWindow<T>(false, null, true);
    32. }
    33.  
    So the problem is that when you invoke GetWindow() by default the focus is also set on that window and taken away from any other control. If you do that in OnSceneGUI() this happens of course over and over and the Editor becomes unusable.

    This post is merely a help, I thought I'd share in case anyone else runs into the same problem with similar symptoms. I'd have expected that the window would explicitly have to be given focus and shown, but it's the way it is.
     
    SorraTheOrc likes this.
  2. SorraTheOrc

    SorraTheOrc

    Joined:
    Jun 11, 2016
    Posts:
    197
    Thank you, I just started debugging this, you saved me quite some time there.
     
    Rowlan likes this.