SceneManager.LoadSceneAsync() is freezing the menu scene while the async operation is loading the game scene.
I have some animation as part of my menu scene and as soon as I start the async operation the animation completely freezes for about 10-15s, then the game scene is shown.
I have tried a bunch of things with no success including upgrading to Unity 2017.2.1f1.
My expectation with using Async is that the animations will continue normally and I can click and do things while the Async operation is doing its thing. Is my expectation correct or am I wrong.
My searches have shown that a lot of people have this same problem over the years and I thought this would have been fixed by now.
Any suggestion would be appreciated.
Here is the code:
public class AsyncLoadLevelController : MonoBehaviour {
private string loadProgress = "Loading...";
private string lastLoadProgress = null;
public void StartLoadRoutine(string levelName)
{
StartCoroutine(LoadRoutine(levelName));
}
private IEnumerator LoadRoutine(string next)
{
AsyncOperation op = SceneManager.LoadSceneAsync(next);
op.allowSceneActivation = false;
while (!op.isDone)
{
if (op.progress < 0.9f)
{
loadProgress = "Loading: " + (op.progress * 100f).ToString("F0") + "%";
}
else // if progress >= 0.9f the scene is loaded and is ready to activate.
{
op.allowSceneActivation = true;
loadProgress = "Loading ready for activation";
}
if (lastLoadProgress != loadProgress) { lastLoadProgress = loadProgress; Debug.Log(loadProgress); } // Don't spam console.
yield return null;
}
loadProgress = "Load complete.";
}
}
It’s a known issue that Unity’s async loading is causing spikes and freezes, thus not really async at all:
The same problem exists for the editor:
However, 10-15 seconds is quite long and I doubt that it’s related to these Unity issue entirely. What platform do you target?
According to the code you posted, you have an else clause to enable scene activation. Do you know whether the 10-15s freeze is caused by loading or scene activation? You should be able to use Unity’s Profiler to find out.
A common issue for freezes during scene load is scene activation, which I believe is split in at least to phases.
Unity is doing something called “Asset Integration” which runs on the main-thread. Not much we can do here afaik, perhaps packaging smaller asset bundles.
Unity calls Awake() on every Component that is in the loaded scene. If you do stuff inside Awake, it can sum up to take quite long. You could split your scene into more smaller ones. There are other workarounds presented in the video below.
Here is a video from Unite 2016, where the INSIDE creators describe how they implemented stutter free loading. It starts at a 9m 54s:
The short version of it is: They modified the Unity source code and put a lot of effort into this issue.
Wow !!!
Great Answer. Great information.
Now I know that Async is NOT Async. Hmmm things you learn every day.
Since time slicing, scheduling, etc. is way above my expertise (right now) I now know that I am screwed with my little Android game to use Async.
Why doesn’t Unity incorporate all of that stuff that they describe in the video into Unity and call it LoadSceneAsync() ??!!??
Maybe Unity should release whatever code they came up with in the video and let people use it rather than have to try to create something on their own.
The platform is Android.
From what I can tell from looking at the output in the console (reviewing the time.time Debug.Log()). It only takes 1 -2 frames to get to 90%. However, the pause could be happening during this time or after this time. Not sure how I would know since everything freezes.
Interesting enough the background music continues playing without any interruption.