- Home /
How to get child sprites from a 'Multiple Sprite' Texture?
So, I have a texture with TextureType: Sprite and SpriteMode: Multiple in Unity 4.3. I used a Sprite Editor to cut out some Sprites from my texture. Now my sprites are showing as children in the Project window.
How can I access these children in the code?
Lets say Im writing my class and using
public Texture2D txt;
to set a texture in the inspector (by drag and dropping it). But later I want to access Sprites inside this texture in the code. How ca I do it?
Answer by OIIOIIOI · Jul 10, 2015 at 08:37 AM
I was looking for a solution to this that would work in builds and not only in the editor (as opposed to swalex's answer here)
Finally got it to work, so I figured this could help someone in the future:
public Texture2D texture;
...
Sprite[] sprites = Resources.LoadAll<Sprite>(texture.name);
The multi-sprite texture has to be in a Resources folder in your project so that it can be accessed with Resources.LoadAll
.
texture
is declared as a public Texture2D so that it can be set in the editor.
Finally! was looking for a way to solve this for hours. But still, it seems odd to me that using Resources.LoadAll would be the only way to this...
Answer by swalex · Mar 16, 2014 at 09:49 PM
using System.Linq;
using UnityEngine;
...
string spriteSheet = AssetDatabase.GetAssetPath( txt );
Sprite[] sprites = AssetDatabase.LoadAllAssetsAtPath( spriteSheet )
.OfType<Sprite>().ToArray();
Answer by adsilcott · Feb 07, 2019 at 03:42 PM
For anyone searching for this topic, it should be noted that there's a new way of doing this.
In Unity 2017 SpriteAtlas was added. You can make one from the Create menu. If you add a multi-sprite texture to an atlas, you can access the child sprites by using spriteAtlas.GetSprite("child_sprite_name"), (you can change the individual sub-sprite names in the sprite editor) or get all of them with GetSprites(spriteArray).
Wow, I'm really glad I didn't look this up yesterday - I would have missed your answer :D thanks for this!
Thanks for your answer! The only problem I have with atlases is how to decide the packing order(the original spriteSheet is packed in the wrong order,is there a way to specify it?)
It's been a while since I used the SpriteAtlas, but I remember that order was an issue. Atlases are un-ordered, so the only way I could come up with to do it was by rena$$anonymous$$g each sub-sprite in the sprite editor and sorting them through a na$$anonymous$$g convention, which is obviously less than ideal. Although, I wonder if a scripted importer could handle the na$$anonymous$$g part -- something to experiment with. Anyway, unless someone figures out a better way, or unless they add some functionality to the SpriteAtlas, it seems like this might not be the best method for sprites where order is important.
Answer by Shawn-Halwes · Dec 09, 2013 at 09:52 PM
For multi-sprite textures I found this to work:
UnityEngine.Object[] allSprites = AssetDatabase.LoadAllAssetRepresentationsAtPath("Assets/Textures/" + SpriteTextureName + TextureExtension);
But what about caching... i thought if i'm building an app and then i'll have only textures which are in my scene (scene which is added to the build) but when i'm using AssetDatabase.LoadAllAssetsAtPath i can take the resources which are not in the build ... how its gonna work? or should i add additional resources to build somehow also?
If your texture is in a Resources folder it will be available at run time. see: http://docs.unity3d.com/Documentation/$$anonymous$$anual/LoadingResourcesatRuntime.html
and
http://docs.unity3d.com/Documentation/ScriptReference/Resources.html
As @Raiden Freeman mentioned this also only works for the editor :/
Answer by Umai · Dec 26, 2014 at 09:38 AM
I just did this, it seems to work... might be downsides like taking more memory but draw calls should be kept low.
static void Loadcardfrontatlasnamed(string nameofcardfrontatlas) {
Sprite[] sprites = Resources.LoadAll<Sprite>(string.Format("sprites/cardfronts/texture_cardfront_{0}", nameofcardfrontatlas));
atlasCardfront.Clear();
foreach (Sprite asprite in sprites) {
atlasCardfront[asprite.name] = asprite;
}
}
static Dictionary<string, Sprite> atlasCardfront = new Dictionary<string, Sprite>();
Then you can just grab a Sprite by name from the static atlasCardfront. This static function should be called from the static constructor of this class. This all depends on your project and class setup but you probably want to cache the sprites vs filenames early.