Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

YOU get a bundle! And YOU get a bundle! AKA - how to organize assets?

Discussion in 'Addressables' started by hypnoslave, May 14, 2022.

  1. hypnoslave

    hypnoslave

    Joined:
    Sep 8, 2009
    Posts:
    440
    Hey all! I still have some confusion around when assets are unloaded. it sounds like even if the ref count is zero, the asset won't necessarily be removed from memory if other assets in the same bundle are loaded, is that correct?

    I suppose the obvious question then is why not make a game where every asset has it's own group so there's like six thousand bundles? I know obviously that would be hard to manage but in that case, assets could be just loaded and unloaded nicely right?
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    8,074
    This is correct, an asset bundle (read, asset group) needs to be completely de-referenced for it to unload.

    See this page in the docs: https://docs.unity3d.com/Packages/com.unity.addressables@1.20/manual/PackingGroupsAsBundles.html

    Long story short, thousands of groups is not a good idea.

    I think the answer is, really, to make groups that make sense with respect to how content is used in your game. To pull an example off the top of my head, imagine your bog standard game with a series of levels. I would make each level (scene), and the assets unique to that level, into their own groups.

    Commonly used assets can be in their own series of groups that make logical sense. Thus, when you unload a scene, everything in that scene should be unloaded from memory. If everything from the the shared assets is de-referenced then they'll be unloaded too.
     
  3. hypnoslave

    hypnoslave

    Joined:
    Sep 8, 2009
    Posts:
    440
    Hey thanks very much for taking the time to reply. I'll dig through this and do some thinking.
     
  4. unity_shane

    unity_shane

    Unity Technologies

    Joined:
    Jun 3, 2021
    Posts:
    106
    @hypnoslave, Hi there, I can confirm that @spiney199's advice is accurate (Thanks spiney199!) All dependencies on an asset need to have a ref count of zero before the asset will be unloaded. This also means that if an asset has a dependency on an asset in another bundle, then all assets in BOTH bundles have to have a ref count of 0 for the asset to unload.

    While it's true that you could try and have 1 asset bundle for each asset, in practice, this isn't super practical. AssetBundles have some inherent overhead in loading them, and doing this sort of circumvents the utility of AssetBundles.

    Generally speaking, best practices around AssetBundles revolve around bundling assets together that are likely to be used together, such that all of the assets that are in the bundle are both loaded and unloaded at similar times, for example by bundling all of the assets that are used in a particular scene. This prevents memory from being used wastefully, since once the assets are done being used, you can unload them all, rather than potentially having a large number of assets sitting unused in memory. All of this is covered in more detail in the docs. Sorry for the convenience and I hope this all makes sense!
     
    Last edited: May 25, 2022
    hypnoslave likes this.
  5. hypnoslave

    hypnoslave

    Joined:
    Sep 8, 2009
    Posts:
    440
    Thank you very much, that's super helpful.

    I have one more question I'm still a bit foggy on if you're still around and willing. It's not clear to me how this kind of thing works with respect to using GameObject.Instantiate, as I am. I'm careful to release the handles after calling GameObject.Destroy on the instantiated objects, but it's not clear to me how this is supposed to work. what if I DON'T destroy a game object before releasing it? (I guess I could test this). Does the object disappear in the scene? does the bundle not get unloaded?