SceneManager.LoadSceneAsync not working as expected

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:

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using System.Collections;

public class LoadingScreen : MonoBehaviour {

    public const int LoadingSceneIndex = 1;

    public static int NextSceneIndex = -1;

    /// <summary>
    /// Shows the loading screen, then asynchonously loads a level with it.
    /// </summary>
    public static void LoadLevel(int index)
    {
        if(index >= 0)
        {
            NextSceneIndex = index;
        }

        SceneManager.LoadScene(LoadingSceneIndex, LoadSceneMode.Single);

    }


    #region INSTANCE METHODS

    [SerializeField]
    Slider progressSlider;

    void Start()
    {
        progressSlider.minValue = 0f;
        progressSlider.maxValue = 100f;
        progressSlider.value = 0f;

        doneLoadingScene = false;

        StartCoroutine(LoadNextLevelAsync());

    }

    AsyncOperation ao;
    bool doneLoadingScene = false;


    void Update()
    {
        if(ao != null && !doneLoadingScene)
        { 
            progressSlider.value = ao.progress * 100f;
            if(ao.isDone)
            { 
                progressSlider.value = 100f;
                doneLoadingScene = true;
            }
        }
     }

    IEnumerator LoadNextLevelAsync()
    {
       

        ao = SceneManager.LoadSceneAsync(NextSceneIndex, LoadSceneMode.Single);
        yield return ao;
    }

    #endregion

}

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.

3 Likes

Have you tested it outside of the Editor? IIRC, LoadLevelAsync has never actually been async within the Editor, but it works in builds.

5 Likes

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

2 Likes

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 :slight_smile:

13 Likes