Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

AssetBundles with Sprite Atlas : add sprites?

Discussion in 'Asset Bundles' started by TobyKaos, Feb 28, 2022.

  1. TobyKaos

    TobyKaos

    Joined:
    Mar 4, 2015
    Posts:
    214
    Hello,

    I use AssetBundles to distribute SpriteAtlas but I added Sprite alongside to it. When I build AssetBundle I enable include in build via script before creating AB and then disable it after. Then when I build the player it does not include in build sprite atlas.

    Then I wonder if I must include Sprites alongside sprite atlas in my Asset Bundles? I want to optimize size.

    If I do not do it sprites are blank.
    (Maybe I must use atlasRequested callback?)
     
  2. TobyKaos

    TobyKaos

    Joined:
    Mar 4, 2015
    Posts:
    214
    I know that Unity developer have constraints with the engine but:

    1/ If a Sprite Atlas (SA) is packed in AssetBundles (AB) you must include individual Sprite with it if you want that auto reference works.
    Via script I enable include in build for my SA before create AB (for auto refe works and avoid the use of Late Binding) and then for the build I disable it to not include them in build apk.
    If you do not include Sprites in your AB with SA then they will be included in another AB (my scenes AB for instance because UI Image have ref to its)

    -> An improvement will be to not include Sprites at all in AB if we include SA in AB

    2/ If you create a variant SA for low resolution device. Then Sprites are included in your High Definition AB but not in Low Definition AB. Auto ref of sprite in UI Image or Sprite Renderer works. But if you need to access individual Sprite you cannot use AB.LoadAsset<Sprite>(name) as you do it for HD AB. You must use AB.LoadAsset<SpriteAtlas>("AtlasName").GetSprite("name") instead.
    But it is ok and the AB LD is more light because no indivual Sprites are included.

    Then if for AB Low Definition with Sprite Atlas variant works without individual Sprites in AB. Why this cannot works for the AB with the master Sprite Atlas?

    When we build AB with a master Sprite Atlas, the Sprites should not be included in another AB and if we do not tag sprites in the same AB that SA then the auto ref must works (also Late Binding do not work because Sprite ref to SA is lost I think)
     
  3. TobyKaos

    TobyKaos

    Joined:
    Mar 4, 2015
    Posts:
    214
    Ok I investigate a bit more and I apologize to Unity developer because of the Sprites are not included in AssetBundles along side of Sprite Atlas, but the infos concerning Sprites are. This is good for point 1.

    For point 2 we can deal with spriteAtlas.GetAllAssetNames() and search Sprite in all Sprite Atlas of our bundle.

    Else I prefer for the moment deal with my own bundle variant that consist of Duplicate HD to Low Res Sprites and have two master Sprite Atlas (one each for HD and LR).