Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
16
Question by Horz · Dec 07, 2013 at 04:43 PM · spritetexture2dchildrenunity 4.3

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

5 Replies

· Add your reply
  • Sort: 
avatar image
25

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image blueknee · Mar 31, 2016 at 08:39 AM 0
Share

I ran into the same problem and you saved me! Thanks!

avatar image Denvery · Aug 09, 2016 at 11:26 AM 0
Share

Thank you, OIIOIIOI!

avatar image DrMox · Dec 29, 2016 at 01:59 PM 0
Share

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...

avatar image
19

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();
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image cityismychurch · Jun 02, 2014 at 02:56 AM 1
Share

Would upvote if I could.

avatar image Dunkelheit · Aug 08, 2014 at 07:46 PM 0
Share

Thanks, that worked for me.

avatar image Raiden-Freeman · Feb 03, 2015 at 10:28 PM 4
Share

Be careful: This only works for the editor.

avatar image
10

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).

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image theGreatSlutze · Feb 08, 2019 at 12:04 PM 1
Share

Wow, I'm really glad I didn't look this up yesterday - I would have missed your answer :D thanks for this!

avatar image Afelium · May 10, 2019 at 01:55 PM 0
Share

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?)

avatar image adsilcott Afelium · May 10, 2019 at 10:43 PM 0
Share

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.

avatar image
3

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);

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Horz · Dec 12, 2013 at 03:27 PM 0
Share

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?

avatar image Shawn-Halwes Horz · Dec 12, 2013 at 04:07 PM 0
Share

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

avatar image Marv_ · Feb 13, 2015 at 04:15 PM 0
Share

As @Raiden Freeman mentioned this also only works for the editor :/

avatar image
1

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image CodeNameHj86 · Jun 29, 2017 at 09:29 AM 0
Share

thank you thank you

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

28 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Cast Texture2d to Sprite 4 Answers

Struggling to find how to access the Texture 2D of a sprite. 1 Answer

How do I access a texture slice's rect via scripting? 1 Answer

iOS Sprites in PVRTC? 1 Answer

Resize a sprite by giving it more transparent pixels 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges