Search Unity

Resolved SpriteAtlas without compression takes up more space than its individual sprites

Discussion in '2D' started by _eternal, Jan 31, 2023.

  1. _eternal

    _eternal

    Joined:
    Nov 25, 2014
    Posts:
    304
    SpriteAtlas question for Unity 2021.3.4.

    The idle animation for my main character contains 24 sprites, and the inspector shows that each one is 170.4 KB when mipmaps are turned on and compression is set to normal. The total size would be around 4 MB.

    If I create a SpriteAtlas with this folder, and if I turn off mipmaps and compression on it, the resulting atlas is actually 9 MB. It generates two sheets at 4 MB each, and another at 1 MB. The atlas only becomes smaller than the 24 sprites if I turn on compression (it becomes around 2.25 MB).

    Is this expected behavior? I would expect the atlas to be smaller than the folder of sprites, or at least the same size — certainly not double the size. I'm unsure if the atlas is ignoring the sprites' compression, or if the space-saving can only be seen at runtime via the Memory Profiler rather than in the inspector.

    --

    Side note: regarding whether SpriteAtlas compression should be turned on or off, I had previously asked about this: https://forum.unity.com/threads/spriteatlas-compression-best-practices.1292340/

    And the conclusion seemed to be that turning on compression would result in double compression.
     
  2. karliss_coldwild

    karliss_coldwild

    Joined:
    Oct 1, 2020
    Posts:
    602
    In general case there is no reason for for SpriteAtlas to be smaller than individual sprites. All it does is glue multiple images into single (or at least few) images. It will take same space as individual sprites potentially slightly more due to packing inefficiencies and additional padding between multiple sprites.

    Only in very specific situations SpriteAtlas might allow packing the sprites more tightly than individual images. For it to happen you need:
    • sprites of shape which waste a lot space when stored individually
    • tight outline mode for sprite
    • tight packing enabled for the SpriteAtlas
    If you have a bunch of blocky player sprites that look like snowman and outline is more or less rectangular, then placing them next to each other will do you no good. It might even take more space due to space lost at the end of row.
    upload_2023-2-1_10-12-21.png

    On the other hand if you have something like v shapes which can nest very well. You can save some space.
    upload_2023-2-1_10-14-19.png



    Comparing size of compressed individual sprites to uncompressed SpriteAtlas makes no sense. Think of it this way - If you had a bunch of jpegs (representing individual sprites), you glued them side by side into one big BMP image (sprite atlas). Would you expect the size of combined BMP to be smaller than individual Jpegs? Any saving from packing multiple images together in most cases will be overshadowed by difference between having compression enabled or disabled. Similar situation applies to sprites/SpriteAtlas and texture compression. Texture compression will typically give you fixed compression ratio (regardless of content) somewhere between 2 and 6 depending on chosen compression level. Unless your sprites have very odd shape and pack very well space saving from SpriteAtlas will often be less than <2.

    It seems like you got the wrong message from post you linked about what you should do with compression settings. You need to disable the compression for individual sprites and enable it for the sprite atlas! Previous analogy with gluing a bunch Jpegs should make it clearer.
     
    Last edited: Feb 1, 2023
    MartinTilo and _eternal like this.
  3. _eternal

    _eternal

    Joined:
    Nov 25, 2014
    Posts:
    304
    Thank you, this is a great explanation. Yes, I can see that it saves space for sprites with a lot of whitespace (for some of them, I had previously increased the canvas size to save them as power-of-two because I wasn't using SpriteAtlases yet). Whereas it doesn't affect my rectangular sprites very much.

    So I think the correct setup is:
    • Compression and Mipmaps turned off for the base sprites
    • Compression turned on for the SpriteAtlas
    • Mipmaps optionally turned on for the SpriteAtlas (I use this to reduce blur at lower resolutions, as discussed in that other thread)