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

Access sprites from atlases programatically

Discussion in '2D' started by Ciprian, Dec 1, 2013.

  1. Ciprian

    Ciprian

    Joined:
    Jul 9, 2013
    Posts:
    27
    Is there any way to access the sprites from the atlases created with Sprite Packer at runtime? I don't want to use the Resources folder and load them with Resources.Load
     
  2. giyomu

    giyomu

    Joined:
    Oct 6, 2008
    Posts:
    1,094
    I don't think right now , there is a way to get back a "sprite" element within the sprite sheet, using the spritesheet meta data you can get rect coord, name etc for the sprite but that do not get you the sprite element really.

    What you can do is either create an array and put all your sprite there so you can then access them from that , or if you know the rectangle it occupy on yoru sprite sheet, use Sprite.Create() function to generate a sprite from your actual sprite sheet.
     
  3. Ciprian

    Ciprian

    Joined:
    Jul 9, 2013
    Posts:
    27
    thank you giyomu for your answer..

    can you please elaborate on "What you can do is either create an array and put all your sprite there so you can then access them from that "

    Cheers
     
  4. TomasJ

    TomasJ

    Joined:
    Sep 26, 2010
    Posts:
    256
    Sprites in SpritePacker atlases are the same sprites you generate from textures. We replace rendering data for packed sprites internally so all you need to do is use the same sprite as before, but instead you'll be getting packing benefits.
     
  5. Ciprian

    Ciprian

    Joined:
    Jul 9, 2013
    Posts:
    27
    Tomas, that's good to know.. it makes a lot of sense..

    I would like to ask you, are there any other benefits(except for convenience) for using unity's built-in Sprite Creator rather than importing from TexturePacker? I mean performance, size, so on..
     
  6. TomasJ

    TomasJ

    Joined:
    Sep 26, 2010
    Posts:
    256
    Besides convenience there's also tight packing which currently may or may not pack better than TexturePacker - depends on assets. Otherwise a Sprite is a Sprite no matter what texture it is created from.