Unity3d 4.3 get sprite from atlas

Hello, i have atlas (many sprites have one packing tag), how i can get list of all sprites, or get one sprite by name?
I don’t believe that only way is Load from resources (Resources.Load)

How are the sprites packed? I'm assuming you have a text file that is defining your sprites. If not, then you can define and name your sprites with the built in [Sprite Editor][1]. [1]: http://docs.unity3d.com/Documentation/Manual/SpriteEditor.html

Otherwise, you will have to use a library or script a way to parse your definition file and build the sprites based on that.

no, i just set packing tag on all sprites http://gyazo.com/763b09cb50e38bb49c91d2d3f621b18d.png I realized this tag is needed to sprites united in atlases, and I think there should be a mechanism for obtaining sprites from atlases. I do bubble shooter, and have many colored balls sprites (with different pictures on it), i make enum BallColor { red, blue ... e.t.c.} and when i set enum on prefab - blue, i want to load sprite equals blueball, and i think that load it from resources very stupid... For example in 2d toolkit i can load sprite by name from atlas.

You have sprite mode set to Single, you have to change it. Then, an edit button will appear and clicking it will open the [Sprite Editor][1] where you can define and name your sprites. [1]: http://docs.unity3d.com/Documentation/Manual/SpriteEditor.html

Are you using Unity's built in Sprite Packer?

4 Answers

4

You can’t access the sprites directly from a Texture2d set up as a sprite atlas.

You can however access them from the texture’s asset. The texture is the main asset and all sprites are sub assets.

This should work:
Let me know if there are any errors.

void doSomethingWithAtlasSprites(Texture2D atlas)
{
  String atlasPath;
  Object[] atlasAssets;

  atlasPath = AssetDatabase.GetAssetPath(atlas);
  atlasAssets = AssetDatabase.LoadAllAssetsAtPath(atlasPath);

  foreach(Object asset in atlasAssets)
  {
    if(AssetDatabase.IsSubAsset(asset))
    {
      //asset is a sprite.....
      //do something
      //add it to a list maybe
    }
  }
}

It is a part of my working code. Hope it helps.

public Texture2D spritesheetTexture = null;
private Sprite[] spritesArray = null;
//===============================================================================
private void LoadSpritesFromSpritesheet ()
{
if (this.spritesheetTexture == null) return;

if (this.spritesArray != null) this.spritesArray = null;

UnityEngine.Object[] spritesAsObjects = AssetDatabase.LoadAllAssetRepresentationsAtPath (AssetDatabase.GetAssetPath (this.spritesheetTexture));

if (spritesAsObjects == null) return;

this.spritesArray = new Sprite[spritesAsObjects.Length];

for (int i = 0; i < spritesAsObjects.Length; i++)
{
this.spritesArray _= spritesAsObjects *as Sprite;*_

}
}
//===============================================================================
* public Sprite GetSpriteByName (string spriteName, Sprite[] spriteArray)*
{
for (int i = 0; i < spriteArray.Length; i++)
{
if (spriteArray*.name.Equals (spriteName))*
{
return spriteArray*;*
}
}

return null;
}

I don't know is it correct and efficient way, but it works:) Unity Editor only. Use Resources.LoadAll instead if deploying for example on a device.

I’m also looking for a way to get the material from a Sprite reference but the best I’ve been able to come up with right now is to have a SpriteRenderer from that packing tag present in your scene and then getting the sharedMaterial from that SpriteRenderer at runtime.

Use Unity’s built in Sprite Packer, how to load atlas?