• 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
3
Question by Shar1ngan · Dec 26, 2013 at 04:39 PM · 2dspriteatlas

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)

Comment
Add comment · Show 24
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 Spinnernicholas · Dec 26, 2013 at 06:35 PM 0
Share

How are the sprites packed? I'm assu$$anonymous$$g 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.

avatar image Spinnernicholas · Dec 26, 2013 at 06:36 PM 0
Share

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

avatar image Shar1ngan · Dec 27, 2013 at 07:39 AM 0
Share

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.

avatar image Spinnernicholas · Dec 27, 2013 at 06:37 PM 0
Share

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 where you can define and name your sprites.

avatar image Spinnernicholas · Dec 27, 2013 at 06:41 PM 0
Share

Are you using Unity's built in Sprite Packer?

avatar image Shar1ngan · Dec 28, 2013 at 09:12 AM 0
Share

Yes, and i have many sprites in individual files, but they have one packing tag "Balls" and how if i know atlas name, i can get needed sprites by Sprite name?

avatar image Spinnernicholas · Dec 28, 2013 at 06:07 PM 0
Share

Do you even have access to the sprite packer? It is in developer preview and not everyone has access to it.

Do you open the sprite packer? $$anonymous$$enuBar>Window>Sprite Packer

avatar image edincanada · Jan 29, 2014 at 03:29 PM 0
Share

I believe what the question poster means is: "How can I get a reference to the list of sprites associated with a given spritesheet (texture2d)" Can I get confirmation on that? I do not believe he is asking how to create sprites. Answerers please re-read the question and confirm that my observation makes sense.

avatar image Shar1ngan · Jan 30, 2014 at 09:13 AM 0
Share

edincanada, yes you right!

avatar image Spinnernicholas · Jan 30, 2014 at 04:42 PM 0
Share

I don't think he is creating the spritesheet correctly. If that's true, then nothing will work. I was trying to verify that he actually created a valid spritesheet. Unless the spritesheet was manually created with the built-in sprite packer, he will have to manually define the sprites. The sprite packer doesn't automatically pack sprites, it has to be run.

I need to see a screen shot of the asset browser with the spritesheet expanded before I can help.

avatar image Spinnernicholas · Jan 30, 2014 at 04:45 PM 0
Share

It is like in this video.

avatar image edincanada · Jan 30, 2014 at 04:46 PM 0
Share

Ok, I'm gonna need confirmation from the poster again, but I think I know what he means, and what I think he means is, If I have a reference to a texture2D, can I get a list of its sprites? Something like:

myTexture2DObject.spriteList[0] ; //This would be a Sprite Object.

I do not believe he needs help with sprite creation, but with programmatic access to Sprite Objects in the project.

Once more, poster. Prove me wrong and do correct me!

avatar image Spinnernicholas · Jan 30, 2014 at 04:50 PM 0
Share

P.S. To use sprite packer, after setting the packing tag on your sprites, you have to go to the sprite packer menu, select the packing tag, and finally hit pack. Did you do that?

avatar image edincanada · Jan 30, 2014 at 04:56 PM 0
Share

I know this is unconfirmed, but I believe he is not asking for help on how to create sprites. I believe he is trying to access from code the list of sprites associated with a Texture2D (a spritesheet texture). Something like this:

foreach ( Sprite sprite in texture2DObject.Sprites ) //I made up the Sprites field { Debug.Log( sprite.name ) ; }

If this is what the original poster is asking, I think the answer is "No there is no way" but I'm not quite sure yet. Any of you clever Unity fellas would know?

avatar image Spinnernicholas · Jan 30, 2014 at 05:09 PM 0
Share

I agree that is what he is asking. But, I am not going to waste my time answering it if he is not in a position to actually verify it. If he thinks just setting a packing tag on his sprites gives him an atlas, he's wrong. If he doesn't actually have an atlas, then he will never be able to verify that anything works and the whole thing is a lost cause.

