Get Sprite by name packed inside of parent Sprite

9883902--1426098--upload_2024-6-11_2-12-38.png

How can I get the "Bows-Bow T0 Arrow" sprite by name, when all I have is a reference to the GameObjects Sprite?

The GameObjects sprite is a sprite sheet :

9883902--1426101--upload_2024-6-11_2-13-38.png

I tried using Sprite Atlas, but it's not viable for this project. It's very slow and crashes on import sometimes. I'm using TexturePacker with it's importer now :

9883902--1426104--upload_2024-6-11_2-14-31.png

If I was using a sprite atlas, I would do the following :

            int spriteSize = atlas.GetSprites(spriteArray);

            for (int i = 0; i < spriteSize; i++)
            {
                var sprite = spriteArray[i];

To iterate through all the children sprites in the parent sprite.

Any clues will be VERY appreciated!

addendum

Some people suggest using 'AssetDatabase' :

string spriteSheet = AssetDatabase.GetAssetPath( txt );
Sprite[] sprites = AssetDatabase.LoadAllAssetsAtPath( spriteSheet )
    .OfType<Sprite>().ToArray();

But that is Editor only. Not viable. Some people suggest using Resources folder :

Sprite[] sprites = Resources.LoadAll<Sprite>(texture.name);

Not so sure about that… I try to avoid using the Resources system.

Other people suggest referencing each sprite one by one. I want the process to be automated, this is still not viable.

Some people mention you can add the packed sprite into an atlas and request sprites by name using the sprite atlas... I tried it without any luck. When I get a sprite it's null.

How unfortunate! Well then, that's your loss. It's built in, it works 100% of the time, it's fast and performant.

With Resources.LoadAll() you will get a list of sprites and each has a name to can check.

I guess you're stuck setting up Addressables or something, or dragging them all into something and searching that something... then making some kind of table indexing system. Yuck, I hate writing code that duplicates something as powerful as Resources!

Good luck!

1 Like

9887664--1426938--upload_2024-6-12_21-14-56.png

https://learn.unity.com/tutorial/assets-resources-and-assetbundles#

9887664--1426941--upload_2024-6-12_21-15-9.png

Bummer, there's no way to get the sprites using Addressables.

I've found a workaround, I'm packing the sprite sheet into a sprite atlas, and now I can get the children sprites using GetSprite.

1 Like

What Unity recommend and what actually works for your project are two different things. You should do whatever works for your project.

And at the end of the day - if you don't want to use Resources - you will need to pre-cache this information. Such as having a scriptable object that stores references to these sub-assets (Either direct ones, or Addressable Asset references). Wise to use editor-code to automate this.

Resources is just a hard-coded/built in Asset Bundle, so it's effectively doing the same thing you would with Addressbles anyway.

2 Likes

Totally agree. Cheers!

1 Like