Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Scene loading time is too much(around 3 seconds)

Discussion in 'Scripting' started by phantom_x404, May 1, 2020.

  1. phantom_x404

    phantom_x404

    Joined:
    Apr 26, 2020
    Posts:
    26
    I have made an infinite runner game with three levels, easy medium and hard, and there is a menu from which you can start and choose levels, the easy and medium have quick loading times however the Hard Level takes a longer time to load(around 3 seconds), so I tried to use animation and Couroutines to fix this but it still lags, the code is given below:
    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3. using UnityEngine.SceneManagement;
    4. using UnityEngine.UI;
    5.  
    6. public class Level3Loader : MonoBehaviour
    7. {
    8.  
    9.     public Animator transition;
    10.     public float transitionTime = 1f;
    11.  
    12.     public void LoadLevel3(int sceneIndex) //means it requires input of int and it shud be a scene index
    13.     {
    14.         StartCoroutine(LoadAsynchronously(sceneIndex));
    15.  
    16.     }
    17.     IEnumerator LoadAsynchronously(int sceneIndex)
    18.     {
    19.         //play animation
    20.         transition.SetTrigger("Start");
    21.  
    22.         //wait
    23.         yield return new WaitForSeconds(transitionTime);
    24.  
    25.         //load scene
    26.         SceneManager.LoadScene(sceneIndex);
    27.     }
    28.  
    29. }
    This is the level loader code, and we have to add transition to this, I added this to the HARD LEVEL button so that whenever the button is clicked, it should preload the level while showing the animation, and then instantly show the game scene, but it still takes around 3 seconds to load, and only after 3 seconds does it show the end animation(fade out), also, it does not show the start animation at times. Any help on how to reduce loading times or make the animation right?
     
  2. sbalanoff

    sbalanoff

    Joined:
    Nov 14, 2019
    Posts:
    36
  3. phantom_x404

    phantom_x404

    Joined:
    Apr 26, 2020
    Posts:
    26
    Hey, I did that, it is actually: transition.speed = transitionTime;
    because the Animator is called transition, but it still takes a lot of time to load up and only shows the ending animation, i.e. the fade out on the start of the level, any idea on what to do next? since transition speed did not work :-/
     
  4. sbalanoff

    sbalanoff

    Joined:
    Nov 14, 2019
    Posts:
    36
    How big is your next scene? If it is something that is a little bigger you may need a loading screen to keep the player interested and not looking at white screens :)

    Another thing is that if you have set the animator speed and you're running a courotine that is waiting for that time its going to be double as long right?
     
  5. phantom_x404

    phantom_x404

    Joined:
    Apr 26, 2020
    Posts:
    26
    well, I tried setting up a loading screen,
    but, the level hard again takes a really long time to load up(around 3 seconds), while the others load instantly, so I set up a loading screen which is displayed synchronously while the game scene is loading up. In the loading screen there is a loading bar and that is pretty much it, the loading bar fills up as the screen loads.
    The loading also works perfectly for Easy, and Medium, but for some reason, whenever I click on the HARD LEVEL, it lags for around a second(stays in main menu), then goes into loading screen with the loading bar directly at 100%, and then it again lags for around a second before starting the game. I have tried to implement this Loading screen on the RESTART button for the HARD LEVEL also, and for the restart button, it does not even display it, it just lags for a second and directly takes me to the level. Again, this only happens for the HARD LEVEL.
    The code for the loading screen is below:
    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3. using UnityEngine.SceneManagement;
    4. using UnityEngine.UI;
    5.  
    6. public class LevelLoader : MonoBehaviour
    7. {
    8.  
    9.     public GameObject loadingScreen;
    10.     public Slider slider;
    11.     public Text progressText;
    12.  
    13.     public void LoadLevel(int sceneIndex)
    14.     {
    15.         StartCoroutine(LoadAsynchronously(sceneIndex));
    16.        
    17.     }
    18.  
    19.     IEnumerator LoadAsynchronously(int sceneIndex)
    20.     {
    21.         AsyncOperation operation = SceneManager.LoadSceneAsync(sceneIndex);
    22.  
    23.         loadingScreen.SetActive(true);
    24.  
    25.         while (!operation.isDone)
    26.         {
    27.             float progress = Mathf.Clamp01(operation.progress / 0.9f);
    28.            
    29.             slider.value = progress;
    30.             progressText.text = progress * 100f + "%";
    31.  
    32.             yield return null;
    33.         }
    34.     }
    35.    
    36. }
    37.  
    any idea what to do next ? :-(
     
  6. sbalanoff

    sbalanoff

    Joined:
    Nov 14, 2019
    Posts:
    36
    Ok, I'd try using a profiler to see what is lagging your game
     
  7. phantom_x404

    phantom_x404

    Joined:
    Apr 26, 2020
    Posts:
    26
    Thanks, let me try that out