1. Help us improve the editor usability and artist workflows. Join our discussion to provide your feedback.
    Dismiss Notice
  2. We're looking for feedback on Unity Starter Kits! Let us know what you’d like.
    Dismiss Notice
  3. We’re giving 2017.1 beta testers a chance to win t-shirts and a Nintendo Switch. Read more on the blog.
    Dismiss Notice
  4. We want to know how you learned Unity! Help us by taking this quick survey and have a chance at a $25 gift card
    Dismiss Notice
  5. Are you an artist or level designer going to Unite Europe? Join our roundtables there to discuss artist features.
    Dismiss Notice
  6. Unity 5.6 is now released.
    Dismiss Notice
  7. Check out all the fixes for 5.6 on the patch releases page.
    Dismiss Notice

EditorWindow won't destroy

Discussion in 'Extensions & OnGUI' started by FeastSC2, Jun 19, 2017 at 9:17 PM.

  1. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    46
    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 at 9:26 PM
  2. shawn

    shawn

    Unity Technologies

    Joined:
    Aug 4, 2007
    Posts:
    529
    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:
    46
    Thank you ;) It works