Search Unity

Question Sprite Atlases the right way?

Discussion in 'Addressables' started by Kujo87, May 19, 2022.

  1. Kujo87

    Kujo87

    Joined:
    Sep 16, 2013
    Posts:
    168
    Hi,

    I've been working with addressables for a bit, and moving our games more and more over to them to manage the memory, but there are a couple of things I'm unsure of when it comes to Sprite Atlases and UI. Usually, our menus are prefabs stored in an addressable group that are loaded and destroyed as needed and the Sprite Atlases are their own addressable group.

    Its my understanding that to use them addressably, the the atlas is market as addressable and uncheck Include in Build? This then requires the lazy loading method from the SpriteAtlasManager.atlasRequested callback and load then with LoadAssetAsync. So I usually put the Sprite Atlases into their own group and mark them as pack separately so they can be unloaded when nothing is referencing that AssetBundle any more.

    This leads to my first problem. When an atlas is no longer being used by that menu page - how do you unload it? I'm aware than when a resource is loaded, we have to unload it again - but using the SpriteAtlasManager callback, I don't have a handle on the resource really and can't be sure if that atlas is still in use or not.

    The other problem I seem to be having is when I run the Duplicate analyser - ALL the sprites that are used across mutiple prefabs (stored in different groups) are flagged, even though they should be being referenced from the Sprite Atlas. Do I just ignore this, or am I doing something wrong to get the sprite atlases to be addressable?
     
  2. davidla_unity

    davidla_unity

    Unity Technologies

    Joined:
    Nov 17, 2016
    Posts:
    763
    I think you're seeing that in the analyzer because you have "include in build" unchecked from your atlas. If the sprites inside the atlas aren't explicitly marked Addressable, they'll be duplicated by whatever uses them. You'll need to make sure your atlas is included in the build if you want assets to not be duplicated.

    There's a section in our documentation at the bottom here: https://docs.unity3d.com/Packages/com.unity.addressables@1.20/manual/ManagingAssets.html that discusses different behaviors for atlases + Addressables.

    The bundle with the atlas should automatically unload when nothing else references that bundle completely (the bundle as a whole, which should just be the atlas since you're using pack separately, but I wanted to call that out). The ref counting should happen on the Addressables back end.

    I hope some of that helps!
     
  3. Kujo87

    Kujo87

    Joined:
    Sep 16, 2013
    Posts:
    168
    Thanks for the info - that's quite helpful. So I think the scenario I was expecting was example 3 with addressables sprites. Just to clarify how this works - I need to mark my sprite atlases as Include in Build and have my atlases in a bundle, but I ALSO need the sprites from those bundles marked addressable as well - even though the sprite atlas contains the actual texture being used? And these can sit in their own bundle but will only be small because its meta?
     
  4. Kujo87

    Kujo87

    Joined:
    Sep 16, 2013
    Posts:
    168
    Hi @davidla_unity - been doing some more work on this, as something else I noticed that I'm not sure how to get around. So we use spriteatlas variants for our game so that we can use lower res sprites, but when Include in Build is selected, it just defaults to the main.

    What is the best way to go about this to continue using addressables without duplicates, but also allow for variants?