How does sceneClosing callback work?

This is what I’m doing right now:

[ExecuteInEditMode]
public class SomeScript : MonoBehaviour {
   void Start () 
   {
      UnityEditor.SceneManagement.EditorSceneManager.sceneClosing += SceneClosing;
   }

   void SceneClosing(Scene scene, bool removingScene) 
   {
      Debug.Log("It's working");
   }
}

I would expect when scene is closed, I would see “It’s working” on the console. But I get nothing.

I just tested the following code and all events do work, except sceneClosing and sceneClosed. I guess it’s a bug in the editor, I tested 2017.3.0p2.

EDIT: I was able to reproduce this issue with 2018.1.0b7 as well and submitted a bug-report:

[InitializeOnLoad]
class SceneClosingCallbackTest
{
    static SceneClosingCallbackTest()
    {
        UnityEditor.SceneManagement.EditorSceneManager.sceneClosing += SceneClosing;
        UnityEditor.SceneManagement.EditorSceneManager.sceneClosed += SceneClosed;
        UnityEditor.SceneManagement.EditorSceneManager.sceneOpening += SceneOpening;
        UnityEditor.SceneManagement.EditorSceneManager.sceneOpened += SceneOpened;
        UnityEditor.SceneManagement.EditorSceneManager.newSceneCreated += NewSceneCreated;
    }

    static void SceneClosing(UnityEngine.SceneManagement.Scene scene, bool removingScene)
    {
        Debug.Log("SceneClosing");
    }

    static void SceneClosed(UnityEngine.SceneManagement.Scene scene)
    {
        Debug.Log("SceneClosed");
    }

    static void SceneOpening(string path, UnityEditor.SceneManagement.OpenSceneMode mode)
    {
        Debug.Log("SceneOpening");
    }

    static void SceneOpened(UnityEngine.SceneManagement.Scene scene, UnityEditor.SceneManagement.OpenSceneMode mode)
    {
        Debug.Log("SceneOpened");
    }

    static void NewSceneCreated(UnityEngine.SceneManagement.Scene scene, UnityEditor.SceneManagement.NewSceneSetup setup, UnityEditor.SceneManagement.NewSceneMode mode)
    {
        Debug.Log("NewSceneCreated");
    }
}
5 Likes

Hey! Thank you so much.

1 Like

Did this get fixed? Having a little trouble digging up @Peter77 's ticket. i’m on 2018.3.2f1 and still not seeing it firing.

I suspect this probably works if you call UnityEditor.SceneManagement.EditorSceneManager.CloseScene, but that seems to require multiple scenes to be loaded. This is conjecture on my part based on the warning unity gives when calling CloseScene on the active scene.

I really just want a hook when the current scene is about to close so i can clean up some garbage monobehaviours before it’s saved.

Nope, it’s not marked as “Fixed” yet:
https://issuetracker.unity3d.com/issues/editorscenemanager-sceneclosing-and-sceneclosed-callbacks-are-not-called-when-switching-between-scenes

Guys, vote for this issue, please, or it will never be fixed.

the only 2 that work in 2019.2 are SceneOpening and SceneOpened :frowning:

1 Like

Was this issue also affecting 2020.1?
Just tested on 2020.1.0a22 and it seems to be working.

Edit: If you use “discard changes” in a scene, it seems to call the close/closing twice.
The seconds call will have invalid scenes (can be seem below: “scene.name” is empty)
5460111--557868--screenshot 2020.02.09-14.02.08.png

2 Likes