Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by SP0KK0 · Aug 25, 2013 at 04:45 PM · editorwindowcheckopen

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");
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SP0KK0 · Aug 25, 2013 at 06:27 PM 0
Share

Thanks number$$anonymous$$runcher, works perfectly

avatar image dmd · Sep 23, 2014 at 03:58 PM 0
Share

Good solution, but, it seems, would not work between Play / Stop - because all static data in classes will be destroyed.

avatar image
3

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Oct 04, 2020 at 07:01 PM 0
Share

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

avatar image Yecats Bunny83 · Oct 04, 2020 at 07:36 PM 0
Share

Thanks for confir$$anonymous$$g the editor version, I should have looked for that when posting my answer. I've updated it!

avatar image
1

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges