My unity version is 2017.3.0f. I wrote this coroutine to load a scene asynchronously. But when I call it using StartCoroutine(LoadGame()), Unity Engine stucks (just the way an infinite loop would do). Please help me find what’s wrong with this.
IEnumerator LoadGame()
{
// Create the async operation
AsyncOperation async = SceneManager.LoadSceneAsync(SceneToLoad, LoadSceneMode.Additive);
while (!async.isDone)
{
yield return null;
}
}
You gotta active the scene by manual since you are using LoadSceneAsync.
private IEnumerator LoadSceneAsync(string scene, Action OnLoadScene = null)
{
yield return null;
AsyncOperation ao = SceneManager.LoadSceneAsync(scene,LoadSceneMode.Additive);
ao.allowSceneActivation = false;
while (!ao.isDone)
{
// [0, 0.9] > [0, 1]
// float progress = Mathf.Clamp01(ao.progress / 0.9f);
// Loading completed
if (ao.progress == 0.9f)
{
ao.allowSceneActivation = true;
}
yield return null;
}
if (OnLoadScene != null)
OnLoadScene.Invoke();
SceneManager.SetActiveScene(SceneManager.GetSceneByName(scene));
}