- Home /
Is it possible to use LoadLevelAsync and then wait?
Hello,
So what I want to do is to load a level in the background. However I don't want the actual switch to happen until I specify. Is this possible? Currently LoadLevelAsync switches as soon as the level has been loaded.
This is for Android/iOS so we're not using streaming.
Thanks.
If you need more info just give me a shout.
Answer by TwisterK · Sep 05, 2013 at 04:53 AM
You can control the activation scene with the latest Unity, all you have to do is
AsyncOperation op = Application.LoadLevelAsync ( "ExampleScene" );
op.allowSceneActivation = false;
When you need to activate the scene, use this
op.allowSceneActivation = true;
http://docs.unity3d.com/Documentation/ScriptReference/AsyncOperation-allowSceneActivation.html
Answer by sebas77 · Apr 10, 2012 at 04:03 PM
no you cannot do it, but since you are obviously using the pro version, what you can do is to create an assetbundle of your scene. In this way you can load the assetbundle asynchronously, then wait, then use Application.LoadLevel (which will be instantaneous because the data is all loaded).
@gizmoi can you explain why using assetbundles in this way is incorrect? i'm looking for good info on this topic.
Answer by BornInABottle · Mar 05, 2018 at 01:50 PM
Here's a working example for a Loading Screen - the loading bar updates as the scehe is loading, then devices that load in 2.5s+ will open the new scene instantly while faster devices will wait for the timer to count down before opening the scene.
IEnumerator LoadAsynchronously(string levelName) {
float timer = 0f;
float minLoadTime = 2.5f;
AsyncOperation operation = SceneManager.LoadSceneAsync(levelName);
operation.allowSceneActivation = false;
while (!operation.isDone) {
float progress = Mathf.Clamp01(operation.progress / 0.9f);
loadingBar.value = progress;
timer += Time.deltaTime;
if (timer > minLoadTime)
{
operation.allowSceneActivation = true;
}
yield return null;
}
yield return null;
}
This is quite an old question now and functionality for this has been added since with allowSceneActivation.
But I did want to comment that there is a potential problem with the code you have posted. If the operation is done before $$anonymous$$LoadTime (either if the load is very quick or you set the $$anonymous$$LoadTime too high) then allowSceneActivation will never be set to true and the scene will never switch.
If you're setting allowSceneActivation to false, then it is probably for a reason and it's much better to have allowSceneActivation set to true under some condition rather than a timer.
Thanks for the feedback. Hadn't realised the functionality was new - was just searching for a solution and couldn't find one online so posted the solution used in my game here :) Regarding operation.isDone, this is stopped at 0.9 until allowSceneActivation = true, so will never complete before $$anonymous$$LoadTime. Docs. Cheers!
Oh, I didn't know that about isDone. Good to know. Thanks!
Your answer
Follow this Question
Related Questions
LoadLevel + LoadLeveAsync = freeze? 1 Answer
Application.LoadLevelAsync() causes Unity crash? 2 Answers
LoadLevelAsynq and LoadLevelAdditiveAsynq 1 Answer
BuildPipeline.BuildPlayer - Download scenes without adding them to build menu 1 Answer
SceneManager.LoadSceneAsync causes lag spikes, how to make background loading less CPU heavy 1 Answer