Search Unity

  1. How has 2019.2 and the beta been for you so far? Give us feedback in this thread.
    Dismiss Notice

SceneManager.LoadSceneAsync not working as expected

Discussion in '5.3 Beta' started by SentreStage, Nov 24, 2015.

  1. SentreStage

    SentreStage

    Joined:
    Aug 8, 2011
    Posts:
    22
    Hey guys,

    So I'm using the new SceneManager namespace to load some levels from a title screen in little prototype I'm tooling around with to learn the new stuff from the beta here. One of the things I've come across is that LoadSceneAsync does not appear to be asynchonous.

    When called in my script, it still hangs the editor for the same amount of time while it loads the level up. Heres my code:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using UnityEngine.SceneManagement;
    4. using System.Collections;
    5.  
    6. public class LoadingScreen : MonoBehaviour {
    7.  
    8.     public const int LoadingSceneIndex = 1;
    9.  
    10.     public static int NextSceneIndex = -1;
    11.  
    12.     /// <summary>
    13.     /// Shows the loading screen, then asynchonously loads a level with it.
    14.     /// </summary>
    15.     public static void LoadLevel(int index)
    16.     {
    17.         if(index >= 0)
    18.         {
    19.             NextSceneIndex = index;
    20.         }
    21.  
    22.         SceneManager.LoadScene(LoadingSceneIndex, LoadSceneMode.Single);
    23.  
    24.     }
    25.  
    26.  
    27.     #region INSTANCE METHODS
    28.  
    29.     [SerializeField]
    30.     Slider progressSlider;
    31.  
    32.     void Start()
    33.     {
    34.         progressSlider.minValue = 0f;
    35.         progressSlider.maxValue = 100f;
    36.         progressSlider.value = 0f;
    37.  
    38.         doneLoadingScene = false;
    39.  
    40.         StartCoroutine(LoadNextLevelAsync());
    41.  
    42.     }
    43.  
    44.     AsyncOperation ao;
    45.     bool doneLoadingScene = false;
    46.  
    47.  
    48.     void Update()
    49.     {
    50.         if(ao != null && !doneLoadingScene)
    51.         {
    52.             progressSlider.value = ao.progress * 100f;
    53.             if(ao.isDone)
    54.             {
    55.                 progressSlider.value = 100f;
    56.                 doneLoadingScene = true;
    57.             }
    58.         }
    59.      }
    60.  
    61.     IEnumerator LoadNextLevelAsync()
    62.     {
    63.        
    64.  
    65.         ao = SceneManager.LoadSceneAsync(NextSceneIndex, LoadSceneMode.Single);
    66.         yield return ao;
    67.     }
    68.  
    69.     #endregion
    70.  
    71. }
    72.  

    As you can probably tell, this code is supposed to update the 'value' property of a Slider (uGUI) using the AsyncOperation.progress to create a simple loading screen. However, it would appear that the AsyncOperation is not running in the background, and instead runs on the main thread for some reason. (Just an assumption I'm making based on the fact that Unity stops responding while its loading, and my mouse turns into the "waiting" cursor).

    Is this actually a bug? Or am I doing something wrong here? I had expected this code to allow me to smoothly update a Slider across the screen based on the progress to make a simple loading screen.
     
    erenaydin and Novack like this.
  2. superpig

    superpig

    Quis aedificabit ipsos aedificatores? Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,157
    Have you tested it outside of the Editor? IIRC, LoadLevelAsync has never actually been async within the Editor, but it works in builds.
     
    erenaydin and Novack like this.
  3. SentreStage

    SentreStage

    Joined:
    Aug 8, 2011
    Posts:
    22
    Ahh didn't think about that, will test that theory out. Oddly enough though, heres a little update to my post. About 2 hours after posting that, I was test playing the app again, and wammo! it started working.. same code, no changes, right in the editor. So I've come to the conclusion that I don't know whats going on anymore lol
     
    Anisoropos likes this.
  4. SteenLund

    SteenLund

    Unity Technologies

    Joined:
    Jan 20, 2011
    Posts:
    437
    Hi,

    Async loading loads as much as possible on the loading thread but there are still a few things we cannot do on the loading thread e.g. texture uploading to the GPU. In the editor prefab merging is also done on the main thread which will also block it, but this does not actually happen in the standalone players.

    So in general while Play Mode in the editor is good for testing game design and other stuff, it is not good for performance testing :)