Search Unity

How does sceneClosing callback work?

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

  1. dogzerx2

    dogzerx2

    Joined:
    Dec 27, 2009
    Posts:
    3,971
    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

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,618
    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
  3. dogzerx2

    dogzerx2

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

    robochase

    Joined:
    Mar 1, 2014
    Posts:
    244
    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

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,618
  6. Develax

    Develax

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

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    the only 2 that work in 2019.2 are SceneOpening and SceneOpened :(
     
    SolidAlloy likes this.
  8. SugoiDev

    SugoiDev

    Joined:
    Mar 27, 2013
    Posts:
    395
    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)
    screenshot 2020.02.09-14.02.08.png
     
    Last edited: Feb 9, 2020
    Ghat-Smith and ModLunar like this.