- Home /
LoadLevelAsync is acting like LoadLevel
I am having trouble getting LoadLevelAsync to work correctly. For some reason it seems to act just like LoadLevel. The loading time is almost the exact same and when the user clicks on the gui to pick which level to load next it freezes all movement while it attempts to load the next level. All I need is for the user to be able to continue walking around the scene while the next scene is loaded. Loading time isn't as important. Here is a small snippet of my code, and I just can not figure out what the problem is.
void OnGUI()
{
Rect[] guiRects = {Rect1, Rect2, Rect3, Rect4};
string[] configNames = {"Level1", "Level2", "Level3", "Level4"};
for(int i = 0; i < guiRects.Length; i++)
{
if(GUI.Button(guiRects[i], configNames[i]))
StartCoroutine(this.LoadLevel(configNames[i]))
}
}
IEnumerator LoadLevel(string name)
{
yield return new WaitForSeconds(1.5f);
//nextLevel is one of the class fields
nextLevel = Application.LoadLevelAsync(name);
while (!nextLevel.isDone)
{
yield return 0;
}
}
When you put a Debug.Log in your while loop, how often does it get called?
It only got called once and that was right as the next scene loaded
I believe you can yield directly on the LoadLevelAsync call (it returns an AsyncOperation).
Answer by logicalerror · Sep 17, 2014 at 01:33 PM
I discovered that LoadLevelAsync behaves like LoadLevel depending on which thread it's running.
Unity has this nasty undocumented behavior that in Start/Awake it's running on the main thread (which makes LoadLevelAsync behave like LoadLevel) and everywhere else it might not run on the main thread. Update seems to make LoadLevelAsync asynchronous for me .. on PC at least.
Had a similar problem, when starting a coroutine in the first OnEnable. yield return null;
fixed it.
(yield return null;
is the same as yield return new WaitForEndOfFrame();
but does not impact garbage-collection)
Answer by Stevenwithaph · Jul 29, 2013 at 05:15 PM
while (!nextLevel.isDone)
{
yield return 0;
}
This line here might be the problem, I think you're pausing the application in the while loop.
Looking at the docs(http://docs.unity3d.com/Documentation/ScriptReference/Application.LoadLevelAsync.html) you may want to try something like this
AsyncOperation async = Application.LoadLevelAsync("MyBigLevel");
yield return async;
And just to make sure, async operations are unity pro only, so make sure you have unity pro setup.
I don't know why my answer is just showing that tiny piece, there was a lot more.
Answer by DVFrance · Aug 06, 2014 at 01:54 PM
I've wrote this maybe it could help you : (in C#)
void OnGUI()
{
if(async != null)
{
loadingComplete = async.progress;
}
else
loadingComplete = 0;
GUI.DrawTexture(new Rect(0, 0, Screen.width * (loadingComplete), 50), Texture2D);
}
IEnumerator waitAsecond (float waitTime)
{
yield return new WaitForSeconds (waitTime);
StartCoroutine(Load ());
}
IEnumerator Load ()
{
async = Application.LoadLevelAsync("###");
async.allowSceneActivation = false;
yield return async.isDone;
yield return loadingComplete = 100;
SwitchScene();
}
private void SwitchScene()
{
if (async != null)
async.allowSceneActivation = true;
}
I still have a freeze but it's the best I could do, so if someone found a better way I'm interested !!!
Answer by smoggach · Aug 06, 2014 at 02:05 PM
While you're waiting for your level to load your coroutine is just returning 0 constantly. The loading is not what is slowing your program it is this loop.
Instead of yield return 0, use yield return new WaitForEndOfFrame()
Answer by J_Troher · Dec 08, 2014 at 01:26 AM
I've tried all of these answers and none of them work for me. lol. Also Using Pro.