- Home /
Question by
MMollick · Mar 25, 2015 at 02:06 PM ·
coroutineloadingyieldloading screenloadlevelasync
Loading screen with LoadLevelAsync
Hello Everyone,
I'm trying to implement a simple loading screen, but can't seem to get the loading screen to actually display. Instead I just get a black screen when the level is loading. Sometimes the black screen is only for a fraction of the second and other times it's nearly a full second.
Heres the code I've got so far. Does anyone have any ideas on how to get this to work? Perhaps I'm using coroutines and yields wrong?
using UnityEngine;
using System.Collections;
public class LevelManager : MonoBehaviour {
static GameObject screen;
void Awake()
{
screen = gameObject;
DontDestroyOnLoad(screen);
screen.SetActive(false);
}
public static void Load(string name)
{
screen.SetActive(true);
LevelManager instance = screen.GetComponent<LevelManager>();
instance.StartCoroutine(instance.InnerLoad(name));
}
IEnumerator InnerLoad(string name)
{
AsyncOperation async = Application.LoadLevelAsync(name);
while (!async.isDone)
{
screen.SetActive(false);
yield return null;
}
}
}
Comment
$$anonymous$$eep in $$anonymous$$d async loading is much slower in the editor, and doesn't report progress accurately.
To test its workings, you have to build the game you're making.