I’m loading scenes additively on demand. When I load a scene it freezes up everything for an instant. Being a VR game this is kind of disorienting. I plan on having some distracting elements while the scene loads, like a door sliding open, so it doesn’t have to be super fast. I’ve been experimenting with the code from the Unity docs and I still get that freeze for an instant the first time. The second time it works pretty smoothly, but I don’t want to have multiple scenes loaded up just because of the freeze. I also experimented with adding a WaitForSeconds on the yield but that doesn’t seem to matter.
Using Unity 2021.3.2f1
public void LoadScene(string sceneName)
{
UnloadPreviousScene();
currentSceneName = sceneName;
//
// SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Additive);
StartCoroutine(LoadYourAsyncScene(sceneName));
}
IEnumerator LoadYourAsyncScene(string sceneName)
{
// The Application loads the Scene in the background as the current Scene runs.
// This is particularly good for creating loading screens.
// You could also load the Scene by using sceneBuildIndex. In this case Scene2 has
// a sceneBuildIndex of 1 as shown in Build Settings.
AsyncOperation asyncLoad = SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Additive);
// Wait until the asynchronous scene fully loads
while (!asyncLoad.isDone)
{
yield return null;
}
}
Start by taking a profiler and narrowing down what is causing freezing. Even if scene assets are loaded async instantiating all the objects and running all the awake/start methods will probably happen within main thread. Maybe you have too much heavy initialization for the scripts within scene you load. Or maybe heavy initialization for some static variables which get accessed for the first time in scene you are loading, that could cause the difference you are observing with second time not causing freeze.
im having that problem because its loadings a big level, unavoidably large. i want the scene to load in the background while my main menu scene stays in basic operation enough to display a loading bar. probably one of the most basic asks for a video game.
“Unavoidably” doesn’t really exist No matter how good a loading system is, you can always push beyond its limits and complain about the performance. I recently played a lot Space Engineers again. The map has already grown to several hundreds of megabytes and loading the world takes something around 2 minutes (it has to recreate tons of cube voxel grids and a lot of smooth marching cube planets and asteroids). So the game freezes for 2 minutes straight. Trying to somehow make it loading smoothly would probably mean that it takes a quater of an hour to load ^^.
So you only have two options here. Either show an actual loading screen while the scene is still loading which of course would freeze the player, or split the level into smaller parts which you can load independently in sequence. Some other game engines may have such a splitting / streaming mechanic already built into the engine. However such in-house engines are often optimised for a particular problem they faced at the time. Unity is a general purpose engine and as I said, with a generic game you can always push beyond the capabilities. It’s just not possible to have an engine that performs 100% perfect in any potential situation. So your task is to find a workaround that works for your case.
You may be able to reduce the freeze time by disabling all the colliders in your scene before saving it. Then after you load the scene you can enable all the colliders gradually over several seconds. For PhysX to be so fast at collision detection it needs to build a complex data structure which can be a quite slow compared to the actual transfer time of data from your drive to system memory and GPU memory.
That’s predicated on the assumption that profiling has been done and this has been shown to be a problem which I’d suspect isn’t at all compared to the generation of all the GameObjects, Components, Transform hierarchy set-up, resource loading etc.
Disabling them is enough. It’ll probably be mesh colliders that are causing a freeze as I find primitive colliders aren’t really a problem unless you’re instantiating or loading lots of overlapping colliders.
But don’t assume the colliders are your issue. You may just have lots of data that needs to be loaded. So do tests. And I’m not sure if newer versions of Unity still have the issue with colliders. It was many years ago when I was having problems with generating lots of colliders.