Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

Bug SceneManagement.EditorSceneManager.sceneClosing

Discussion in '2019.3 Beta' started by Quatum1000, Aug 24, 2019.

  1. Quatum1000

    Quatum1000

    Joined:
    Oct 5, 2014
    Posts:
    889
    https://docs.unity3d.com/ScriptRefe....EditorSceneManager.SceneClosingCallback.html
    API: Callbacks of this type which have been added to the sceneClosing event are called just before a Scene is closed.

    When removing/unload a scene from the hierarchy, in my opinion, the scene should exist at this time when call:

    SceneManagement.EditorSceneManager.sceneClosing += PreSceneClosing;

    But unity comes up with a Error in
    PreSceneClosing(...)
    var gos = scene.GetRootGameObjects(); // Error: Scene does not exist!

    I think it's a bug that also occurs in final release 2019..2.2f1 as well.

    Test script can exist anywhere in the project. Load a scene and unload.

    Code (CSharp):
    1. #if UNITY_EDITOR
    2. using UnityEditor;
    3. using UnityEditor.SceneManagement;
    4. using UnityEngine;
    5.  
    6. /// <summary>
    7. /// Get a Notification if the unity/user starts to remove/save/unload a scene
    8. /// </summary>
    9. [InitializeOnLoad]
    10. public static class EditorSceneManagerNotification {
    11.  
    12.    static EditorSceneManagerNotification() {
    13.  
    14.        // https://docs.unity3d.com/ScriptReference/SceneManagement.EditorSceneManager-sceneClosing.html
    15.        EditorSceneManager.sceneClosing += PreSceneClosing;
    16.    }
    17.  
    18.    static void PreSceneClosing(UnityEngine.SceneManagement.Scene scene, bool removingScene) {
    19.        // What happen here: Before Scene Close: Raise Cube to y=10m for next loading
    20.  
    21.        // This event is called !!before!! closing an open Scene.
    22.        // It ! MUST ! be possibe to change gameobjects before unloading the scene.
    23.        
    24.        var gos = scene.GetRootGameObjects();  // Wrong Error: ArgumentException: The scene is not loaded.
    25.        foreach (var go in gos) {
    26.            Transform[] trss = go.GetComponents<Transform>();
    27.            foreach (var trs in trss) {
    28.                trs.gameObject.transform.position = new Vector3(0, 10, 0);
    29.            }
    30.        }
    31.        
    32.        // Save the scene
    33.        // On the next Load the cube should have position Y=10.
    34.        EditorSceneManager.SaveScene(scene);
    35.    }
    36. }
    37. #endif
    38.  
    39.  
     
    Last edited: Aug 25, 2019
  2. Quatum1000

    Quatum1000

    Joined:
    Oct 5, 2014
    Posts:
    889
    (Case 1179151)