Search Unity

Issue reloading certain scenes using UnloadSceneAsync

Discussion in 'Scripting' started by acmsharBAH, Jun 26, 2018.

  1. acmsharBAH

    acmsharBAH

    Joined:
    Jan 8, 2015
    Posts:
    6
    I am having an issue unloading scenes in the editor. Our project has ~10 scenes, and I have written a script to load them in alongside our base scene.

    I am trying to make this work in the editor so that if a scene is already loaded, it gets unloaded then reloaded (because they shouldn't be active when the game starts). I was able to get this working simply using SceneManager.UnloadSceneAsync(), but a few scenes just wouldn't unload, so I wrote the following code that should wait until the scene is unloaded before reloading, but it hangs on a few of our scenes (others it works for just fine). If I don't try to reload the scene, the unload works just fine on the problematic scenes.

    None of the objects in those scenes are in DontDestroyOnLoad, so I'm wondering if I'm missing something with how to use SceneManager.UnloadSceneAsync.

    Code (CSharp):
    1.     IEnumerator LoadRestOfScenes()
    2.     {
    3.         asyncLoadRestOfScenes = new AsyncOperation[RestOfScenes.Length];
    4.         asyncUnloadRestOfScenes = new AsyncOperation[RestOfScenes.Length];
    5.  
    6.         for (int i = 0; i < RestOfScenes.Length; i++)
    7.         {
    8.             if (SceneManager.GetSceneByName(RestOfScenes[i].SceneName).isLoaded)
    9.             {
    10.                 asyncUnloadRestOfScenes[i] = SceneManager.UnloadSceneAsync(RestOfScenes[i].SceneName);
    11.                 while (!asyncUnloadRestOfScenes[i].isDone)
    12.                 {
    13.                     Debug.Log("UNLOADING: " + RestOfScenes[i].SceneName);
    14.                     yield return null;
    15.                 }
    16.                 Debug.Log("LOADING");
    17.                 asyncLoadRestOfScenes[i] = SceneManager.LoadSceneAsync(RestOfScenes[i].SceneName, LoadSceneMode.Additive);
    18.             }
    19.             else
    20.             {
    21.  
    22.                 Debug.Log("LOADING: " + RestOfScenes[i].SceneName);
    23.                 asyncLoadRestOfScenes[i] = SceneManager.LoadSceneAsync(RestOfScenes[i].SceneName, LoadSceneMode.Additive);
    24.              
    25.             }
    26.             asyncLoadRestOfScenes[i].allowSceneActivation = false;
    27.         }
    28.     }

    Edit: UnloadScene seems to work just fine using the following:

    Code (CSharp):
    1.     void LoadRestOfScenes()
    2.     {
    3.         asyncLoadRestOfScenes = new AsyncOperation[RestOfScenes.Length];
    4.  
    5.         for (int i = 0; i < RestOfScenes.Length; i++)
    6.         {
    7.             if (SceneManager.GetSceneByName(RestOfScenes[i].SceneName).isLoaded)
    8.             {
    9.                
    10.                 SceneManager.UnloadScene(RestOfScenes[i].SceneName);
    11.             }
    12.             asyncLoadRestOfScenes[i] = SceneManager.LoadSceneAsync(RestOfScenes[i].SceneName, LoadSceneMode.Additive);
    13.             asyncLoadRestOfScenes[i].allowSceneActivation = false;
    14.         }
    15.     }
    However, since that method is deprecated, I'd prefer to replace it with the async version if I can get it to work.
     
    Last edited: Jun 27, 2018
  2. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    890
    What does "hangs" mean? Is there an error caught or something. And I really do not understand, why you unload the scenes and reload them again, but maybe thats just me not getting it ;) Did you try to get a new async operation for every scene instead of one asyncoperation for all of them, could think of it might override itself and stop reloading the previous one or are you doing this already with asyncLoadRestOfScenes being an async array?
     
  3. acmsharBAH

    acmsharBAH

    Joined:
    Jan 8, 2015
    Posts:
    6
    1. By hang I mean it doesn't complete. With the code above it just prints out 'UNLOADING: SCENENAME' forever.
    2. The reason for reloading is that if a designer has the scene open in the editor, I don't want it to load the scene twice, so I'd like to unload it, reload it, and set it to not active (like would happen in the build).
    3. Yes, asyncLoadRestOfScenes and asyncUnloadRestOfScenes are both arrays.