- Home /
Find out if an Editor window is open
Hey
Is there any way to find out if an editor window is open through script. So something like this:
if (!EditorWindow.IsOpen(Type)) {
Debug.Log ("This window is not open");
}
Answer by numberkruncher · Aug 25, 2013 at 05:09 PM
If you can only ever have one window open at any given time, the following works quite well:
using UnityEngine;
using UnityEditor;
public class YourEditorWindow : EditorWindow {
public static YourEditorWindow Instance { get; private set; }
public static bool IsOpen {
get { return Instance != null; }
}
// Not necessary, but tidy I think :)
public static void ShowWindow() {
GetWindow<YourEditorWindow>("Your Window");
}
void OnEnable() {
Instance = this;
}
}
Then usage example:
Debug.Log(YourEditorWindow.IsOpen); // false
YourEditorWindow.ShowWindow();
// Sometime when your window is shown...
Debug.Log(YourEditorWindow.IsOpen); // true
Though, if you are just trying to avoid showing multiple instances of the same window, there is no need to worry since the default behaviour of GetWindow
is to focus window if it is already open.
Good solution, but, it seems, would not work between Play / Stop - because all static data in classes will be destroyed.
Answer by Yecats · Oct 04, 2020 at 05:07 PM
For anyone else looking to solve this - you can use the built in API as of 2019.3:
if (EditorWindow.HasOpenInstances<YourWindowType>())
{
// do stuff
}
Read more here.
Edit:
Added editor version - Thank you to @Bunny83 for providing the information.
I just like to add that I have several versions of the UnityEditor dll in ILSpy. I just searched for this method and it looks like the first Unity version I used that has this method is "2019.3.0f6"
I just looked through the release notes and yes, it was added in 2019.3
Thanks for confir$$anonymous$$g the editor version, I should have looked for that when posting my answer. I've updated it!
Answer by Mark Currie · Sep 30, 2014 at 02:09 PM
As dmd mentioned, Play/Stop will set static member references to null. So another way would be to do something like this, it's not so fast, but you could optimize it by combining this with the static member approach, and updating your static member when there's an editor state change.
static void FindEditorWindow<WindowType>() where WindowType : EditorWindow
{
WindowType[] windows = Resources.FindObjectsOfTypeAll<WindowType>();
if(windows != null && windows.Length > 0)
{
// do something...
}
}
I've noticed in Unity 4.5.4 (and early versions), it's possible to leak editor windows, like if you create an EditorWindow and you don't call Show() and your press Play/Stop. I think in cases like that, sometimes the object doesn't get cleaned up like you'd expect, so basically you have a leak. When this happens you can destroy the EditorWindow in code, or you can do it by pressing "Revert Factory Settings" on the layout menu. So it's possible that the list of windows returned by FindObjectsOfTypeAll() will be greater than one.
Your answer
Follow this Question
Related Questions
Open new editor window from editor class 2 Answers
Unity 5 Bug on Open/New Project Window 1 Answer
Multiple editor windows combined 0 Answers
Editor GUI error 0 Answers
How do I repaint/refresh/focus without calling Key Events multiple times? 1 Answer