Search Unity

  1. Unity 2019.1 is now released.
    Dismiss Notice

Unexpected Scene Loading Behaviour

Discussion in 'Scripting' started by Joshhua5, Nov 16, 2018.

  1. Joshhua5

    Joshhua5

    Joined:
    Oct 25, 2016
    Posts:
    3
    I'll just right into it, we have three scenes: SceneOne, SceneTwo and Loading.

    SceneOne loads the Loading scene which is additive, this loading scene will remain active while we swap out the other scenes.

    SceneOne then loads SceneTwo, Sets Loading as the active scene, calls unloadAsync and loadAsync at the same time, allow completion is set to false for the load. Once the unload is complete we set the allowCompletion to true and wait for the completed even on the load.

    My problem though is that the event for the completion doesn't get called for either the unload of SceneOne or the load of SceneTwo UNTIL we exit play mode on the editor, then everything snaps to completion and the game stops.

    I created a sample project that demonstrates this, I'm not sure if I'm using the API incorrectly or if there's a bug deeper in Unity with 2018.2.15f.

    Code (CSharp):
    1.  
    2. public class SceneLoader : MonoBehaviour {
    3.  
    4.     string loadSceneName;
    5.  
    6.     Scene currentScene;
    7.     Scene loadingScene;
    8.  
    9.     AsyncOperation loadSceneAsync;
    10.     AsyncOperation unloadSceneAsync;
    11.  
    12.     // Use this for initialization
    13.     void Start ()
    14.     {
    15.         currentScene = SceneManager.GetActiveScene();
    16.         SceneManager.LoadSceneAsync("Scenes/Loading", LoadSceneMode.Additive)
    17.             .completed += LoadingSceneAsync_completed;
    18.     }
    19.    
    20.     private void LoadingSceneAsync_completed(AsyncOperation obj){
    21.         loadingScene = SceneManager.GetSceneByName("Loading");
    22.     }
    23.  
    24.     // This function will unload the current scene and load in the new one.
    25.     public void StartSceneChange()
    26.     {
    27.         string SceneName = "SceneTwo";
    28.          
    29.         loadSceneName = SceneName;
    30.         // If we're just refreshing the scene
    31.         // Set the scene to hide the scene swap
    32.         SceneManager.SetActiveScene(loadingScene);
    33.         // Hides any new scene transitions
    34.  
    35.         // Start loading and unloading the new scenes
    36.         loadSceneAsync = SceneManager.LoadSceneAsync(SceneName, LoadSceneMode.Additive);
    37.         loadSceneAsync.allowSceneActivation = false;
    38.  
    39.         unloadSceneAsync = SceneManager.UnloadSceneAsync(currentScene);
    40.         unloadSceneAsync.completed += UnloadSceneAsync_completed;
    41.         loadSceneAsync.completed += LoadSceneAsync_completed;
    42.  
    43.         Debug.Log("StartSceneChange");
    44.     }
    45.  
    46.     private void UnloadSceneAsync_completed(AsyncOperation obj)
    47.     {
    48.         Debug.Log("UnloadSceneAsync_completed");
    49.         loadSceneAsync.allowSceneActivation = true;
    50.     }
    51.  
    52.     private void LoadSceneAsync_completed(AsyncOperation obj)
    53.     {
    54.         Debug.Log("LoadSceneAsync_completed");
    55.         var scene = SceneManager.GetSceneByName(loadSceneName);
    56.         if (!scene.IsValid())
    57.             scene = SceneManager.GetSceneByPath(loadSceneName);
    58.  
    59.         SceneManager.SetActiveScene(scene);
    60.         currentScene = scene;
    61.     }
    62. }
    63.  
     

    Attached Files:

  2. Joshhua5

    Joshhua5

    Joined:
    Oct 25, 2016
    Posts:
    3
    2018.2.16f has the same behaviour.