Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Confused about Sprite Atlases!

Discussion in '2D' started by Matt-Cranktrain, Aug 14, 2023.

  1. Matt-Cranktrain

    Matt-Cranktrain

    Joined:
    Sep 10, 2013
    Posts:
    129
    Hi, so I'm trying to understand what I should be seeing in the Memory Profiler after using Sprite Atlas.

    Let's say I have a Texture2D with lots of Sprites in it called 'BuildOptions':

    upload_2023-8-14_15-12-12.png

    ... and I create a Sprite Atlas that contains all those Sprites along with a hundred other ones:

    upload_2023-8-14_15-13-9.png

    Looks good to me in the preview, and I don't see any visual regressions/abnormalities in the game.

    But when I look at a snapshot in the Memory Profiler:

    upload_2023-8-14_15-15-59.png

    I'm seeing both the original BuildOptions texture and the Sprite Atlas pages in memory, and not just the new Sprite Atlas images as I would have expected.

    What am I doing wrong here?

    The way I'm using these sprites is mostly programmatically, loading them into Sprites and UI Images like so:

    Code (CSharp):
    1. // Create a sprite name -> sprite lookup:
    2. Sprite[] buildOptionSprites = Resources.LoadAll<Sprite>("Sprites/Interface/BuildOptions/BuildOptions");
    3. buildOptionLookup = new Dictionary<string, Sprite>();
    4. foreach (Sprite sprite in buildOptionSprites) {
    5.     buildOptionsLookup.Add(sprite.name, sprite);
    6. }
    7. // ... and then look up later:
    8. image.Sprite = buildOptionsLookup["ASpecificSpriteName"];
    I'm not building the lookup from the Sprite Atlas .GetSprites() method because that creates Clone sprites, and I am under the impression that Unity ought to 'invisibly' make sure the Sprite Atlas gets used. But then, in double-checking what's happening in the Memory Profiler I saw the duplication in both the original BuildOptions texture and the new packed sprite atlas Texture... and here we are.
     
  2. Skiriki

    Skiriki

    Joined:
    Aug 30, 2013
    Posts:
    66
    Hi there,
    Did you take that capture from an Editor or a Player?
    I can't tell from your post and the most common confusion that arises here is because of differences in how the Editor deals with sprites and Atlas memory vs how a built Player does.
     
    Matt-Cranktrain likes this.
  3. Matt-Cranktrain

    Matt-Cranktrain

    Joined:
    Sep 10, 2013
    Posts:
    129
    Thanks Skiriki, yep, I had read this forum thread on the Editor differences previously, but I'm experiencing this in both the Editor and in an IL2CPP Windows build.

    Or, I was experiencing it, in learning a bit more about the Memory Profiler (which I'm new to) I found the 'References>References To' section helpful, because I could see that one of my asset store assets was doing something unexpected.

    By default the saving-and-loading asset "Easy Save 3" collects a record of GUID references to assets in a saved scene, and it was recording the asset ID for the texture, which was enough to summon it into memory and be duplicated alongside the Atlas version.

    It's easy to turn that feature off and in doing so it looks like the problem is solved for me.
     
    karliss_coldwild and MartinTilo like this.