Search Unity

Slow Loading Time - Scene Loading Async

Discussion in 'Scripting' started by el_Guero, Jun 15, 2021.

  1. el_Guero

    el_Guero

    Joined:
    Sep 15, 2017
    Posts:
    185
    So I'm trying to load this scene during a Loading Scene:

    Screen Shot 2021-06-14 at 17.48.35.png

    On my low end Android device it takes around 9 seconds to load the scene. How can I find out what causes this delay?

    Because when I add to this time the duration of the Unity Splash screen and an additional 2 second black screen I get added for unknown reasons, I get a total loading time of 18-19 seconds which is simply too much.

    If I Load this main scene directly from the very startup of the app, the total Loading time is about 11 seconds only (8 seconds faster!!!) but the problem is that 7 out of these 11 seconds is a black screen that makes the game seem to be completely crashed/freezed (the other 4 seconds are for the Unity Splash screen).

    On iOS testing on a iPhone SE I get much faster loading time of approximately 5-6 seconds only. On a iPhone 11 Pro it loads instantly.

    Now you might think the problem is my Android device, but I compared this to a colleague who uses Playmaker to make his games (like I do) and his games load at my Android device within 4-5 seconds. I also tested a complete empty Unity project and it loads instantly.

    Any help is appreciated, I am trying to resolve this for the last 2 months and it literally drives me nuts.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    The reference way to investigate performance issues is to attach the profiler (Window -> Analysis -> Profiler).

    Another tried and true way to do things is to start wholesale ripping chunks out of that scene to find out what is taking so long. It doesn't even matter if the scene is runnable; just find out what's taking it so long to load.

    This latter approach is often fastest, but it presupposes you are using proper source control at all times, which means you can instantly and trivially reset all investigatory damage you inflict on your project when you are done experimenting.

    If you are NOT using source control, you should IMMEDIATELY begin doing so, assuming that you value your hard-earned work.

    Personally I use git because it is free and there are tons of tutorials out there to help you set it up.

    As far as configuring Unity to play nice with git, keep this in mind:

    https://forum.unity.com/threads/prefab-links-keep-getting-dumped-on-git-pull.646600/#post-7142306

    Here's how I use git in one of my games, Jetpack Kurt:

    https://forum.unity.com/threads/2-steps-backwards.965048/#post-6282497

    Using fine-grained source control as you work to refine your engineering:

    https://forum.unity.com/threads/whe...grammer-example-in-text.1048739/#post-6783740

    Share/Sharing source code between projects:

    https://forum.unity.com/threads/your-techniques-to-share-code-between-projects.575959/#post-3835837

    Setting up the right .gitignore file:

    https://forum.unity.com/threads/removing-il2cpp_cache-from-project.1084607/#post-6997067

    Generally setting Unity up (includes above .gitignore concepts):

    https://thoughtbot.com/blog/how-to-git-with-unity
     
    Claytonious, Ultroman and tmonestudio like this.
  3. el_Guero

    el_Guero

    Joined:
    Sep 15, 2017
    Posts:
    185
    Thanks.
    I use Sourcetree with Bitbucket. So no issues there.

    I also already ripped my project apart and deleted all plugins, all UI, all models, all prefabs. None really improved heavily the loading time.

    Getting rid of third party Monetization and analytics SDKs including MAX, FB, GA and Google Version Handler improved it about 1 second.

    Same for deleting all UI canvases. But the loading time as well as the black screen is still there noticeably.

    There must be something that creates that bottleneck, but plugins/sdks seems not to be responsible for it.

    How can I use the profiler when it comes down to loading time?
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Well the first thing to do is try and establish a baseline: make a nearly-blank empty scene and rebuild with that as your first scene. However fast that loads on your low-end phone is going to be your best possible result.

    If you are only 1 second slower with your own game, there's little you can do. But if an empty scene loads fast, you have some room to do more investigating.

    It's less useful for one-off things like this, as in it would be faster to pare things down in a binary fashion to isolate what is the offending system.
     
  5. It's hard to tell without profiling, so you really should do that, profile on device with build. But my best guess is that you have heavy initialization. Right after load (covered by splash screen). If you can thin it out, run your initialization in multiple frames, that can help, or stick an animated loading screen on it and sit it out. You can only do so much on low end devices, where memory, memory bandwidth, CPU, SD card bandwidth are all super low... But profile first, make sure you know what is happening and then you can either try to optimize things or hide it behind the loading screen.
     
  6. el_Guero

    el_Guero

    Joined:
    Sep 15, 2017
    Posts:
    185
    The loading scene I use is pretty much an empty scene with a logo and the logic to load the main scene async. So I just go straight into it, have a 0.1 second delay and then load.

    I read everywhere that this is the fastest way and in many places they say that this way even the black screen is gone. Not in my case though as described above. Still a 1-2 second black screen and the scene itself takes much more time to load. Isn’t that against all logic?

    Loading the main scene directly is faster in total duration but with a longer black screen.

    If i only take the time for the empty scene to load it’s about 5 seconds. I would be happy with that if I could bring it down to this for sure but that doesn’t get me to the main scene doesn’t it?

    I tried to check the profiler but can’t see anything really either. Or not that I would understand.

    I guess at that point I even would hire someone who can help me because I really don’t know where to take it from here.
     
  7. el_Guero

    el_Guero

    Joined:
    Sep 15, 2017
    Posts:
    185
    Ps: I also now moved some of the initialization into the loading scene. Like GameAnalytics, Easy Mobile Pro or Dotween. That didn’t change anything though. The main scene still takes the same time to load.
     
    essorgames likes this.
  8. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,735
    Have you got some large sound files in your project or other assets that are flagged to be preloaded? I've found that this can cause a delay with a blank screen before the initial scene loads.
     
    SkygodGames likes this.
  9. el_Guero

    el_Guero

    Joined:
    Sep 15, 2017
    Posts:
    185
    Not sure how I can flag Assets for this but no. No audio files in the project. And textures I only have one atlas for the UI elements.

    What I think is weird is that my colleagues’ games do have zero black screen while mine does have with or without that loading screen. His games only use one main scene and the game still loads immediately.

    So yes, my problem is probably that black screen in the first place. If I can resolve that one I might resolve the loading time for the scene as well.

    What are the reasons to get a black/blank screen at all?
     
  10. el_Guero

    el_Guero

    Joined:
    Sep 15, 2017
    Posts:
    185
    So I took my project completely a part (deleting everything I can individually and testing if I can reduce the loading time) but nothing works really. The only way I can get a scene show fast is when I use an empty Loading Scene (without Playmaker) and only a logo on a Canvas and use the async loading through a c# script I found online:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class LoadSceneAsync : MonoBehaviour
    7. {
    8.     private AsyncOperation _asyncOp;
    9.  
    10.     // Start is called before the first frame update
    11.     void Start()
    12.     {
    13.         StartCoroutine(AsynchronousLoad(1));
    14.     }
    15.  
    16.     private IEnumerator AsynchronousLoad(int scene)
    17.     {
    18.         yield return new WaitForSeconds(0.1f);
    19.  
    20.         Application.backgroundLoadingPriority = ThreadPriority.High;
    21.  
    22.         _asyncOp = SceneManager.LoadSceneAsync(scene);
    23.         _asyncOp.allowSceneActivation = false;
    24.         while (!_asyncOp.isDone)
    25.         {
    26.             if (Mathf.Approximately(_asyncOp.progress, 0.9f))
    27.             {
    28.                 _asyncOp.allowSceneActivation = true;
    29.             }
    30.             yield return null;
    31.         }
    32.  
    33.         Application.backgroundLoadingPriority = ThreadPriority.BelowNormal;
    34.  
    35.         yield return new WaitForEndOfFrame();
    36.         yield return null;
    37.     }
    38. }
    Doing that, the first scene opens immediately after Unity Splash screen. Meaning, the black screen is gone for good. With no other setup I have achieved that. So not sure if Playmaker is (at least part of it) the culprit. Because it seems as soon as I have a direct reference to the main scene, even with a dontDestroyOnLoad for a gameobject, that black screen is back.

    The real problem I have though is that the async loading of the main scene takes ages if done like that. Meaning the black screen is still the fastest way to load the game. And that sucks.

    Seems the part that takes the most time is when the loading progress gets to 0.9. I guess this is when the scene gets activated?

    Anyways, would it help if I posted here the logcat so anyone can take a look? I highly would appreciate this. Or I also can hire someone to help me debug this if someone is up to it. Just let me know.
     
  11. Canoide

    Canoide

    Joined:
    Feb 24, 2014
    Posts:
    11
    Hello, did you solve the slow loading of the scene scene? The same thing happened to me when I updated unity to 2021.2
     
    amisner2k likes this.
  12. amisner2k

    amisner2k

    Joined:
    Jan 9, 2017
    Posts:
    43
    I recently started porting a project to version 2021.2 (currently on 2021.2.10) and came here wondering if I had missed something because I too noticed a longer than normal 5-6 second black screen before my initial scene appears. I wonder if anyone else is noticing this with 2021.2.

    I tried setting my initial scene to be a basic scene with just a Camera and Directional Light and setting it to be the only scene in the build and it's about 5 seconds of a black screen after the Unity Logo splash animation before I see the scene appear.

    Update: I realized this delay was only happening when using SRP (HDRP or URP) as the render pipeline. When I switched to using the Standard pipeline, the delay went away.
     
    Last edited: Feb 18, 2022
  13. ConorsFlying

    ConorsFlying

    Joined:
    Apr 26, 2018
    Posts:
    8
    I have the same problem on the same version. Longer initial load + longer load times in general. Back to Unity 2020 I go...
     
  14. Canoide

    Canoide

    Joined:
    Feb 24, 2014
    Posts:
    11
    My solution was to pass all scenes except the main scene to addresasble, loading with Addressables.LoadSceneAsync. It reduced the loading time
     
  15. elDiablik

    elDiablik

    Joined:
    Oct 25, 2021
    Posts:
    4
    Have a same situation with URP - testing on old Samsung a520(2017), when loading game 3-4 second, then goes logo and after that 9-10 second black screen and only then menu scene loads, (sound initializes a bit faster - 2 sec before menu shows up)
     
  16. CassClay

    CassClay

    Joined:
    Mar 21, 2020
    Posts:
    71
    In my case the problem was loading of a 45mb ambient track for my android game. Right click on properties of the track, changing Load type to Streaming and quality to 50% solved it for me!
     
    simaxiom and Whatever560 like this.
  17. seojihyuk

    seojihyuk

    Joined:
    Sep 19, 2014
    Posts:
    30
    I have similar problem. And it seems related to URP, DOTS World, and other packages loading before scene loaded. cause my project is using DOTS, I can't switch to Standard pipeline.
    I've watched profiler and found out that first 1 frame took 58 sec.
    Can I put loading scene before DOTS default world and other package loading?
    (loading scene has only MonoBehaviour for loading and UI using UIToolKit, therefore it doesn't need ECS package at the loading scene).
    I left you zip file of my first 1 frames profile picture. 1frame.png
     
  18. CassClay

    CassClay

    Joined:
    Mar 21, 2020
    Posts:
    71
    TypeManager is part of ECS that wasn't a thing when this thread started consider making new one but TypeManager is part of it from what I see, never used it, and it's eating big chunk of that initialization time.