Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 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 · May 22, 2017 at 07:24 AM · spritesheet

Loading Sprite from Sprite Sheet

I have a sprite sheet that contains the various parts of a human. The player can equip/unequip clothes, so the pieces from the basic sprite sheet will be swapped with pieces from another sheet. I need a way to load a single sprite from a sprite atlas.

According to similar questions, using Resources.LoadAll("File Name") is the way to go if the sprite atlas is in the Resources folder. I've tried this, and the Sprite Atlas always has an array size of 0.

Here is my code:

     public Texture newSkin; //This is the sprite atlas I want the object to look like.
 
     // Use this for initialization
     void Start () {
 
         //change out each child
         //    Head = sprite0
         //-----
 
         Sprite[] newSprites = Resources.LoadAll<Sprite>(newSkin.name);
         Debug.Log ("Loaded " + newSkin.name + " with length: " + newSprites.Length);
         transform.FindChild ("Head").GetComponent<SpriteRenderer> ().sprite = newSprites[0];
     }

The Debug prints out: Loaded Template_Human1 with length: 0

The last line returns an IndexOutOfRange exception.

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
2
Best Answer

Answer by Masterio · May 22, 2017 at 07:45 AM

  1. You should use some handlers for slots like: head, body, feets etc. it will boost performence.

  2. I found something like that:

      public Sprite GetSprite(string path, string name)
         {
             Object[] sprites = AssetDatabase.LoadAllAssetRepresentationsAtPath(path);
     
             if(sprites == null)
                 return null;
     
             for(int i = 0; i < sprites.Length; i++)
             {
                 if(sprites[i].name == name)
                     return (Sprite) sprites[i];
             }
     
             return null;
         }
    
    
    

It is not optimized probably but it works for me.

Inputs example: ("Assets/Textures/SkinAtlas_0", "Head")

Comment
Add comment · Show 2 · 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 No_Username_Found · May 22, 2017 at 08:25 AM 0
Share

I don't understand what you mean by "use some handlers". How do I change the name of the individual sprites in the atlas? Right now they all have the assigned names (Template_Human0, Template_Human1, etc).

I was able to get the sprites to swap out using AssetDatabase.LoadAllAssetRepresentationsAtPath(AssetDatabase.GetAssetPath(newSkin))

avatar image Masterio No_Username_Found · May 22, 2017 at 11:04 AM 0
Share

It is just suggestion about handlers:

replace this:

 transform.FindChild ("Head").GetComponent<SpriteRenderer> ().sprite

for handler like this:

 headRenderer = transform.FindChild ("Head").GetComponent<SpriteRenderer> ().sprite;

and add this property to the class:

 SpriteRenderer headRenderer;

This is good when you want to replace sprites in game, otherwise you must always search head transform and sprite component and it takes some performence lose.

About names i dont know if there is possibility to change meybe in newer versions of unity, try click on sprite atlas file and press button Sprite Editor in inspector. If not works you must create new atlas with good names.

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

67 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

Related Questions

[ANDROID] Downloading large (4000x4000) textures causes a lot of memory consumption. 3 Answers

random sprite javascript 0 Answers

Load specific sprite from sprite sheet 1 Answer

InvalidOperationException: Operation is not valid due to the current state of the object. 1 Answer

Can't Optimize Huge Sprite Sheet 1 Answer


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