- Home /
Instantiate many Prefabs / Async Loading
Hi,
I am aware that there are already many questions and answers related to async level loading, but none of these really satisfy me. So here we go:
I am working on an MMO-style game. The world is pretty dynamic, so instead of using unity scenes we create game areas at run-time. This means that a) dozens of prefab resources get loaded b) thousands of prefabs get instantiated
For large areas this takes some time. Now I would like to have an animated loading screen and I don't want to lock up the main thread for several seconds anyway.
Surprisingly, instantiating the objects takes A LOT longer than the actual resource loading. It is much faster in a real build than in the editor, though.
Question 1: Are there tricks to speed up the instantiation process? Batch-Instantiate or something like that? It seems strange that it should take so long.
Question 2: Is there a way to put the loading/instantiating in a background thread? Basically I want my own version of LoadLevelAsync(). I already considered using the Fiber-like logic of CoRoutines, has anyone already tried something like that?
Thanks!
Answer by MadDave · Nov 07, 2012 at 06:50 PM
Since this question still receives views but no answers I'll quickly describe how we solved it. May not be the best solution but works for us so far:
We put all objects to be instantiated in a list. A CoRoutine works its way through that list and yields after a fixed time limit, continuing in the next frame. This allows us to draw a progress bar and do network updates during level loading.
Thanks for the update to your own question. I too am looking for Async Instantiate. I need not so many items to load in very fast but also not lag the current game at all. I might make a list similar to yours and have the LoadNextItem be based on deltaTime.
I wonder if I could load in parts of an Object over time, such as; if (deltaTime < 0.02) load 1% of Item X per frame.
Thanks for your reply anyhow, it helped.
Answer by HexGrimm · Jan 11, 2018 at 09:00 AM
@MadDave I think you can use Scene.AsyncLoad (Additional Load) method to reach your target. I guess it is possible to combine scenes with prefabs linked, so they represent pool of instanced reusable objects. But I am not sure about shaders prewarm and initialization phase that can increase your frame time. It usually happens because some Unity default components do a big work on Awake, which process in main thread context.
Answer by jpgordon00 · May 30, 2021 at 10:21 AM
I created a solution to load any amount of prefabs async with callbacks, timers and more.