Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

How does sceneClosing callback work?

Discussion in 'Scripting' started by dogzerx2, Feb 16, 2018.

  1. dogzerx2

    dogzerx2

    Joined:
    Dec 27, 2009
    Posts:
    3,804
    This is what I'm doing right now:

    Code (csharp):
    1.  
    2. [ExecuteInEditMode]
    3. public class SomeScript : MonoBehaviour {
    4.    void Start ()
    5.    {
    6.       UnityEditor.SceneManagement.EditorSceneManager.sceneClosing += SceneClosing;
    7.    }
    8.  
    9.    void SceneClosing(Scene scene, bool removingScene)
    10.    {
    11.       Debug.Log("It's working");
    12.    }
    13. }
    14.  
    I would expect when scene is closed, I would see "It's working" on the console. But I get nothing.
     
  2. Peter77

    Peter77

    Joined:
    Jun 12, 2013
    Posts:
    4,114
    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:
    https://issuetracker.unity3d.com/is...-are-not-called-when-switching-between-scenes

    Code (CSharp):
    1. [InitializeOnLoad]
    2. class SceneClosingCallbackTest
    3. {
    4.     static SceneClosingCallbackTest()
    5.     {
    6.         UnityEditor.SceneManagement.EditorSceneManager.sceneClosing += SceneClosing;
    7.         UnityEditor.SceneManagement.EditorSceneManager.sceneClosed += SceneClosed;
    8.         UnityEditor.SceneManagement.EditorSceneManager.sceneOpening += SceneOpening;
    9.         UnityEditor.SceneManagement.EditorSceneManager.sceneOpened += SceneOpened;
    10.         UnityEditor.SceneManagement.EditorSceneManager.newSceneCreated += NewSceneCreated;
    11.     }
    12.  
    13.     static void SceneClosing(UnityEngine.SceneManagement.Scene scene, bool removingScene)
    14.     {
    15.         Debug.Log("SceneClosing");
    16.     }
    17.  
    18.     static void SceneClosed(UnityEngine.SceneManagement.Scene scene)
    19.     {
    20.         Debug.Log("SceneClosed");
    21.     }
    22.  
    23.     static void SceneOpening(string path, UnityEditor.SceneManagement.OpenSceneMode mode)
    24.     {
    25.         Debug.Log("SceneOpening");
    26.     }
    27.  
    28.     static void SceneOpened(UnityEngine.SceneManagement.Scene scene, UnityEditor.SceneManagement.OpenSceneMode mode)
    29.     {
    30.         Debug.Log("SceneOpened");
    31.     }
    32.  
    33.     static void NewSceneCreated(UnityEngine.SceneManagement.Scene scene, UnityEditor.SceneManagement.NewSceneSetup setup, UnityEditor.SceneManagement.NewSceneMode mode)
    34.     {
    35.         Debug.Log("NewSceneCreated");
    36.     }
    37. }
     
    Last edited: Feb 3, 2019
    Bill-Sansky and dogzerx2 like this.
  3. dogzerx2

    dogzerx2

    Joined:
    Dec 27, 2009
    Posts:
    3,804
    Hey! Thank you so much.
     
    Peter77 likes this.
  4. robochase

    robochase

    Joined:
    Mar 1, 2014
    Posts:
    213
    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.
     
  5. Peter77

    Peter77

    Joined:
    Jun 12, 2013
    Posts:
    4,114
  6. Develax

    Develax

    Joined:
    Nov 14, 2017
    Posts:
    7
    Guys, vote for this issue, please, or it will never be fixed.