I am trying to get a web player to use LoadLevelAsync. Here are some of the problems.
If I set the web player to streaming, LoadLevelAsync seems to assume that the level is ready, and jump to it straight. The streaming is then interrupted. (I am not using the streaming levels functions because the user is allowed to select a level from a menu).
If the web player is not streaming, it will load all the levels and its assets, thus there is no need for LoadLevelAsync.
What is the right way to use LoadLevelAsync?
Edited: Here's the code I am using. Maybe I am doing something wrong?
public var loader : AsyncOperation;
public function Start()
{
DontDestroyOnLoad(this);
}
public function LoadLevel( sceneName:String)
{
loader = Application.LoadLevelAsync(sceneName);
yield loader;
}
public function OnGUI()
{
if (loader != null && loader.isDone == false)
{
// TODO: Change to show the progress bar
GUI.Label(new Rect(0,20, 200, 20), "Loading..." + loader.progress);
}
}
To invoke it
function OnGUI()
{
if (GUI.Button(new Rect(0,0,20, 100), "Load Level")
{
GameObject.Find("Loader").SendMessage("LoadLevel", "level1");
}
}
What happens is as follow:
1) On a streaming webplayer, I get the standard Unity3D blue screen (ie, when the scene is empty). The web player just loads the new level without loading any assets or creating objects for the new level.
2) On a non-streaming webplayer, all levels were already loaded, so it makes no difference.
You need to use Application.GetStreamProgressForLevel() to check if the level has already been downloaded. LoadLevelAsync will not download the level for you, it will just load the level objects while you are still playing, so you get rid of that short loading hickup.
LoadLevelAsync allows you to load new levels while still playing the current one, show a progress bar or create a completely streaming world.
So if you have a menu level, you can load the level in the back while showing a loading bar and then switch to it, when it's ready :)
This is useful because when creating web players, you need to catch peoples attention fast. so a small starter level(plus dll's), play some music flashing color and so forth. then as soon as you'r in the menu, you start downloading lvl 1 so it's ready for the user.
In your scenario where the user can pick and choose, you really win download time for the menu level, but sadly you gotta download the level when the user pushes the button. Still useful because you can show a nice loading screen.
In addition to the above answers, be aware that the objects in your new level will have their Awake and Start functions called in the same thread as your loader level, so if all the work is happening in one of those functions, you’re not going to see any incremental progress.