- Home /
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. :)
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.
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.
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);
}
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
Your answer
Follow this Question
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