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

Is it possible to load a Sprite from within an SpriteAtlas but not knowing which one ?

Discussion in 'Addressables' started by Kiupe, Oct 16, 2019.

  1. Kiupe

    Kiupe

    Joined:
    Feb 1, 2013
    Posts:
    528
    Hello,

    I'd like to use Addressable Assets to dynamically load Sprites which are currently directly referenced in GOs or SOs. But those Sprites are packed in SpriteAtlas so if I made a Sprite Addressable will Unity be able to load it using its names and will it get the packed version of that Sprite ?

    If that is not the case, I guess I would have to load the right SpriteAtlas and search the Sprite inside it. But when a SpriteAtlas has many pages how does it works ? How to load the right pages in order to avoir do load all SpriteAtlas pages ? Can each page be set as addressable ?

    Thx
     
  2. kobayashi-ambr

    kobayashi-ambr

    Joined:
    Oct 11, 2019
    Posts:
    6
    Hi, Kiupe,

    First of all, if you use SpriteAtlas with Addressables, you can only Load SpriteAtlas with using Addressables.Load API.
    ( you can't load sprite which is packed in sprite atlas directly by Addressables )
    After loading target spriteatlas, you have to Call API such as SpriteAtlas.GetSprite() or SpriteAtlas.GetSprites().
    https://docs.unity3d.com/ScriptReference/U2D.SpriteAtlas.GetSprite.html
    https://docs.unity3d.com/ScriptReference/U2D.SpriteAtlas.GetSprites.html

    If you use sprite in SpriteAtlas for GUI, you only have to do set sprite for Image component.
    At Runtime, when that image component is loaded, SpriteAtlasManager.atlasRequested will be called.
    In its argments "tag" show the sprite name which is required.
    When the callback was called, you need to call Addressables.Load<SpriteAtlas>(tag).
    Then automatically sprite will be set to image component.

    So, you are not need to think about the relationship between sprite and SpriteAtlas unless your script use SpriteAtlas.
    If you want to use spritealtas in your code, you have to call API describe above.

    cheers.
     
    Kiupe likes this.