Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Scenes and memory management

Discussion in 'Addressables' started by Adrien_Danglard, Jun 28, 2020.

  1. Adrien_Danglard

    Adrien_Danglard

    Joined:
    Nov 4, 2019
    Posts:
    18
    Hello, I have a couple of questions about scene loading / unloading.

    If I have a non-addressable scene (part of built-in data), and I call Addressables.UnloadSceneAsync on it, will the assets used by this scene get unloaded? Judging by the documentation on SceneManager.UnloadSceneAsync (which I believe is used behind the scenes), the answer is no, and the only way to clear memory in this case is to call Resources.UnloadUnusedAssets. Is this correct?

    Say I have a bundle loaded with three assets, A, B and C. I hold on to a reference to A, but not B and C. I understand that until I let go of the reference of A, the whole bundle will be kept in memory - unless I call Resources.UnloadUnusedAssets, in which case B and C will be unloaded.

    But what happens, if, using UnloadUnusedAssets, I unload part of the bundle, and then later I try to load the same bundle again? Is the whole bundle going to be loaded again, meaning that I'll now have a leaked "A" asset, a leftover from a previous bundle load? And if this is the case, do we have to make sure to let go of all references to all bundles, in case we call Resources.UnloadUnusedAssets to clean up after a scene which is not itself bundled?

    EDIT: OK, so after a bit of testing it seems that misunderstood how UnloadUnusedAssets would affect the bundles themselves - as in, calling UnloadUnusedAssets would not unload the bundles themselves, but it would unload the assets that are in the bundles, but currently not referenced. This kind of makes sense.

    My question, then, is: is there a quick and easy way to keep already loaded bundles in memory, with all their assets, while resources.UnloadUnusedAssets is called?
     
    Last edited: Jun 28, 2020
  2. ProtoTerminator

    ProtoTerminator

    Joined:
    Nov 19, 2013
    Posts:
    583
    If you called
    LoadAsset(s)Async
    and never released that handle, scene unloading shouldn't unload that asset or its dependencies. If you never called
    LoadAsset(s)Async
    and only used
    InstantiateAsync
    , then unloading the scene should also unload those bundles.

    I believe that's how it works based on what devs have said before, but I haven't done any testing myself.
     
  3. Adrien_Danglard

    Adrien_Danglard

    Joined:
    Nov 4, 2019
    Posts:
    18
    OK, this seems clear, my question is more about how to prevent bundled assets being cleaned up, when switching a scene or calling UnloadUnusedAssets. The idea is this: if I'm switching to a new scene that shares some of the assets with the current scene, it would be a waste to unload and then reload those assets again. So what I've been trying to do is to hold on to a reference of an asset in the bundle that I want to keep around. So far as I understand the addressables documentation, because no asset will be unloaded until the bundle itself is unloaded, this ought to work. Except that if at this point I call UnloadUnusedAssets, then the assets that I don't hold a reference to will be cleaned up.