Search Unity

Questions regarding loading the same addressable scene multiple times.

Discussion in 'Addressables' started by gilley033, Apr 7, 2022.

  1. gilley033

    gilley033

    Joined:
    Jul 10, 2012
    Posts:
    1,192
    If I want to load the same addressable prefab multiple times, I can load the asset bundle once then just instantiate copies from the base asset.

    Does the same thing happen under the hood with scenes? If I load a scene that is already loaded, will a new asset bundle be loaded or will it use the same underlying asset bundle to load the scene, meaning the asset bundle won't have to be retrieved from the server again? I imagine so but just want to make sure.

    I also have a possibly unique situation where I move the game objects out of the scenes they were loaded in after the addressable scene is loaded (each scene only has a single root game object that is moved however).

    Currently I am keeping track of both the scene and AsyncOperationHandle and when the object needs to be unloaded, I move it back to the scene, unload the scene, and release the handle.

    Is this the best strategy for my type of situation? To be honest, keeping track of which object the scenes/handles belong to is a bit problematic since I can have multiple scenes/objects of the same asset loaded. It can be done, however I am wondering if there is a simpler way to handle this situation, or at least a different way.

    Edit 1: I have another system that loads/unloads non addressable scenes. With that system, I take everything that needs to be unloaded and put it in a special scene that is then unloaded, so everything is unloaded in one go. Two questions:

    1) Is that more or less performant than unloading multiple scenes (one for each object that needs to be unloaded)?

    2) There use to be an Acquire method on the AsyncOperationHandle class that seemed to allow you to unload the original scene immediately after moving the object out of it, but the scene was kept around under the hood until the handle was released. I don't see that method anymore. What is the alternative?
    (Never Mind - Found it)


    Edit 2: I found the Acquire method. Is there a reverse of this where you can decrement the handle without releasing it?

    What I was thinking of doing was: When a scene is first loaded, store its handle, move the game object from the scene, and then unload the scene instance (do I need to call Acquire on the handle first when doing this)?

    Then, each time the same scene is loaded, call Acquire on the first handle to increment its reference count, then release the handle used to load the new scene, and finally unload each scene instance.

    When a game object is no longer needed, move it to a temporary scene that is unloaded, then decrement the handle reference count. Once it reaches 0, release it and stop tracking it.

    Would this work? If so, how do I decrement the reference count of the handle?
     
    Last edited: Apr 8, 2022