avatar image Spinnernicholas · Jan 30, 2014 at 05:12 PM 0
Share

For a good answer, we need to know the current and desired states. We know he wants to programmatically access sprites from something like a reference to the texture asset. But, I don't know enough about where he is starting at.

avatar image Spinnernicholas · Jan 30, 2014 at 05:21 PM 0
Share

As far as I can tell, what he will need to do is treat the main atlas texture sprite as an asset bundle as opposed to a sprite or texture.

That will take a chunk of coding, If I do it, I want recognition. That requires validation.

I'm not asking for much, just proof that he has a valid sprite atlas. Without that, I'm not going to help.

avatar image edincanada · Jan 30, 2014 at 05:35 PM 0
Share

Let us remember the fellas that may search for the same question, hoping to find an answer to it. I found this question through google because I was wondering myself, and I would expect its answer to serve as reference material for anyone with the same question. After all, the question doesn't seem procedural (how do I export.. how do I make something bounce) It is more of a reference question :)

avatar image Spinnernicholas · Jan 30, 2014 at 06:23 PM 0
Share

That just makes it even more important that all the information is accurate.

I would create a new better formed question and answer that before I answer this one in its current state.

example:

I have an sprite atlas asset. Here is a screenshot:...

Is there a way to programmatically access the underlying sprites from just a reference to the main atlas asset?

avatar image Shar1ngan · Jan 30, 2014 at 06:27 PM 0
Share

edincanada, you right again) but i don't understand how i can get myTexture2DObject i have a lot of single sprites, united by one packing tag. and i need get list of this sprites Oh shit it's hard to explain...

avatar image Spinnernicholas · Jan 30, 2014 at 06:48 PM 0
Share

If done correctly the sprite packer will generate a single Texture2D asset with all of the tagged sprites. That is the "myTexture2DObject".

avatar image edincanada · Jan 30, 2014 at 06:58 PM 0
Share

Shar1ngan, altho I understand your question, I'm afraid I have no answer. These fellas will sure help as they always do tho

avatar image Spinnernicholas · Jan 30, 2014 at 07:34 PM 0
Share

Shar1ngan, all you have to do is post a screen shot of your texture atlas in the asset browser, then I know we are on the same page. If you don't have a texture atlas asset, then we will need to take a step back and create it. I'll walk you though the whole process, but I need more information.

avatar image Spinnernicholas · Jan 30, 2014 at 07:35 PM 0
Share

You're in the driver seat, the more you give me the more I'll give you.

4 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Spinnernicholas · Jan 30, 2014 at 06:44 PM

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
     }
   }
 }
Comment
Add comment · 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
1

Answer by doska · Apr 10, 2014 at 03:55 AM

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[i] = spritesAsObjects[i] as Sprite;
 }
 }
 //===============================================================================
     public Sprite GetSpriteByName (string spriteName, Sprite[] spriteArray)
 {
 for (int i = 0; i < spriteArray.Length; i++)
 {
 if (spriteArray[i].name.Equals (spriteName))
 {
 return spriteArray[i];
 }
 }
 
 return null;
 }
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 Ravensburg3r · Apr 16, 2014 at 09:58 AM 0
Share

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

avatar image
0

Answer by bryan_lee · Apr 10, 2014 at 02:59 AM

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

Comment
Add comment · 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
0

Answer by Jeiel · Jan 09, 2014 at 06:54 AM

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.

Comment
Add comment · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

24 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

Related Questions

Sprite Packer Causes Unity to Hang on 5.5.0f3 0 Answers

Sprite/ Atlas/ Memory limit in 2D Mobile Games? 2 Answers

why does my 2d sprite keep setting itself to "Advanced" and don't pack ? 0 Answers

How do I get the bounds of a sprite on its sprite atlas? 0 Answers

How can I use atlas sprite as texture for a material? 2 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