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

Show more comments

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.

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 Atlas ETC2 Fallback Bug? 0 Answers

loading sprite from atlas in script 4 Answers

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


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