Hi guys,
For couple of weeks I’m facing with problem with loading/unloading scenes in Unity.
My main idea is to make possible fast transition between two levels/scenes.
I’m having Main menu scene and couple of Level scenes. My approach was to use
All works perfect BUT, when I want to unload loaded scene and load new one it doesn’t work.
For example:
I’m playing Level1.
Next loaded scene is Level2, but I “died” in Level1.
I want to unload Level2 and load MainMenu.
Currently I’m stuck on Level2 and I see that both Level3 and MainMenu are in memory but none of them is activated.
I’ve put demo project in the attachment, which explains my problem, if you have any suggestion or maybe some different approach it’ll be helpful.
Uff, now I’m trying different approach using LoadSceneMode.Additive. But still I’m having problem. I have 2 scenes loaded but none of them is activated. This is part of the code I’m using:
public void LoadNewScene() {
Debug.Log("Loading new scene");
LoadScene("AreaB");
}
IEnumerator LoadSceneLoop(string scene) {
async = SceneManager.LoadSceneAsync(scene, LoadSceneMode.Additive);
async.allowSceneActivation = false;
async.priority = 1;
yield return new WaitForSeconds(2f);
Debug.Log("Oops I died load new scene");
async1 = SceneManager.LoadSceneAsync("BeginningScene", LoadSceneMode.Additive);
async1.allowSceneActivation = false;
async1.priority = 10;
yield return new WaitForSeconds(1f);
Debug.Log("Load new scene");
async1.allowSceneActivation = true;
}
Anyone have suggestion on how to resolve this? Basically I need to make fast transition between 2 scenes without waiting.
Now if you want to start loading but then only switch when you’re ready (say as after a fade is completed), you can create the AsyncOperation as a class member and check it’s progress. If the progress is >= 0.9 then you can set the boolean to true to finish the load.
void Update()
{
if(m_AsyncLoader != null)
if (m_AsyncLoader.progress >= 0.9f)
{
//Scene loaded! Do fades then set allowSceneActivation to true when done to switch scenes.
m_AsyncLoader.allowSceneActivation = true;
}
}
IEnumerator CoLoadNextScene()
{
m_AsyncLoader = SceneManager.LoadSceneAsync(1);
m_AsyncLoader.allowSceneActivation = false;
yield return m_AsyncLoader;
}
Also note that if you just set the scene activation to false as shown below here, it will never load the scene (it’ll just load it to 90% then not switch).
Hi Josh
thanks a lot for your answer! It helped me to understand better how async loading works in Unity. I’ll just have additional question.
My biggest problem is with multiple async loaded scenes. Example will better describe my problem:
I’m playing Level1.
At the beginning of Level1 I am asynchronously loading next level - Level2. allowSceneActivation is set to false
When I finish Level1, I’m setting allowSceneActivation to true, so I’m immediately switched to next scene.
This scenario works perfect but when I “die” in Level1 I want to load asynchronously MenuScene and immediatelly go into that scene. Here is part of my code.
AsyncOperation m_AsyncLoader1;
AsyncOperation m_AsyncLoader2;
public bool isLoadingBeginningScene = false;
public IEnumerator CoLoadNextScene()
{
nextSceneName = playingLevels[currentLevel].levelName;
currentLevel++;
m_AsyncLoader1 = SceneManager.LoadSceneAsync(nextSceneName, LoadSceneMode.Single);
m_AsyncLoader1.allowSceneActivation = false;
yield return m_AsyncLoader1;
}
public IEnumerator CoLoadMenuScene() {
m_AsyncLoader2 = SceneManager.LoadSceneAsync(0,LoadSceneMode.Single);
m_AsyncLoader2.allowSceneActivation = false;
yield return m_AsyncLoader2;
}
public void Activate() {
m_AsyncLoader1.allowSceneActivation = true;
}
public void Activate2() {
m_AsyncLoader2.allowSceneActivation = true;
}
void Update(){
if (m_AsyncLoader1 != null && !isLoadingBeginningScene)
{
if (m_AsyncLoader1.progress >= 0.9f)
{
//Scene loaded! Do fades then set allowSceneActivation to true when done to switch scenes.
}
}
else if(m_AsyncLoader2 != null && isLoadingBeginningScene) {
if (m_AsyncLoader2.progress >= 0.9f)
{
//Scene loaded! Do fades then set allowSceneActivation to true when done to switch scenes.
Debug.Log("Scene loaded Menu screen");
}
}
}
As i Figured out value of variable m_AsyncLoader2.progress is always 0 although I called method CoLoadMenuScene() which loads beginning scene.