Search Unity

  1. We've closed the job boards. If you're looking for work, or looking to hire check out Unity Connect. You can see more information here.
    Dismiss Notice
  2. We're running great holiday deals on subscriptions, swag and Asset Store packages! Take a peek at this blog for more information!
    Dismiss Notice
  3. Check out our Unite Austin 2017 YouTube playlist to catch up on what you missed. More videos coming soon.
    Dismiss Notice
  4. Unity 2017.2 is now released.
    Dismiss Notice
  5. The Unity Gear Store is here to help you look great at your next meetup, user group or conference. With all new Unity apparel, stickers and more!
    Dismiss Notice
  6. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  7. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice
  8. Unity 2017.3 beta is now available for download.
    Dismiss Notice

EditorWindow won't destroy

Discussion in 'Extensions & OnGUI' started by FeastSC2, Jun 19, 2017.

  1. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    235
    I have a custom EditorWindow and when I close it, it calls the OnDestroy() method.

    So I assume it's been correctly deleted...
    However each time I create the window again with GetWindow it runs all the methods twice.
    And if I delete that 2nd EditorWindow, and create a new one, all the methods are running 3 times!

    It's like the EditorWindow is not being deleted somehow! What can I do to make this stop?

    EDIT: Oh nevermind I think it's something related to my OnEnable function. Someone handed me this code but I don't really understand it, I think it's the root of the problem.
    Code (CSharp):
    1. private void OnEnable()
    2.         {
    3.             if (SceneView.onSceneGUIDelegate != null) SceneView.onSceneGUIDelegate -= OnScene;
    4.             SceneView.onSceneGUIDelegate += OnScene;
    5.         }
     
    Last edited: Jun 19, 2017
  2. shawn

    shawn

    Unity Technologies

    Joined:
    Aug 4, 2007
    Posts:
    531
    Yeah, that code snippet isn't going to really work. Short answer is that your native representation of the EditorWindow is being destroyed (which calls OnDestroy), but the C# representation isn't because there's a reference being kept with this event. This code snippet is attempting but failing to remove the reference, because the OnScene this snippet is referring to is on a newer object than the original.

    This is the pattern for registering for the onSceneGUIDelegate event we use internally:
    Code (CSharp):
    1. private void OnEnable ()
    2. {
    3.     SceneView.onSceneGUIDelegate += OnScene;
    4. }
    5.  
    6. private void OnDisable ()
    7. {
    8.     SceneView.onSceneGUIDelegate -= OnScene;
    9. }
     
    FeastSC2 likes this.
  3. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    235
    Thank you ;) It works