Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Loading scene starts only after large second scene is loaded

Discussion in 'Scripting' started by Hermonirr, Dec 1, 2019.

  1. Hermonirr

    Hermonirr

    Joined:
    Dec 23, 2013
    Posts:
    56
    Hi,

    I have two scenes:
    1. A small loading scene with a background image and a progress slider
    2. A large game scene with many textures and gameobjects.

    The loading scene has this script:

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5. using UnityEngine.UI;
    6.  
    7. public class LoadSceneAsync : MonoBehaviour
    8. {
    9.     public Slider slider;
    10.     public int sceneIndex;
    11.     void Start()
    12.     {
    13.             // Use a coroutine to load the Scene in the background
    14.             StartCoroutine(LoadSceneAsyncMethod());
    15.     }
    16.  
    17.     IEnumerator LoadSceneAsyncMethod()
    18.     {
    19.         // The Application loads the Scene in the background as the current Scene runs.
    20.         // This is particularly good for creating loading screens.
    21.         // You could also load the Scene by using sceneBuildIndex.
    22.         AsyncOperation AO = SceneManager.LoadSceneAsync(sceneIndex,LoadSceneMode.Additive);
    23.         AO.allowSceneActivation = false;
    24.         while(AO.progress < 0.9f)
    25.         {
    26.             slider.value = AO.progress;
    27.             yield return null;
    28.         }
    29.  
    30.         AO.allowSceneActivation = true;
    31.  
    32.     }
    33. }
    The problem is that the loading scene does not show until the 2nd scene is fully loaded.
    I get a black screen for a long time, then the loading scene appears for a very short while and then the game starts.

    Thanks.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,517
    Scenes don't load until the end of frame, so you are saying "load the loading scene and then load the next scene in start," which gives the first scene no time to load.

    Instead, you can actually make Start() into a Coroutine, which can be handy. Do this, and then insert a yield return null before starting the new loader:

    Code (csharp):
    1. IEnumerator Start()
    2. {
    3.   yield return null;
    4.   StartCoroutine( LoadSceneAsyncMethod());
    5. }
    Scene loading timing details take some brain-bending to really become comfortable with, but once you do it, it lets you break your game into lots of smaller simple scenes and load each one separately, such as having a player control scene, a UI scene, a weapon scene, and content scenes (level data)... then you can make a manager that loads them all additively and break your project into nicely-manageable chunks, kind of like how you broke out the Loading scene above.
     
    Hermonirr likes this.
  3. Hermonirr

    Hermonirr

    Joined:
    Dec 23, 2013
    Posts:
    56
    Thanks! Works great.
     
    Kurt-Dekker likes this.