Search Unity

  1. We are looking for feedback on the experimental Unity Safe Mode which is aiming to help you resolve compilation errors faster during project startup.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

SceneManager.LoadSceneAsync() freezes animation

Discussion in 'Editor & General Support' started by TGKG, Dec 19, 2017.

  1. TGKG

    TGKG

    Joined:
    Dec 31, 2014
    Posts:
    178
    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:
    Code (CSharp):
    1.  
    2. public class AsyncLoadLevelController : MonoBehaviour {
    3.    
    4.     private string loadProgress = "Loading...";
    5.     private string lastLoadProgress = null;
    6.  
    7.      public void StartLoadRoutine(string levelName)
    8.     {      
    9.         StartCoroutine(LoadRoutine(levelName));
    10.     }
    11.    
    12.     private IEnumerator LoadRoutine(string next)
    13.     {
    14.         AsyncOperation op = SceneManager.LoadSceneAsync(next);
    15.         op.allowSceneActivation = false;
    16.         while (!op.isDone)
    17.         {
    18.             if (op.progress < 0.9f)
    19.             {
    20.                 loadProgress = "Loading: " + (op.progress * 100f).ToString("F0") + "%";
    21.             }
    22.             else // if progress >= 0.9f the scene is loaded and is ready to activate.
    23.             {              
    24.                 op.allowSceneActivation = true;              
    25.                 loadProgress = "Loading ready for activation";
    26.             }
    27.             if (lastLoadProgress != loadProgress) { lastLoadProgress = loadProgress; Debug.Log(loadProgress); } // Don't spam console.
    28.             yield return null;
    29.         }
    30.         loadProgress = "Load complete.";
    31.        
    32.     }
    33. }
    34.  
     
    indieDoroid likes this.
  2. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    4,928
    It's a known issue that Unity's async loading is causing spikes and freezes, thus not really async at all:
    https://issuetracker.unity3d.com/issues/async-load-is-not-async

    The same problem exists for the editor:
    https://issuetracker.unity3d.com/is...-the-loading-scenes-with-a-lot-of-gameobjects

    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.
     
    Last edited: Dec 19, 2017
    Cfirzi and indieDoroid like this.
  3. TGKG

    TGKG

    Joined:
    Dec 31, 2014
    Posts:
    178
    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.

    Thank you.
     
    Last edited: Dec 19, 2017
  4. TGKG

    TGKG

    Joined:
    Dec 31, 2014
    Posts:
    178

    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.
     
  5. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    4,928
    As far as I know, just double-checked the documentation too, Unity triggers scene activation at 90%, see here.

    The Unity Profiler should help here, I guess.

    The reason for this is probably, because music is playing on a separate thread. If loading blocks the main thread, music is not affected.
     
  6. TGKG

    TGKG

    Joined:
    Dec 31, 2014
    Posts:
    178
    Thanks Peter77, great feedback. I will work with these ideas and see if it all helps.
     
  7. TitanUnity

    TitanUnity

    Joined:
    May 15, 2014
    Posts:
    180
    Not sure if related, but we're seeing a loading stutter as well but within LoadAssetAsync related to assetbundles
    here
     
  8. TGKG

    TGKG

    Joined:
    Dec 31, 2014
    Posts:
    178
    Okay,
    I will check to see if I am missing a yield statement and see if that fixes the issue.
     
unityunity