Search Unity

Asynchronous Loading?

Discussion in 'Scripting' started by kinguhdahood5, Apr 15, 2019.

  1. kinguhdahood5

    kinguhdahood5

    Joined:
    Dec 18, 2018
    Posts:
    84
    So I am curious on how I can fix this. I'm trying to async load my scenes/levels and it works but it lags due to it loading the scene in one frame.

    I've looked this up for a while, and it seems as though it loads some of the level in the background but loads all OnAwake and Start calls into one frame? I'm not entirely sure but I'm assuming that's what's happening. Unfortunately, all the resources I've looked up, none have a definite solution to the lag spikes.

    Back to me being curious, if I separated my level into a few scenes and loaded each scene by itself in it's own frame, and then merge those scenes into one, will that give me a smoother transition or will that give me more problems?
     
  2. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    In unity asyc load means read all bytes from disk on another thread. Your awakes and starts are run next frame after all data is loaded and they must run on main thread, so there's no way to run them in a background thread. But you may use coroutines to smudge the initialization for few frames.
     
  3. kinguhdahood5

    kinguhdahood5

    Joined:
    Dec 18, 2018
    Posts:
    84
    Thanks for replying! So I am using a script with coroutines that starts the loading process after I've faded into the loading screen. Here's the code:

    Code (csharp):
    1.  
    2.     public GameObject fadeOut;
    3.  
    4.     void Start ()
    5.     {
    6.         StartCoroutine(waitForProgram());
    7.     }
    8.  
    9.     IEnumerator waitForProgram()
    10.     {
    11.         yield return new WaitForSeconds(1);
    12.         StartCoroutine(LoadAsyncScene());
    13.     }
    14.  
    15.     IEnumerator LoadAsyncScene()
    16.     {
    17.         AsyncOperation asyncLoad = SceneManager.LoadSceneAsync(Globals.LevelOn);
    18.         asyncLoad.allowSceneActivation = false;
    19.         yield return (asyncLoad.progress > 0.9f);
    20.         fadeOut.SetActive(true);
    21.         StartCoroutine(loaded(asyncLoad));
    22.     }
    23.  
    24.     IEnumerator loaded(AsyncOperation sync)
    25.     {
    26.         yield return new WaitForSeconds(1);
    27.         sync.allowSceneActivation = true;
    28.     }
    29.  
    This is what I have so far in my loading script, the 'waitForProgram' and 'loaded' coroutines are for creating the proper transitions of fading in and then fading out. 'LoadAsyncScene' is the function I'm targeting in terms of the loading functionality. This was tested and works but it does persist with the lag during load. So when you referred to using coroutines to stretch out the initialize process, what could I improve or input to achieve that?
     
    Eloren likes this.
  4. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    I mean, if you have 1000 objects initializing in one frame and it causes the lag, then you may use a coroutine to initialize, say, 10 of them per frame so they won't hurt performace badly.
     
  5. Moreus

    Moreus

    Joined:
    Oct 25, 2018
    Posts:
    5
    asyc is not multi threading, is loading on one tread asynchronous and not blocking main thread