Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

SceneManager: activating and deactivating a scene

Discussion in 'Scripting' started by rcenzo, Aug 15, 2017.

  1. rcenzo

    rcenzo

    Joined:
    Aug 21, 2014
    Posts:
    13
    Hi,

    I'm currently looking into building an application which uses several scenes as different sections of the app, which are switched at appropriate moments within the run of a single app.

    When switching from, let's assume scene A to B, a significant freeze occurs on loading the second scene. I know this can be worked around using SceneManager.LoadSceneAsync(...), and making sure scene B is ready at the time the user performs a specific action (holding off using AsyncOperation.allowSceneActivation). This reduces the freeze time to practically zero, and that's good.

    However, for my situation, I want the user to be able to go back to scene A, select a different "item", and display said item in scene B again. This currently unloads scene B, reloads scene A, and prepares scene B again, just to unload A when the user performs an action. It seems incredibly redundant.

    I wonder if there is a way to revert a scene to the state where it's waiting for the user to perform an action. Such as, the scene is deactivated in the app, but not unloaded, in case the user wants to re-instantiate it. Does anyone have an idea regarding this conundrum?

    rcenzo
     
  2. TaleOf4Gamers

    TaleOf4Gamers

    Joined:
    Nov 15, 2013
    Posts:
    825
    You can have multiple scenes loaded at the same time using LoadSceneMode.Additive or along those lines:
    https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.LoadScene.html
    Simply active the scene you need and unload it if it is no longer needed.
     
  3. rcenzo

    rcenzo

    Joined:
    Aug 21, 2014
    Posts:
    13
    Thanks for your reply, but I'm kinda looking for a single-scene load mode, to reduce memory pressure and performance stress on Android phones.
     
    CasualT_Bossfight likes this.
  4. Loden_Heathen

    Loden_Heathen

    Joined:
    Sep 1, 2012
    Posts:
    456
    I'm looking into a similar use case. e.g. we have a multi-scene game, 2 scenes alternate e.g. map/strategy gameloop has its own scene and battle has its own scene.

    We have found that unloading the map scene takes a long time for some reason (loads fast though) and doesn't seem to be async dispite using the unload async call. So our idea was to simply leave it loaded but of course we need to disable its objects.

    It would be greate if we could simply deactivate the scene leavign it in memmory but ignoring its objects till re-activated but I suppose worst case we could root all elements in the scene under a single gameobject and simply disable that to get the same effect ... seems unnessisary though.

    Anyone done similar before?
    Better yet any ideas why a scene would load from scratch fast but take forever to unload ... more over it locks the machine up for a few seconds nearly a min in some cases and yes that is with the unload async. We do a good bit of load and unload with additive scenes and this is the only case where we have seen this massive lag with an unload.
     
  5. MarcSpraragen

    MarcSpraragen

    Joined:
    Jun 29, 2020
    Posts:
    8
    I'm looking to use the "root object deactivate" mechanism for keep loaded game levels around. I'll update with how it goes.
     
    tubelightboy likes this.
  6. Pablomon

    Pablomon

    Joined:
    Mar 9, 2015
    Posts:
    40
    Looking for the same kind of approach. Any progress on this front?
     
  7. Loden_Heathen

    Loden_Heathen

    Joined:
    Sep 1, 2012
    Posts:
    456
    No we worked around the issue
    For gameplay we just disable the root object as opposed to unload the scene in question

    For returning to main menu we found it was faster if we went in and manually destroyed any objects that where spawned in during gameplay. ... so basicly just call destroy on the same GO we disbale when toggle between map and combat
     
  8. Capvenmar

    Capvenmar

    Joined:
    Mar 23, 2022
    Posts:
    1
    hello guys, how to enable/disable the other scene?example the teacher & student have accounts so how teacher can dis/enable the quiz?
     
  9. Edzard

    Edzard

    Joined:
    Jan 21, 2019
    Posts:
    5
    Any progress on this? I don't really like the root gameobject approach and I'm not sure if it has the same effect as an hypothetical "disabled scene but still loaded" scene, performancewise.

    It'd be useful to be able to keep multiple scenes loaded, ready to activate at will. Although I'd be fine if someone confirmed that the root gameobject approach is effectively the same as what we're asking.
     
  10. TrentSterling

    TrentSterling

    Joined:
    Jan 4, 2013
    Posts:
    97
    So is wrapping the scene root objects in a container and disabling it still the best method for making a scene inactive we might want to wake back up?
     
  11. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,355
    As far as I know, there's no such thing as making a scene active or inactive (There is such a thing as the "active" scene but it does not mean the same thing as we're talking about here). So yeah, you have to somehow activate or inactivate game objects.