Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
5
Question by DerWoDaSo · Feb 21, 2013 at 05:59 PM · editoreditorwindow

Find instance of EditorWindow without creating new one?

Hey everybody!

I need to Repaint a specific custom EditorWindow, triggered by changes in a custom inspector. For this I need to find any existing instance of this window.

EditorWindow.FindObjectOfType() does not give me any results at all here...

My current workaround is to create a static pseudo singleton reference to the last EditorWindow, but this is lost once Unity is restarted and not a very elegant way.

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

2 Replies

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

Answer by Bunny83 · Feb 22, 2013 at 09:20 AM

GetWindow actually does what you want. It ensures that only one instance is open. When you call GetWindow and the window is already created it reuses the existing window. So just use GetWindow() whenever you need to open the window.

If you want a singleton-like property, just do:

     public static MyEditorWindow Instance
     {
         get { return GetWindow< MyEditorWindow >(); }
     }

If you really just want to find all instances that are open use Resources.FindObjectsOfTypeAll. As mentioned in the docs, be careful since this function can return also internal stuff like the SceneView camera, ...

edit
I've finally made a base class for MonoBehaviour singletons a couple of days ago. I just made another one for EditorWindow ;)

 // Runtime Extension class
 public class MonoBehaviourSingleton< TSelfType > : MonoBehaviour where TSelfType : MonoBehaviour
 {
     private static TSelfType m_Instance = null;
     public static TSelfType Instance
     {
         get
         {
             if (m_Instance == null)
             {
                 m_Instance = (TSelfType)FindObjectOfType(typeof(TSelfType));
                 if (m_Instance == null)
                     m_Instance = (new GameObject(typeof(TSelfType).Name)).AddComponent<TSelfType>();
                 DontDestroyOnLoad(m_Instance.gameObject);
             }
             return m_Instance;
         }
     }
 }


 //Editor Extension class
 public class EditorWindowSingleton< TSelfType > : EditorWindow where TSelfType : EditorWindow
 {
     private static TSelfType m_Instance = null;
     public static TSelfType FindFirstInstance()
     {
         var windows = (TSelfType[])Resources.FindObjectsOfTypeAll(typeof(TSelfType));
         if (windows.Length == 0)
             return null;
         return windows[0];
     }
     
     public static TSelfType Instance
     {
         get
         {
             if (m_Instance == null)
             {
                 m_Instance = FindFirstInstance();
                 if (m_Instance == null)
                     m_Instance = GetWindow<TSelfType>();
             }
             return m_Instance;
         }
     }
 }

Just derive your class from the singleton base class and your ready to go. You have to pass the type itself as generic parameter. Something like that:

 public class MyEditorWindow : EditorWindowSingleton< MyEditorWindow >
 {
     //[...]
 }

Comment
Add comment · Show 4 · 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 DerWoDaSo · Feb 22, 2013 at 01:37 PM 0
Share

Thanks a lot! Resources.FindObjectsOfTypeAll() did the trick...

avatar image FatWednesday · May 08, 2014 at 04:44 PM 0
Share

Great little script to pop in projects if you're wanting singleton editor windows. Thanks for sharing.

avatar image rusoaica · Apr 18, 2018 at 09:23 PM 1
Share

Sorry to revive an old post, but just my small two cents. In order for this to work when using Build and Run, make sure to surround EditorWindowSingleton class between

 #if UNITY_EDITOR
     //your EditorWindowSingleton class here
 #endif

Otherwise you will get compilation errors about the namespace name 'UnityEditor' not being found. And it does make sense, since the editor library is not included when you export and run the game.

avatar image Zyl rusoaica · Apr 19, 2018 at 06:28 PM 1
Share

"Sorry to revive an old post"

Don't $$anonymous$$d it. It's always good to see a fellow sufferer, err, developer.

avatar image
2

Answer by numberkruncher · Feb 21, 2013 at 06:48 PM

 using UnityEngine;
 using UnityEditor;

 public class MyEditorWindow : EditorWindow {

     #region Window Management
     
     private static MyEditorWindow _instance;

     // And use the property like @Bunny83 suggested
     public static MyEditorWindow Instance {
         get { return GetWindow<MyEditorWindow>(); }
     }

     public static void RepaintWindow() {
         if (_instance != null)
             _instance.Repaint();
     }
     
     #endregion

     void OnEnable() {
         _instance = this;
     }

 }

Warning, I haven't tested the above code, but you should get the idea :-)

Comment
Add comment · Show 3 · 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 DerWoDaSo · Feb 22, 2013 at 08:57 AM 0
Share

Thank! But that's basically what I ment with "create a static pseudo singleton reference to the last EditorWindow". It works fine, but when reloading the script or restarting Unity while keeping the EditorWindow open, will break this reference.

avatar image numberkruncher · Feb 22, 2013 at 03:20 PM 0
Share

Another option is to initialize the static instance variable using the OnEnable message. I will update the above...

avatar image Zyl · Oct 20, 2016 at 02:46 PM 0
Share

Good solution. Works for me. Don't forget to set _instance back to null in OnDisable() though.

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

14 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How can i get the center of the entire editor window 0 Answers

Editor window script only works when window is open? 1 Answer

Using System.Reflection for this code? 1 Answer

OnSelectionChange Without EditorWindow 3 Answers


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