Search Unity

  1. New Unity Live Help updates. Check them out here!

    Dismiss Notice

Preload a scene and its dependencies to memory

Discussion in 'Addressables' started by Daerst, Mar 18, 2020.

  1. Daerst

    Daerst

    Joined:
    Jun 16, 2016
    Posts:
    221
    Let's say I have a corridor with two doors leading into other scenes. When opening a door, the respective scene behind it should be loaded.

    I have the scenes on my harddrive already. As soon as the player enters the corridor, I would like to preload the two doors' scenes (and their dependencies of course) to memory, so that when the player actually opens a door, the actual
    Addressables.LoadSceneAsync
    is as quick as possible.

    I tried
    Addressables.Load<Scene>(myAssetReference)
    , which throws an exception. Just starting the
    Addressables.LoadSceneAsync
    with
    activateOnLoad=false
    works only for one scene - no way to have two incomplete AsyncOperations lingering at the same time, I think.

    Any way to do this? Thanks in advance.
     
  2. Daerst

    Daerst

    Joined:
    Jun 16, 2016
    Posts:
    221
    Bumpy bump. I thought simplifying memory management is exactly one of the selling points of using Addresables.
     
  3. Thermos

    Thermos

    Joined:
    Feb 23, 2015
    Posts:
    69
    Unity can't handle scenes preloading properly when using multi-scenes.

    For example, you cannot preload one before unloading a scene, incomplete loading will block other scenes from unloading. Another scenario is what you mentioned, you can't have two scenes preload together.

    This limit is at the engine side, it has nothing to do with Addressable. The only possible solution I found is to transform the scene into one large prefab. As unity moving everything to DOTS and Sub Scene workflow, I doubt that they will change this behavior since sub-scene is the ultimate solution for async additive loading.
     
  4. Daerst

    Daerst

    Joined:
    Jun 16, 2016
    Posts:
    221
    Thanks for the insights @Thermos. Since the dependencies of a scene (e.g. meshes, textures etc.) are known at compile time, they could be statically analyzed and I would expect Addressables (or Unity itself, for that matter) to allow me to list and load them in some way without instantiating the actual GameObjects. To me, being prepared to quickly load a number of scenes, and then actually loading one depending on player actions, sounds like a very common scene loading requirement.
     
    BTStone likes this.
  5. Daerst

    Daerst

    Joined:
    Jun 16, 2016
    Posts:
    221
    I'm seriously considering ditching Scenes in favor of Prefabs. I'm working on a 2D game, so I care neither about lighting settings nor NavMesh. Usings Prefabs would allow me to preload things using Addressables and instantiate right when I need them. I can preload Prefabs in parallel, and actually instantiate only those that I really need. No more wonky "your scene will start loading in the next frame", stopping at 0.9 and then taking a non-deterministic number of frames (in my fairly small 2D game anything between 2 and 18!) to actually complete after allowSceneActivation is set to true.

    Two downsides I can think of:
    • Changes in a Prefab will propagate, forcing all 'Scene' Prefabs that use it to reserialize, which can potentially be annoying. You wouldn't have this with scenes - they only store the reference.
    • Can't use the localPhysicsMode property to give different scenes their own physical space (which, so far, Addressables don't support anyway). Could potentially be worked around with SceneManager.CreateScene, and then instantiating the Prefab into that.
    Any more things to consider?
     
    BTStone likes this.
unityunity