2
\$\begingroup\$

Documentation says that EditorWindow.Show show the EditorWindow.

I have two classes:

public class WindowOne : EditorWindow {

    public static WindowOne windowOneInstance;

    [MenuItem("Window/Open Window One")]
    public static void Init() {
        windowOneInstance = GetWindow<WindowOne>(false, "Window One", true);
    }
}

and

public class WindowTwo : EditorWindow {

    public static WindowTwo windowTwoInstance;
    public static WindowOne windowOneInstance;

    [MenuItem("Window/Open Window Two")]
    public static void Init() {
        windowTwoInstance = GetWindow<WindowTwo>(false, "Window Two", true);
        windowOneInstance = WindowOne.windowOneInstance;
    }

    public void OnGUI() {
        if (GUI.Button(new Rect(0, 0, 200, 50), new GUIContent("show window one"))) {
            windowOneInstance.Show();
        }
    }
}

In menu I click Open Window One, then Open Window Two, then I close the first window

enter image description here

Now if I click a Button on WindowTwo it doesn't show me WindowOne. Or at least it shows one-pixel of window that I cannot move or resize or do something else.

enter image description here

If I use GetWindow(typeof (WindowOne)); instead of .Show() - it shows the window, but I'm not sure that it shows the same instance I created before, because instanceID of that window will be different.


Why Show method works so strange? Am I doing something wrong? And what should I do to show the same instance of closed (temporarily hidden) window?

Unity version - 5.6.0p4

\$\endgroup\$
  • \$\begingroup\$ Is there a particular reason you want your WindowOnes to be the same instance? \$\endgroup\$ – S. Tarık Çetin Jun 29 '17 at 16:48
  • \$\begingroup\$ @S.TarıkÇetin yes. I create a window - a toolbox for another window that can be hidden/shown in any time \$\endgroup\$ – Alexey Shimansky Jun 29 '17 at 17:03
2
\$\begingroup\$

See here:

OnDestroy is called when the EditorWindow is closed.

It seems like Unity destroys the instance of the editor window when you close it.

After the instance is destroyed, when you call the Show() method, it fails to find the instance so it probably falls down to a default window, which is what you are referring to as one-pixel of window.

But as you can see here in the documentation of GetWindow() method:

Returns the first EditorWindow of type t which is currently on the screen.

If there is none, creates and shows new window and returns the instance of it.

GetWindow() method creates a new instance for you if there is no instance available. So you are getting a fresh instance when you call GetWindow() after closing the existing instance.


About your concerns on the windows being the same instance:

If your purpose is the persistence of data between editor windows, you can achieve so using serialization or a data holder class.


Edit:

After some research, I have come to the conclusion that there is no way to "hide" an editor window rather than "closing" it.

There is no way to prevent an editor window from receiving input, so somehow making it invisible and allowing input to go through is not possible either.

\$\endgroup\$
  • \$\begingroup\$ It seems like Unity destroys the instance of the editor window when you close it - Strange. It seems so.. Then why Show method do not throw a null reference exception >_< ......is there a way just hide a window and show it any time? \$\endgroup\$ – Alexey Shimansky Jun 29 '17 at 17:02
  • \$\begingroup\$ It is strange to me to use Show method and do not use/find Hide but anyway I will be save my data OnDisable window and restore it OnEnable. Thanks. \$\endgroup\$ – Alexey Shimansky Jul 1 '17 at 8:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.