Search Unity

EditorWindow won't destroy

Discussion in 'Immediate Mode GUI (IMGUI)' started by FeastSC2, Jun 19, 2017.

  1. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
    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:
    552
    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:
    978
    Thank you ;) It works