Search Unity

Sprite Atlas and Sprites in Resources folder

Discussion in '2D' started by Democide, Dec 11, 2018.

  1. Democide

    Democide

    Joined:
    Jan 29, 2013
    Posts:
    315
    So, I've just created an atlas A and put a number of Sprites into it. Both the atlas and the sprites are located in resources/. Then i have an atlas B and put a set of other Sprites into it. Here both the atlas and the sprites are NOT in resources/.

    Now when I check out the Last Build Report I can see the size of the various files in the build:

    Now for atlas B, the size of the build-assets are as expected for the size of the textures used. And the build-assets of the sprites used for atlas B only have small filesizes (usually sub 1kb) which indicates that there's only meta information pointing to the Atlas is included, but no actual texture information. Which feels correct since all the texture information is in the atlas B build-asset.

    However when I look at atlas A things are different: The atlas build-assets have the expected size. However when I look at the build-assets of the sprites used for Atlas A things are different. Here the filesizes of the build-assets are as big as the original sprites, which indicates that the entire texture information is included. This makes me wonder if the atlas is even used in that context.



    Does anyone have any insight?

    Edit: Did some further experiments.

    1. Does it use the Resources/ SpriteAtlas?
    Test setup: New project. Added a single large sprite, made it create a mip-map (blur) and put it on a small ui image. Then I created an atlas for that single sprite, and disabled mipmapping there. Then I put both atlas and sprite in the resources/ folder and created a test build.

    Results: Image was crisp - i.e. used the SpriteAtlas. So that's good.

    Update: Tried loading the sprite not by dragging it into the slot but by loading it via Resources.Load. Same results - SpriteAtlas was used.

    2. Does the build contain the Resources/ Sprite tex information?
    Test Setup: As above. Built once with the sprite in the resources/ folder, once with it outside, and once without an atlas.

    Results:
    Build with atlas + sprite outside resources folder: base size
    Build with atlas + sprite in resources folder: base size + sprite size
    Build without atlas: base size - atlas size.

    Less good, that means Unity actually adds the sprite's texture information to the build.
     
    Last edited: Dec 11, 2018
  2. Venkify

    Venkify

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    644
  3. Democide

    Democide

    Joined:
    Jan 29, 2013
    Posts:
    315
    Which is understandable, but strange when they are also present in the atlas.
     
  4. Arthur-LVGameDev

    Arthur-LVGameDev

    Joined:
    Mar 14, 2016
    Posts:
    228
    Was just Googling to find out the answer on this, and make sure it works the way we expected (which is indeed the way it works / is by design).

    By putting the 'sprites' (textures in reality) into the Resources folder, that allows you to Resource.Load() / reference the actual Texture2D. That obviously requires they be present, so it means they have to be packed in the build. If it automatically packed them into the atlas and did NOT keep the Textures in the build, then there would be no way to reference the texture itself -- so if you wanted to use both SpriteAtlas along with maintaining the ability to reference the texture, you'd have to duplicate the assets inside of & outside of the Resources folder.

    I actually thought we were going to have to do that duplication (which would be really ugly but is precisely what my initial test case did), but I'm glad to find out -- via this thread -- that we won't have to, which is great and makes complete sense too. Am glad this is by design, and completely agree with the design decision.

    FWIW, this gives you best of both worlds; if you only want the Sprites accessible (but not the original textures) simply put your SpriteAtlas-referenced sprites/textures outside of the Resources dir. If you do want access to the actual/original Texture, put it inside. That you have both options/tools for different scenarios is great. :)

    Now if only we could get TMPro to use SpriteAtlas instead of a 'custom asset', hah! ;)
     
    hellobody likes this.
  5. Scott-Steffes

    Scott-Steffes

    Joined:
    Dec 31, 2013
    Posts:
    59
    This thread has answered most of my questions, but a problem remains.
    I understand why the original textures are included in a build if they are actually inside the resources folder, but it seems like the original textures also get included in the build if anything in the resources folder references them.

    In my game I have a player prefab that is in the resources folder. I need to be able to instantiate the player in code as I don't want to have to put the player prefab in all of my scenes.
    The player has many animations which reference sprites that are not in the resources folder. I want to be able to pack all the player sprites into a sprite atlas and not include the original images to reduce the build size.

    Is there a way to support instantiating the player prefab through code that does not result in the original image files being included in the build? I don't need to be able to load the individual images through code.

    Any suggestions are greatly appreciated!
     
    ysftulek, frankzzz and Tommy-Angelo like this.
  6. ysftulek

    ysftulek

    Joined:
    Aug 14, 2015
    Posts:
    46

    This is exactly my problem now as I can't really separate textures from the resources folder easily. I was wondering why my build size is so big, then I realized that it's because my other objects that live in the resources folder reference sprites.
     
  7. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,849
    Addressables (and by extension, Asset Bundles) has proper support for Sprite Atlases as dependencies: https://docs.unity3d.com/Packages/c...@2.0/manual/AddressablesAndSpriteAtlases.html

    If you want to take advantage of them, you'd use Addressables for your content streaming.
     
    ysftulek likes this.
  8. ysftulek

    ysftulek

    Joined:
    Aug 14, 2015
    Posts:
    46
    Thank you! To clarify things, I'm not downloading any assets after the game installation. I just wanted to reduce the disk size of my build. After I use Addressables and change all Resources.Load calls with the corresponding Addressable calls with the sync versions instead of async calls, I got rid of all the duplicated assets.