Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
0
Question by No_Username_Found · Jul 02, 2019 at 11:48 PM · spritesheetresources.loadall

Load a SpriteAtlas, or access SpriteAtlas of a Sprite

I have a SpriteManager that has several lists of sprites. I am using it to change sprite textures during runtime.

I have a folder with all the sprite atlases in it. Because all the "tree" sprites have the same parts, they use the same layout in their atlases. So [oak] has an [oak SpriteAtlas] with a [Limb] sprite, and [pine] has an [pine SpriteAtlas] with a [Limb] sprite.

When I load the sprites, I get [Limb] and [Limb] but do not know how to tell which atlas they came from.

So I want a way to load the sprite atlas as a whole, then load the sprites from that atlas. I do not want to have to specify the name of each tree, as there are too many.

Here is the code currently:

     public IEnumerator LoadSprites()
     {
         Debug.Log("Loading sprites");
 
         //first, we can find all the sprites
         allSpriteObjects = Resources.LoadAll("Sprites", typeof(Sprite));
 
         //then we need a list for each atlas
         allSpriteAtlases = new List<Sprite[]>();

        //now we need to put each sprite in the correct atlas entry
         foreach (Object o in allSpriteObjects)
         {
             Debug.Log("Loaded " + o.name);

             //HERE: How do I know what atlas this sprite was pulled from?

             yield return new WaitForSeconds(0.1f);
         }
 }

My end goal is to have a List<List<Sprite>, or a List<Sprite[]> or a List<SpriteAtlas>, so that I can find the [Limb Sprite] of [pine SpriteAtlas] from the manager.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by FedericoDeBona · Jul 03, 2019 at 01:22 PM

Hi,

I putted toghether a fast solution, tell me if it works for you. You also need the following namespaces:

-System.Collections;

-UnityEngine;

-UnityEngine.U2D;

 public class SpriteLoader : MonoBehaviour
 {
     Hashtable spriteTable;
 
     public void Start()
     {
         spriteTable = GetSprites();
 
         //use this to loop through the elements
         foreach (DictionaryEntry element in spriteTable)
         {
             Sprite[] sprites = (Sprite[])element.Value;
             foreach (var sprite in sprites)
                 Debug.Log("Sprite in '" + element.Key + "': " + sprite.name);
         }
     }
 
     public Hashtable GetSprites()
     {
         //Insthed of list of list, i used an Hashtable, with key as sprite atlas name
         //and value as array of the sprites of the curren sprite atlas
         Hashtable table = new Hashtable();
 
         //Loading sprite atlases
         SpriteAtlas[] allSpriteAtlases = Resources.LoadAll<SpriteAtlas>("Sprites");
 
         foreach (var element in allSpriteAtlases)
         {
             Sprite[] sprites = new Sprite[element.spriteCount];
             //Clone the sprites of the curren sprite atlas
             //in the 'sprites' array
             element.GetSprites(sprites);
 
             /* this is the structure of the hashtable:
              * key->string(atlas name) value->Sprite[](array of the sprite in the current atlas)
              * 
              * 
              * To access values:
              * table["atlas name"] --> will return the array of sprites of the atlas selected
              * so, for instance if the atlas is "hosue"
              * Sprite[] houseSprites = table["house"];
             */
             table.Add(element.name, sprites);
         }
 
         return table;
     }
 }
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

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

How to access sliced sprites? 0 Answers

Instantiate random prefab from folder using Resources.LoadAll 2 Answers

Playing through sprite sheet only once? 0 Answers

TexturePacker: How to prevent tpsheet from being created? 1 Answer

How to increase sprite animation speed 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