Search Unity

SceneManager.LoadSceneAsync freeze in 2018

Discussion in 'Editor & General Support' started by arthuronoszko, Aug 1, 2018.

  1. arthuronoszko

    arthuronoszko

    Joined:
    Feb 24, 2016
    Posts:
    21
    Hello!

    I have been using 2017.4.7 without any problems, and then I wanted to update to 2018 but encountered problems with SceneManager.LoadSceneAsync.

    I have tried to google and search in forums, but I couldn't find any help.

    The problems are with loading time. I got inspired by the package "Adventure - Sample Game" where I saw that they are using a SceneController when transitioning between different scenes. I modified that code a bit and this is what it looks like right now.

    Code (CSharp):
    1. public class SceneController : MonoBehaviour {
    2.         public CanvasGroup FadeCanvas;
    3.         public string FirstSceneToLoad = "Map";
    4.  
    5.         IEnumerator Start() {
    6.             FadeCanvas.alpha = 1f;
    7.             FadeCanvas.blocksRaycasts = false;
    8.  
    9.             yield return StartCoroutine(LoadSceneAndSetActive(FirstSceneToLoad));
    10.             StartCoroutine(Fade(0f));
    11.  
    12.         }
    13.  
    14.         public void TransitionToScene(string toScene) {
    15.             var currentScene = SceneManager.GetSceneAt(SceneManager.sceneCount - 1);
    16.             StartCoroutine(Transition(currentScene.name, toScene));
    17.         }
    18.  
    19.         IEnumerator Transition(string fromScene, string toScene) {
    20.             yield return StartCoroutine(Fade(1));
    21.             var unloadScene = SceneManager.UnloadSceneAsync(fromScene);
    22.  
    23.             while (!unloadScene.isDone) {
    24.                 Debug.Log("UNLOAD SCENE: Progress: " + unloadScene.progress);
    25.                 yield return null;
    26.             }
    27.  
    28.             yield return StartCoroutine(LoadSceneAndSetActive(toScene));
    29.             yield return StartCoroutine(Fade(0));
    30.             Resources.UnloadUnusedAssets();
    31.         }
    32.  
    33.         IEnumerator LoadSceneAndSetActive(string sceneName) {
    34.             var loadScene = SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Additive);
    35.             while (!loadScene.isDone) {
    36.                 Debug.Log("LOAD SCENE: Progress: " + loadScene.progress);
    37.                 yield return null;
    38.             }
    39.             var newlyLoadedScene = SceneManager.GetSceneAt(SceneManager.sceneCount - 1);
    40.             SceneManager.SetActiveScene(newlyLoadedScene);
    41.         }
    42.  
    43.         IEnumerator Fade(float targetAlpha, Action faded = null) {
    44.             FadeCanvas.blocksRaycasts = true;
    45.             var startAlpha = targetAlpha > 0 ? 0 : 1;
    46.  
    47.             var progress = 0f;
    48.             var currentTime = 0f;
    49.             var totalTime = 0.8f;
    50.             while (progress <= 1f) {
    51.                 progress = currentTime / totalTime;
    52.                 currentTime += Time.deltaTime;
    53.                 FadeCanvas.alpha = Mathf.Lerp(startAlpha, targetAlpha, progress);
    54.                 yield return null;
    55.             }
    56.  
    57.             FadeCanvas.alpha = targetAlpha;
    58.             FadeCanvas.blocksRaycasts = false;
    59.  
    60.             if (faded != null)
    61.                 faded();
    62.         }
    63.  
    64.     }
    Sometimes it transitions smoothly, but sometimes it gets stuck and you need to wait in order to it to continue. The times when it gets stuck is very random, at least I can not see any pattern there. After looking at the Debug.Log statements, it looks like UnloadSceneAsync works fast, but LoadSceneAsync can freeze. It freezes at different progress values every time.

    I have tried to even load a nearly empty scene with the same results, in order to rule out that there may be something wrong with my scenes.

    Everything works in the editor, but as I understand, the Editor doesn't really use the Async, but the problem is showing on an iPad.

    Have I done something wrong in my SceneController? Could there be something wrong with my scenes? Anybody that have encountered this problem before?

    I have tried with both 2018.1 and 2018.2 with the same results.

    Help would be very much appreciated :)

    BR
    Arthur
     
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    Try throwing in a Debug.Log after this:

    Code (csharp):
    1.  
    2. while (!loadScene.isDone) {
    3.     Debug.Log("LOAD SCENE: Progress: " + loadScene.progress);
    4.     yield return null;
    5. }
    Your assumption seems to be that it stops during that, but "It freezes at different progress values every time" sounds like it gets passed that and freezes at a later stage.

    I believe it should be possible to attach to your process with a debugger in 2018, though I'm not sure if that's the case for IOS. Try to check if that's the case, if it is, it's a very convenient way to check exactly where things stop.


    Since it doesn't happen on 2017, I'm guessing that this is a bad regression of some sort, but bug reports have a much better chance of getting through if you manage to be specific about exactly what's going wrong.
     
  3. arthuronoszko

    arthuronoszko

    Joined:
    Feb 24, 2016
    Posts:
    21
    Thanks for the extremely quick reply Baste!

    I will try to add a logging statement after that to see if it continues. The reason why I think that it stops during that is because after a random time, it starts to log an increased progress. For example, it can freeze at 0.4555, and wait there for some time, and then it kicks off and continues with a larger value. But, I will try that anyway :)

    That would be really neat to be able to attach a debugger directly to the iPad, will google and see if that is possible!

    I tried now, just for test, to build as a release build, and then it no longer seems to freeze. I can not be sure yet which of the settings contributed to that (Development Build or Script Debugging), but I will investigate further.

    I do not want to file a bug report yet since, as you said, I am not very clear of why this is happening.
     
  4. arthuronoszko

    arthuronoszko

    Joined:
    Feb 24, 2016
    Posts:
    21
    Baste: It did not go further than that while-loop, so it gets stuck while trying to load the new scene :(

    It seems to have something with checking the "Development Build" when building to iOS. I have tried making a small app where it does not freeze, and also tried to run the sample made by Unity (https://assetstore.unity.com/packages/essentials/tutorial-projects/adventure-sample-game-76216) with "Development Build" to true, but the loading times seemed normal.

    Apparently there is something with my specific project that causes this, anyone that has any idea of where to look? I tried to profile it while running on an iPad, but the profiler also freezes during that period, so it did not tell me much...
     
  5. NMSUCC

    NMSUCC

    Joined:
    Aug 25, 2014
    Posts:
    4
    I am having a similar problem with a project of mine, only on the WebGL side of things. I've got a game that we're exporting to HTML5. We can load the same scene over and over again, and it will report the same "system memory in use" and number of "loaded objects" each time we load any given scene, but at some point, trying to load that same scene again will just cause the game to hang. The profiler grinds to a halt at this point, too. Sometimes, it will hang on the first try, sometimes it takes 30-40 loads before it hangs. Sometimes we give up before it hangs - we really can't find a common thread.

    We will get the message about the web page using a lot of memory, but looking in the profiler and at the information reported in the console, it's not going up. And I've tried jacking up the WebGL Memory Size a few times, and it doesn't seem to have any effect.

    Not sure where to look at this point. It seems like SceneManager.LoadScene() just simply fails intermittently.
     
  6. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    No clue, then. If it's consistent that things freeze on load, you could send a bug report with your project. Sometimes, you get the bug though, and sometimes they tell you what you're doing wrong. Sometimes it lingers forever.
     
  7. arthuronoszko

    arthuronoszko

    Joined:
    Feb 24, 2016
    Posts:
    21
    It really sounds like we are experiencing the same problems, it is not consistent for me as well. I even tried to load scene without the Async, and it still hangs for a random period of time, so as you are saying, something seems to fail when the SceneManager tries to load a scene. Frustrating to not know what causes this though.. :p

    The problem is that it is not consistent, but I guess this is the time where it could be worth sending this to Unity and hopefully they will look at it and see if there is something wrong with my stuff or theirs.

    Thank you for your responses, I will update this thread as soon as I hear more from Unity :)
     
  8. odysoftware

    odysoftware

    Joined:
    Jul 21, 2015
    Posts:
    84
    I am experiencing the exact same issue right now - using additive loading after scene unloading and then reloading it starts to get stuck at random times - I can wait it out, sometimes a few sec, sometimes a minute. In xCode it doesn't show any errors but CPU usage at this time is 100%. really strange - did anyone solve this?
    I have used a iphone 5s and ios 12 with latest unity 2018.2.9f1
     
  9. arthuronoszko

    arthuronoszko

    Joined:
    Feb 24, 2016
    Posts:
    21
    Sorry to hear that, but this also points that maybe there is a bug in Unity since I am not alone in this :)
    I have filed a bug report with Unity, so I will report back as soon as I hear anything from them!
     
  10. odysoftware

    odysoftware

    Joined:
    Jul 21, 2015
    Posts:
    84
  11. arthuronoszko

    arthuronoszko

    Joined:
    Feb 24, 2016
    Posts:
    21
    Looks interesting, I will try with 2018.3 and see if it is fixed. I have also not investigated the CPU usage when this occur, so I do not know if it goes up to 100%, will check that as well!

    Yeah, I would love to know why this happens to us. I will investigate further and let you know my findings :)
     
  12. Pool4Sky

    Pool4Sky

    Joined:
    Aug 31, 2014
    Posts:
    15
    I wrote to Unity today with a similar (or same) issue. I got frame stuttering or freeze if I load a new scene with SceneManager.LoadSceneAsync. I know that issue is in Unity's bug list. (for years) They should fix it as soon as possible. No game outside has so much issues with loading a scene in background.
     
    Nearo1 likes this.
  13. MatLoz

    MatLoz

    Joined:
    Dec 17, 2014
    Posts:
    25
    I'm having the same problem now, I'm on 2018.2.0. Did you manage to find a version or a tweak to avoid this problem ?
     
  14. odysoftware

    odysoftware

    Joined:
    Jul 21, 2015
    Posts:
    84
    Still having the same issue on a iPhone 5S device - definitely not fixed in 2018.3 - strangely the issuetracker now shows Fixed in 2019.1 !?!? First it was listed as Fixed in 2018.2 then 2018.3... so we cant comment there on the issue because they always say its fixed but now it shows fixed in 2019.1 https://issuetracker.unity3d.com/is...ompare-exchange-weak-explicit-on-some-devices - still if you look in release notes of 2019.1 nothing mentioned of this issue there as being fixed... very strange...
     
  15. f0ff886f

    f0ff886f

    Joined:
    Nov 1, 2015
    Posts:
    201
    Are all of you targeting iOS? I just build my first build on 2018.3.11f1 after upgrading from 2017.4, and it takes about 3 minutes to load our scenes (I load a master then load all scenes additively once), and the entire player hangs and stops responding (on 2017.4 my loading screen works fine).

    Did I just shoot myself in the foot by upgrading?
     
  16. peterdfinn

    peterdfinn

    Joined:
    Aug 15, 2018
    Posts:
    5
    I'm having this issue as well in Unity 2019.4.3f1 Personal. Is there an official response yet?

    I create my loading scene, which has three animations: a MoveSpritesIn animation that sets up things in the loading scene, a looping Loading animation which, when started, unloads all old scenes and asynchronously loads a bunch of new ones, and a MoveSpritesOut animation that clears all sprites in the loading scene when the required loading and unloading is finished.

    I'm playing the game in the editor, and the game freezes at the very beginning of the looping Loading animation. I know that all the scenes get loaded, since the coroutine I set up to do the loading has a nested function that returns true when all AsyncOperations are done, and it does manage to return true fairly quickly.
     
  17. apoorva_unity904

    apoorva_unity904

    Joined:
    Jul 25, 2020
    Posts:
    1
    I am having the same issue too even with Unity 2018.4 (LTS) version. My editor freezes when a scene loads. Sometimes it takes 4 sec and the other time it takes 40 sec to load same scenes. What I have noticed that while a load scene the profiler freezes for sometimes and then it shows large spike/ spikes in the CPU usage area for the first frame (10000 ms sometimes) and then it comes down to 15ms (~ 60 fps) for rest of the frames. I am highly suspecting that when we load a scene using LoadSceneAsync somehow it results into high CPU Usage. I have also tried turning on/off V-Synch and changing time-slice, ring-buffer values but nothing helped. Please help me! Ch 1 Lvl 1 - high CPU usage (profiler analysis).jpg
     
    Last edited: Dec 14, 2020
  18. Max_Aigner

    Max_Aigner

    Joined:
    May 9, 2017
    Posts:
    42
    Hi there, I know this is old, but you are actually using the "yield return Startcoroutine(IEnumerator)" in a way where it is already a Coroutine. Instead you should use "Yield return IEnumerator".. I have edited the code a bit to be more smooth. This works for me now, hope it helps anyone else:

    Code (CSharp):
    1.  
    2. using System;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6. using UnityEngine.SceneManagement;
    7.  
    8. public class SceneController : MonoBehaviour
    9. {
    10.     public CanvasGroup FadeCanvas;
    11.     public string FirstSceneToLoad = "StartScene";
    12.  
    13.     void Start()
    14.     {
    15.         FadeCanvas.alpha = 1f;
    16.         FadeCanvas.blocksRaycasts = false;
    17.         StartCoroutine(Fade(0f));
    18.     }
    19.  
    20.     public IEnumerator TransitionToScene(string toScene, Action finished)
    21.     {
    22.         if(SceneManager.sceneCount > 1)
    23.         {
    24.             var currentScene = SceneManager.GetSceneAt(SceneManager.sceneCount - 1);
    25.             yield return Transition(currentScene.name, toScene);
    26.         } else
    27.         {
    28.             yield return LoadSceneAndSetActive(toScene);
    29.         }
    30.         LightProbes.TetrahedralizeAsync();
    31.         finished?.Invoke();
    32.     }
    33.     private IEnumerator Transition(string fromScene, string toScene)
    34.     {
    35.         yield return StartCoroutine(Fade(1));
    36.         var unloadScene = SceneManager.UnloadSceneAsync(fromScene);
    37.         int lastpercent = 0;
    38.         while (!unloadScene.isDone)
    39.         {
    40.             if((int)(unloadScene.progress*100)!= lastpercent)
    41.             {
    42.                 lastpercent = (int)(unloadScene.progress*100);
    43.                 Debug.Log("UNLOAD SCENE: Progress: " + unloadScene.progress);
    44.             }
    45.             yield return null;
    46.         }
    47.  
    48.         yield return LoadSceneAndSetActive(toScene );
    49.         yield return Fade(0);
    50.         Resources.UnloadUnusedAssets();
    51.     }
    52.  
    53.     private IEnumerator LoadSceneAndSetActive(string sceneName)
    54.     {
    55.         var loadScene = SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Additive);
    56.         int lastpercent = 0;
    57.         while (!loadScene.isDone)
    58.         {
    59.             if ((int)(loadScene.progress *100)!= lastpercent)
    60.             {
    61.                 lastpercent= (int)(loadScene.progress*100);
    62.                 Debug.Log($"LOAD SCENE: Progress: {loadScene.progress * 100f}%");
    63.             }
    64.             yield return null;
    65.         }
    66.         var newlyLoadedScene = SceneManager.GetSceneAt(SceneManager.sceneCount - 1);
    67.         SceneManager.SetActiveScene(newlyLoadedScene);
    68.     }
    69.  
    70.     private IEnumerator Fade(float targetAlpha, Action faded = null)
    71.     {
    72.         FadeCanvas.blocksRaycasts = true;
    73.         var startAlpha = targetAlpha > 0 ? 0 : 1;
    74.  
    75.         var progress = 0f;
    76.         var currentTime = 0f;
    77.         var totalTime = 0.8f;
    78.         while (progress <= 1f)
    79.         {
    80.             progress = currentTime / totalTime;
    81.             currentTime += Time.deltaTime;
    82.             FadeCanvas.alpha = Mathf.Lerp(startAlpha, targetAlpha, progress);
    83.             yield return null;
    84.         }
    85.  
    86.         FadeCanvas.alpha = targetAlpha;
    87.         FadeCanvas.blocksRaycasts = false;
    88.         faded?.Invoke();
    89.     }
    90.  
    91. }
    92.  
     
  19. JesterGameCraft

    JesterGameCraft

    Joined:
    Feb 26, 2013
    Posts:
    452
    I know this is an old thread but perhaps this will help someone. I have many differences from the original setup that was described at start of this thread, and so my issues might be specific to my setup. I am however getting an inconsistent Unity Editor crash, beach ball on Mac. I have a M1 with new apple silicone and am working on project that started initially on a Mac that was intel based (Unity 2021.3.5f1). I have a menu scene that loads a loading scene, and that in turn loads the final game scene. From time to time the Editor will freeze when I attempt to trigger the loading from menu scene. It freezes at loading the game scene (the third and final one).

    I am using LoadSceneAsync with Single mode since I won't want the loading scenes and menu to stick around (at least for now). I traced the problem with the game scene freezing to this message:

    The Progressive CPU lightmapper is not supported on Apple silicon, switching to the Progressive GPU lightmapper.

    Again, I know this is specific to me but I think it might point to a systemic issue. After changing to Progressive GPU lightmapper on the gaming scene it loads solid all the time (at least so far after many tries to reproduce the issue). If I switch it back to CPU lightmapper the problem comes back. Loading the gaming scene on its own with CPU lightmapper never produces a problem, and the message that I normally see after the one above is:

    LightingSettings: switching bake backend from 2 to 1.

    Selecting CPU lightmapper is impossible on Apple silicon in Unity Editor and the only reason I have that setting set is because development started on intel based Mac. I believe that when the game scene is being loaded the Lighting settings switch causes some kind of race condition in Unity and that crashes the Editor. Because that setting is not really possible on Apple silicon (unless you go through upgrade path I did) it was probably never tested.

    In conclusion, I think that perhaps you had a setting that changed between upgrades that was causing some type of race condition as well (obviously not the same problem that I had). I know this is grasping at straws but I figured if someone has the same problem as me they might come across this answer to help them resolve the crash. Also it might help in finding the root cause of this if it still exists.
     
    Errawr likes this.