My project has had an issue with LoadLevelAsync (now LoadSceneAsync) where it freezes (blocking the main thread) when loading large scenes. This happens in the Editor as well as builds, including on other platforms.
I’ve created a test scene that you can download here:
This simple scene uses LoadSceneAsync while rotating a cube in Update. If the rotating cube stops moving, it is because the game is frozen. It also detects when there has been more than 50ms between frames and calls Debug.Log. Here is some example output:
This has been an issue in our project for awhile now, even when we were using Unity 4. It seems to be an issue forothersaswell, but I haven’t seen any solutions. Does anyone know how to get around this issue?
Can you profile it with deep profiling enabled to find out what going on?
Sounds like a bug to me. Please report the simple project you have with the bug report.
In that case it is expected and performance will depend on how many prefabs you have in your scene.
Please use standalone player to test this kind of performance, as the editor is doing additional work which does not give you the right picture of the performance.
When loading an actual scene from my game on an Xbox One build, the profiler looks like this:
The profiler only shows less than 1 second of stalling, but the per-frame Stopwatch says it froze for 14.353 seconds. Here’s the profiler timings on that load:
The difference is likely because I don’t have many different prefabs/assets loading in the test scene, while my actual game’s scene has about 5000 GameObjects (about 1500 unique).
Hi. I think you are hitting a bug (that we have just fixed), where our timers wrap around at 5 seconds. Some of the numbers you have been getting seams quite low for the amount of work being done.
Do you have a bug report case number for this?
-Kim
I just used my Xbox One build as an example; it stalls on all platforms. Here’s some data from the PC build:
The freeze goes down to 1804 ms if I run the build off an SSD. I can post in the Xbox One forum as well if you’d like. Also, I’m not the first user to have this issue:
Ah, I had thought perhaps you were seeing an Xbox One specific bug/issue.
Generally, there are a few things worth keeping in mind with loadlevelasync. While it will do as much as it can async, there are some things in Unity that must happen on the main thread, including calls to Awake in your game scripts. So if you have a very large scene, or expensive awake calls on your objects that can lead to loadlevelasync stalls. The other thing LoadLevelAsync does is tear down the old scene, once the new one has been loaded, which can also be the source of stalls, either because the scene being destroyed is particularly large, or because you have some expensive ondestroy calls in your scripts.
Your callstack above in particular, is the garbage collector running. When switching scenes it’s inevitable for some garbage to be generated, so it might be difficult/impossible to eliminate entirely, but there are things you can do to minimize the effects of GC. You can find some information about that here: Unity - Manual: Memory in Unity