So ive been stuck on this question for a while so i thought to ask to see if anyone has a solution. My problem is that if i want to have for example a loading bar for loading my scene. I can use SceneManager.LoadSceneAsync() but lets say my game maybe spawns some objects in dynamically. Depending on something like a save file. Then those objects wont get loaded asynchrousnly. My problem is just that i dont know how or if there is a way to basically spawn objects as part of the async scene load. As i have a save file that has alot of objects that might need to be spawned in dynamically depending on how much the player has built or placed. Any help would be greatly appreciated as always.
Unity provides a way to instantiate game objects asynchronously using the InstantiateAsync method. However, I think you’re asking about instantiating objects dynamically as the scene itself loads.
If that’s the case, it’s not directly possible. You first need to load the scene, and then create the objects within it. The simplest approach is to load the scene, display a loading screen to the player while the objects are instantiated, and then remove the loading screen once everything is initialized.
There are more complex alternatives, for example, loading the objects in the current scene, disabling them, then additively loading the new scene, moving the objects there, and re-enabling them. However, in most cases this extra effort isn’t worth it. The simplest solution is to keep showing a loading screen —which in fact is a new loading screen that is being shown from the new loaded scene— even after the scene has loaded, until all dynamically instantiated objects are ready.
This api seems very good to use and i will intergrate it in the future. what i do right now is that i just wait a few frames per instantiation to simulate it not freezing the game and spacing it out. It works for now but will definentley use AsyncInstantiation in the future. Thanks for the tip.
Another way you can try, is to not freeze the game but instantiate a very small part of the scene, instead of a loading screen, that hides everything for a few frames while it gets everything else instantiated.
For example, in a 3D world, when the character moves to a different floor in a building, you could instantiate the interior of an elevator. The elevator doors would stay closed until everything in the new scene is fully loaded, creating the illusion of movement and allowing the player to prepare inside the elevator, while masking the loading process.
While this isnt a solution i think would fit my gamel. Its still a very great idea and i will keep it in the back of my head.