- Home /
Unloading a loaded Scene when load Async for a Loading Bar
Hey Community,
got a problem about Loading and Unloading Scenes
My approach ist to make a Loading Scene. In this Loading Scene you can load various Levels.
But in my Case i cannot Unload the Level while it isLoading
public void LoadLevel(string sceneIndex)
{
//Stop Old Coroutine
StopCoroutine("LoadAsynchonously");
//Reset everything
ResetLoad();
//Check in what Level we are and if we are loading a Level then we want to Unload the Level
if (load == "LevelThree" && load != "0" || load == "LevelTwo" && load != "0")
{
Debug.Log("Unload " + load);
if (load == "LevelTwo")
{
SceneManager.UnloadSceneAsync("Scenes/LevelTwo.unity");
}
else
{
SceneManager.UnloadSceneAsync("Scenes/LevelThree.unity");
}
}
else if (load == "LevelThree" && load != "0" || load == "LevelOne" && load != "0")
{
Debug.Log("Unload " + load);
if (load == "LevelOne")
{
SceneManager.UnloadSceneAsync("Scenes/LevelOne.unity");
}
else
{
SceneManager.UnloadSceneAsync("Scenes/LevelThree.unity");
}
}
else if (load == "LevelTwo" && load != "0" || load == "LevelOne" && load != "0")
{
Debug.Log("Unload " + load);
if (load == "LevelOne")
{
SceneManager.UnloadSceneAsync("Scenes/LevelOne.unity");
}
else
{
SceneManager.UnloadSceneAsync("Scenes/LevelTwo.unity");
}
}
//Set the new Level
load = sceneIndex;
//Start Coroutine to load the new Level
StartCoroutine("LoadAsynchonously", load);
}
void ResetLoad()
{
//Reset everyting in Scene
loadingScreen.SetActive(false);
operation = null;
activateLevelButton.gameObject.SetActive(false);
slider.value = 0;
progressText.text = "0%";
}
IEnumerator LoadAsynchonously(string sceneIndex)
{
//Set the Loading Bar and UI Active
loadingScreen.SetActive(true);
//Get the Operation
operation = SceneManager.LoadSceneAsync(sceneIndex);
//Make the Operation not complete by his Own so User can Decide when to start
operation.allowSceneActivation = false;
//While Loading it chance the UI
while (!operation.isDone)
{
float progress = Mathf.Clamp01(operation.progress / .9f);
Debug.Log(progress);
slider.value = progress;
progressText.text = progress * 100f + "%";
if (progress > .9f)
{
activateLevelButton.gameObject.SetActive(true);
}
//return Null ist finish
yield return null;
}
}
public void ActivateLevel()
{
//User can activate the new Level
operation.allowSceneActivation = true;
}
So my main problem is when the "User" Decides to load Level One and then he Change his mind and say i want load Level Two, he will Press the Button for Level Two and the System load the Second Level but never finish it and show the Button to Start the Level.
Also the first loaded Scene is still active in the hierarchy.
Here are Pictures:
My first workaround was to make a Back Button and also disable the Panel with the Loading Buttons for Levels.
This prevent the User from loading another Scene Async.
So he need to decide if he want to Play the Level or want go back. In Case he press the Back Button then the whole LoadingScene gets reloaded and he is again able to Choose a Level.
But i want to archive this without reloading the LevelLoading Scene. Someone got an Idea? Thanks for Help and Hints.
Answer by hectorux · Nov 12, 2018 at 12:09 AM
You could check if other scene is active when you select to load other scene, there exist SceneManager.GetActiveScene("Name"/int Index); , with this you can now unload the existing scene if it exist and dont match with the current scene to load
Your answer
Follow this Question
Related Questions
SceneManager.LoadSceneAsync freeze loading scene in editor. 0 Answers
Save & Load Game question 3 Answers
LoadLevel takes more time : Unity 0 Answers
heavy scene loading 1 Answer
Asyncchronous image loading 2 Answers