Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • 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
0
Question by adsilcott · Apr 11, 2018 at 12:38 AM · scenescene-loadingscene-switching

It's impossible to async load a scene then have it wait while unloading another?

I'm updating my scene management code, and I thought it would be good to start asynchronously loading the next scene while the current one ends, but I've encountered a problem.

For context, I've got scriptable objects that hold scene info and transitions, and I'm doing all of this from a manager scene that stays loaded.

Here's what I had in mind:

 SceneManager.SetActiveScene(managerScene.scene);
 AsyncOperation newSceneLoad = SceneManager.LoadSceneAsync(newScene.scene.buildIndex, LoadSceneMode.Additive);
 //This keeps it from activating while the first scene finishes
 newSceneLoad.allowSceneActivation = false;
 
 //Fade out the current scene    
 yield return currentScene.EndScene();
                 
 AsyncOperation unload = SceneManager.UnloadSceneAsync(currentScene.scene.buildIndex);
 yield return unload; //Should wait for the first scene to unload...
 
 newSceneLoad.allowSceneActivation = true; //This line won't work here...
 yield return newSceneLoad;
 
 //Fades in    
 yield return newScene.StartScene();


Here I'm using UnloadSceneAsync because UnloadScene is depreciated, but it doesn't work when used together with LoadSceneAsync. On this page it says: https://docs.unity3d.com/ScriptReference/AsyncOperation-allowSceneActivation.html

if a LoadSceneAsync.allowSceneActivation is set to false, and another AsyncOperation (e.g. SceneManager.UnloadSceneAsync ) is initialized, the last operation will not be called before the first allowSceneActivation is set to true.

That means I have to move the "allowSceneActivation = true" line up above the "yield return unload" line for unload to continue. But that means the scenes will overlap-- the first one will finishing loading before the second one is removed. And indeed when I do that I get some "There are 2 audio listeners in the scene" warnings, since the cameras for both scenes are present for a few frames.

So although it makes sense that Async loading could be used like this, it doesn't seem to be possible. Can anyone confirm this or let me know a different way to do it?

Comment
Add comment · Show 2
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 sp-LeventeLajtai · Feb 21, 2020 at 07:51 AM 0
Share

Hi! Why do you yield return the unload AsyncOperation? What if you waited for it to complete using while(unload.progress < 0.9 || !unload.isDone) { yield return null; }?

avatar image rh_galaxy · Feb 21, 2020 at 05:29 PM 0
Share

To properly do fading between scenes I ended up having the camera and AudioListener as a Do not destroy that lives outside the scenes, controlled by a Game$$anonymous$$anager. (But this doesn't solve your problem now, just a tip, but there are probably many ways to do it).

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Greyborn · Feb 27, 2020 at 10:12 AM

@adsilcott, I was trying to do exactly as you were:

—

  1. Load the new scene (but don't allow it to activate on load).

  2. Unload the old scene.

  3. Activate new scene.

—

... and I was experiencing the same issue with the AsyncOperation being stalled due to allowSceneActivation being false.

—

My solution was to destroy all root objects on the old scene, activate the new scene, and then unload the old scene:

—

  1. Load the new scene (but don't allow it to activate on load).

  2. Destroy all root objects on the old scene

  3. Activate new scene.

  4. Unload the old scene.

—

This allows the old scene to unload, because the new scene is no longer blocking the AsyncOperation queue. And the old scene objects being destroyed prevents the duplicate objects, such as audio listeners.

—

For the record, I'm reloading a scene (Additive) layer. If I were to unload the old scene before loading the new scene, Unity would release scene resources only to immediately reload them, increasing load times. However, loading the new scene while the old scene is loaded allows Unity to skip loading certain assets that exist in the old scene, which speeds up load times.

—

I'm also using the new Addressables.LoadSceneAsync(assetReference, mode, activateOnLoad) instead of SceneManager.LoadSceneAsync() methods, but I expect they should work the same way. I am unloading the old scene with SceneManager.UnloadSceneAsync(scene).

—

Anyhow, I hope that helps anyone interested in cleanly loading or reloading a scene without overlapping an old scene that should be replaced.

Comment
Add comment · Show 1 · 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 adsilcott · Mar 05, 2020 at 01:08 AM 1
Share

It's been about two years since I wrote that question, and while I've long moved on from the approach I was working on, this seems like a good answer. I may go back and try to rewrite my previous code based on this. Thanks!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

95 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

Related Questions

,Enums not being passed between scripts properly 1 Answer

Load Scene from .unity File 1 Answer

How can I have several scenes running at the same time? 1 Answer

SceneManager.GetAllScenes() only returns the current scene 3 Answers

Re-loading a scene but on the background older scenes are displayed 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