- Home /
Using LoadLevelAsync to load a level in the background , but initiate the level changing after something is done
I have an introductional video as the first level of my game.
As it plays , i want the menu level to be loaded in the background , but only change the level when the video is done playing. I currently have this which does not work.
var introVid:AsyncOperation;
var movieTexture:MovieTexture;
var canLoad:boolean=false;
function Start(){
movieTexture.Play();
LoadLevelAsynchronously();
camera.main.audio.Play();
Debug.Log("level was loaded");
}
function OnGUI () {
GUI.DrawTexture(Rect(0,0,Screen.width,Screen.height),movieTexture,ScaleMode.StretchToFill);
if(!movieTexture.isPlaying)
canLoad=true;
}
function LoadLevelAsynchronously(){
introVid=Application.LoadLevelAsync("Menulvl");
while(!introVid.isDone&&canLoad==false)
yield;
}
What am i doing wrong here?
can you describe the symptoms? Is it just not playing the video? Do you get any runtime exceptions or warnings?
$$anonymous$$y guess is that when the movie is downloading but has not started, the "movieTexture.isPlaying" function will return false because it is technically not playing. Even though I see you telling the movie to play, it might not start immediately. Just because a movie is not playing does not mean it has finished, it might have not started. You might need to check the "movieTexture.isReadyToPlay" function too.
$$anonymous$$ight want to add some debug statements to see what the initial value of "movieTexture.isPlaying" returns.
Also, in your while loop, I would change "&&" to "||", though I don't think that is the culprit.
If it just seems to never do anything, you might need to wrap the line "LoadLevelAsynchronously();" with "StartCoroutine()" and make the function return an Enumerator.
It is playing the video , it interrupts it halfway through though when the level loading is finished , i set it to yield until canLoad gets true , which only gets true when the video is finished , but it is not working.
Answer by HazeTI · Sep 19, 2012 at 02:50 PM
I know this is a really old question but as I came across this when searching for something else I thought I'd answer.
It's basically exactly what I asked here and the answer is that you can't use LoadLevelAsync to choose when to switch levels. It will load the level in the background but then switch as soon as it is ready.
You could use AssetBundles to achieve something, as pointed out in the above link.
Actually this answer is even better: http://answers.unity3d.com/questions/137261/loading-level-async-without-switching-the-level-im.html
@HazeTI rather than adding a comment, you could/should've edited your answer... The second idea indeed seems much better! Also, try bringing the content from any link into your answer, with your own words like you did for the first one. ;-)