- Home /
LoadLevelAsync behaviour within a scene
I'm looking to achieve the same behaviour as LoadLevelAsync, but with in-scene operations. My levels are loaded dynamically from serialized data at the beginning of the scene.
I'm currently using a co-routine to initialize the level while a loading screen displays, but I am unable to show a loading animation as the co-routine seems to just about pause the scene anyway. As I understand this has to do with main thread vs multi-thread but my googling to get the same behaviour hasn't given me what I need.
tl;dr Is there anyway to achieve the same behaviour of LoadLevelAsync when you plan on populating the scene after it begins?
Answer by Ramlock · Mar 26, 2015 at 07:36 AM
Maybe Threading is what you need, instead of Coroutines: https://msdn.microsoft.com/en-us/library/aa645740%28v=vs.71%29.aspx
I didn't understand, though, why you can't just use LoadLevelAsync instead. You said you plan on populating the scene after it begins, but while you do that all you show is a loading screen, so why do they need to be on the same scene? My suggestion is that you simple create a "Loading Scene" that displays your animation and starts the LoadLevelAsync, then when the level is loaded it'll change to the proper scene.
Hope that helps :D
I can't use LoadLevelAsync because my scene is empty. It's only once the scene is loaded that I do some procedural loading. I've done some research on threading before but it appears I can't use the Unity API in another thread, so I can't instantiate and set parameters which is pretty much all I need to do. I'm curious how LoadLevelAsync does this because it seems I just need to emulate its behavior, if only I had the source or something.
I see. You said you are using a Coroutine, but are you using yield often enough? Cause as far as I know, they actually operate on the same thread, so you must prevent it from taking too long on a single step, otherwise you'll have a very low frame rate.
If yielding as often as you can is still not enough, try doing it with a timer (experiment on how long you need it to sleep between steps), and hopefully it'll work.
Hmm, so what your saying is break up my loading sequence with numerous yields?
$$anonymous$$y current coroutine as it stands looks something like this
IEnumerator $$anonymous$$yCoroutine()
{
while(!InitializeLevel())
yield return "";
}
bool InitializeLevel()
{
// Generate Level
return true;
}
With that in $$anonymous$$d would something like breaking up the initialize level function with a step iterator be what your talking about?
IEnumerator $$anonymous$$yCoroutine()
{
int i = 0;
while(!InitializeLevel(i))
{
i++;
yield return "";
}
}
bool InitializeLevel(int i)
{
switch(i)
{
case 0:
// Load some stuff
return false;
case 1:
// Load more stuff
return false;
case 2:
// Load last stuff
return true;
}
}
}
Doing this might help ya?
Yes yes, something like that. From what I see from your original script, it really wasn't behaving like a coroutine at all. Ins$$anonymous$$d, it was loading everything before ever attempting to pause.
But I still don't feel like the switch will give enough pauses for the game not to freeze. Coroutines work with frames, so they should have a pause every frame. In other words, the work they should do each time they're called is $$anonymous$$imal.
First of all, it makes no sense to keep the entire code outside the coroutine method, by doing so you're basically destroying everything the coroutine is there for.
Second, I don't know how you're generating your stuff, but if it's procedural as you said, I suppose it works with multiple loops. If that's the case, try having a yield return null; at every step of it.
I'll be waiting to see how it goes. :D
ya i'm just getting the hang of coroutines and I understand how I can utilize them now after your explanation. I also see why my while() statement and separating everything into another function makes no sense (i did this emulating a coroutine example).
I've set it up now so my initialize game operations have numerous yield statements in between. I can't do quite as many as i would like as there is a lot of work being done in some of my operations but I've got it running marginally smoother.
Thanks for your help!
Your answer
![](https://koobas.hobune.stream/wayback/20220613191252im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
LoadLevelAdditiveAsync progress is always 0 2 Answers
Odd behaviour when preloading multiple scenes 1 Answer
Load menu via SceneManager 0 Answers
SceneManager.LoadScene used within promise (or async callback) does nothing 0 Answers
Get a reference to the newly loaded scene after a LoadSceneAsync() 2 Answers