• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by s1m1ng7on · Dec 27, 2021 at 07:24 PM · scenescene-loadingscene-switchingscenesloading screen

How can I make a loading screen with LoadSceneAsync

Hello everyone, I have three scenes in my project - one for the main menu, one for the map and the game itself and one for the loading screen between the game and the main menu. I'm trying to make it so when I hit the button Play on the main menu, the scene switches to the loading screen and a script attached to the camera immediately starts loading the scene with the map with LoadSceneAsync.

When I try the game, I expect to switch to the loading screen scene after i hit the button Play on the main menu. Then the loading screen scene should stay on until the playable scene with the map loads and switches.

Unfortunately, I get completely different results - after I hit Play, the game freezes (as it does with the normal LoadScene function) until the scene with the map loads. Then it switches to the loading screen scene for a second and then to the scene with the map.

This is the code that is compiled when the button Play on the main menu is pressed:

     public void Play()
     {
         SceneManager.LoadScene("LoadingScreen");
         PresenceManager.UpdatePresence(detail: "Exploring Ancient Greece",start: System.DateTimeOffset.Now.ToUnixTimeSeconds());
         Cursor.visible = false;
     }

This is the script attached to the camera in the loading screen scene which responds for loading the map scene with LoadSceneAsync:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.SceneManagement;
 
 public class LoadingLevel : MonoBehaviour
 {
     void Start()
     {
         SceneManager.LoadSceneAsync("SampleLevel");
     }
 }

Do you have any ideas on what I am doing wrong and how to fix it. Any help would be appreciated. :)

Comment

People who like this

0 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

  • Sort: 
avatar image

Answer by rh_galaxy · Dec 27, 2021 at 07:36 PM

LoadSceneAsync is a little more complicated I think to get right, here is a try:

 AsyncOperation asyncLoad;
 bool bLoadDone;
 IEnumerator LoadAsyncScene()
 {
     asyncLoad = SceneManager.LoadSceneAsync("SampleLevel", LoadSceneMode.Single);
     asyncLoad.allowSceneActivation = false;
     //wait until the asynchronous scene fully loads
     while (!asyncLoad.isDone)
     {
         //scene has loaded as much as possible,
         // the last 10% can't be multi-threaded
         if (asyncLoad.progress >= 0.9f)
         {
             asyncLoad.allowSceneActivation = true;
         }
         yield return null;
     }
     bLoadDone = asyncLoad.isDone;
 }
 
 bLoadDone = false;
 StartCoroutine(LoadAsyncScene()); //call to begin loading scene
 //wait for bLoadDone==true

I think you should have the loading screen implemented in the Menu-scene, hide the menu, then begin the LoadAsyncScene, then show the progress. Fewer scenes is a good thing in my view.


But you will not get totally smooth framerate during scene transitions/loading... It's the way things are, there will be freezes.

Comment
s1m1ng7on

People who like this

1 Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image s1m1ng7on · Dec 30, 2021 at 12:25 PM 0
Share

Hey, just implemented that code into Unity but the game still freezes and stays unresponsive until the scene activation phase begins. It's clear to me the LoadSceneAsync still won't get me smooth FPS during transitions but not freezing the whole scene is totally enough for me.

avatar image rh_galaxy s1m1ng7on · Dec 30, 2021 at 02:13 PM 0
Share

Try adding

 Debug.Log("LoadAsyncScene() is running " + Time.unscaledTime);
 before
 yield return null;

and add a similar print in your Update function that is supposedly frozen. You know that other Update code will not run until the new scene is loaded, only what you have as a Do not destroy object will run for sure.

Your game manager should have something like this in the startup, to make it singleton.

 public static GameManager instance = null;
 void Awake()
 {
     if (instance == null)
     {
         instance = this;
     }
     else if (instance != this)
     {
         //enforce singleton pattern, meaning there can only ever
         // be one instance of a GameManager.
         Destroy(gameObject); //<- this makes OnDestroy() be
         // called so be careful
         return;
     }
     //the rest is done once only...
     DontDestroyOnLoad(gameObject);
 }
 void Update()
 {
     Debug.Log("Update() is running " + Time.unscaledTime);
 }
avatar image

Answer by s1m1ng7on · Dec 30, 2021 at 12:26 PM

Hey, just implemented that code into Unity but the game still freezes and stays unresponsive until the scene activation phase begins. It's clear to me the LoadSceneAsync still won't get me smooth FPS during transitions but not freezing the whole scene is totally enough for me. @rh_galaxy

Comment

People who like this

0 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image

Answer by Speedomon · Dec 11, 2022 at 04:24 PM

Do you test it in the editor, or in a standalone build? The editor will produce very different results when it comes to scene loads.

Comment

People who like this

0 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

160 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

One Location Game (with Scenes Loaded and Disabled) 0 Answers

Change Scene keeping Character and GUI Canvas 1 Answer

Help with error message "Overwriting the same path as another open scene is not allowed" 0 Answers

Bolt Visual scripting and loading different scenes/levels 1 Answer

Load Scene from .unity File 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges