Search Unity

Question Switch between scenes without reloading

Discussion in 'Editor & General Support' started by lz7cjc, Feb 14, 2023.

  1. lz7cjc

    lz7cjc

    Joined:
    Sep 10, 2019
    Posts:
    540
    Hi
    My app is pretty large, and I have two scenes. The main guts of the app is all within a single scene (mainscene) where i show and hide different sections dependent on what is relevant. I have found this is far faster than creating multiple scenes reusing some rather large environment assets.

    I have recently had to create a second scene (videoscene ) to play videos as the package I purchaed won't work with this architecture

    This means when the video finishes playing i switch back to mainscene. Is there a way to keep mainscene e.g. cached/in memory/something similar so i don't experience such a long delay when switching back from the videoscene to mainscene

    thanks
     
  2. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,639
    The most obvious thing would to just leave the main scene loaded. I don't understand why you need to unload the main scene. Can you elaborate on that?
     
    lz7cjc and Kurt-Dekker like this.
  3. lz7cjc

    lz7cjc

    Joined:
    Sep 10, 2019
    Posts:
    540
    thanks for getting back to me
    i guess that is a reflection of my ignorance:confused:
    I thought I had to switch scenes. How do I toggle between the two and keep them both loaded? At the moment I use this
    SceneManager.LoadScene();

    How can i keep both scenes loaded?
     
  4. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,639
    You can actually have multiple scenes open at the same time in Unity. You just need to open them in additive mode:
    https://docs.unity3d.com/ScriptReference/SceneManagement.LoadSceneMode.Additive.html

    You can also open multiple scenes in the editor too, by right-clicking on the scene and selecting "open scene additive." Multiple scenes are useful for a lot of things.

    If you unload an additive scene, the assets used in that seen are not unloaded from memory, from what I understand. That video will probably take-up a big chunk of it, too. You may need to call Resources.UnloadUnusedAssets() additionally.
    https://docs.unity3d.com/ScriptReference/Resources.UnloadUnusedAssets.html
     
  5. lz7cjc

    lz7cjc

    Joined:
    Sep 10, 2019
    Posts:
    540
    ok thanks - i am only looking to do this in the runtime app - will take a look at the resources and see if i can work it out
    thanks for your help
     
  6. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780
    According to this thread they are unloaded, as long as they're actually 'unused' at that point: Question - Memory profiler; Textures remain in memory and point to references that have been destroyed? - Unity Forum

    Edit: Although the docs for UnloadAsync state: Note: Assets are currently not unloaded. In order to free up asset memory call Resources.UnloadUnusedAssets.
    Unity - Scripting API: SceneManagement.SceneManager.UnloadSceneAsync (unity3d.com)
    So, lets keep it at 'maybe'..
     
  7. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,456
    I didn't actually state that there. There was a separate parallel thread, where the user had added a call to Resources.UnloadUnusedAssets in the callback to the scene unload.
    SceneUnloads in Unity generally dont unload the unused assets from that scene (only the previous scene) unless explicitly asked to do so in the scene unload call.
     
    SF_FrankvHoof likes this.
  8. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,456
    Also, by default, only non-additive, destructive scene loads trigger Resources.UnloadUnusedAssets without explicitly specifying that. But they do so before they destroy everything in the scene so all assets still used then will not be unloaded.
     
    SF_FrankvHoof likes this.
  9. lz7cjc

    lz7cjc

    Joined:
    Sep 10, 2019
    Posts:
    540
    Hi - i have just found a problem with using LoadSceneMode.Additive and am hoping you can help
    I have a reticle pointer attached to my camera which is needed in both scenes.

    If I use LoadSceneMode.Additive then both scenes are active and the reticle pointer doesn't appear in the newly loaded scene, although i am getting the correct camera view. Do you have any suggestions on how i can fix this?

    thanks