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.