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
0
Question by Mefistofel · Nov 14, 2013 at 05:14 PM · spriteresource.load

Load UnityEngine.sprite from code.

Trying to understand the new sprite system. All is fine, you can easily create and upload sprites from editor and from the code:

 // from texture
 SpriteRenderer spriteRenderer = gameObject.AddComponent<SpriteRenderer>();
 spriteRenderer.sprite = UnityEngine.Sprite.Create(
   Resources.Load<Texture2D>("Textures/Interface/pause_button"),
   new Rect(0,0,256,256), new Vector2(0.5f, 0.5f), pixelToUnit);
 // from sprite in resource
 SpriteRenderer spriteRenderer = gameObject.AddComponent<SpriteRenderer>();
 spriteRenderer.sprite = Resources.Load<UnityEngine.Sprite>("Textures/Interface/button");

There is a possibility to create a collection of sprites from atlases (via sprite editor). But if you download sprite by texture name, only the first sprite in the collection loading. Is it possible to download the sprites from the atlas by Resource.Load or another function?

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

8 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by luciano182 · Mar 28, 2015 at 04:58 PM

Here's my solution, using a SpriteRepository as Cache.

 public class SpriteRepository
 {
     public static Dictionary<string, Sprite[]> dict = new Dictionary<string, Sprite[]>();
 
     public static Sprite GetSpriteFromSheet(string name)
     {
         string query = @"((\w|-|_|\s)+/)*((\w|-|_|\s)+)(_)(\d+)$";
 
         Match match = Regex.Match(name, query);
 
         string sprName = match.Groups[3].Value;
         string sprPath = name.Remove(name.LastIndexOf('_'));
         int sprIndex = int.Parse(match.Groups[6].Value);
 
         Sprite[] group;
         if (!dict.TryGetValue(name, out group))
         {
             dict[name] = Resources.LoadAll<Sprite>(sprPath);
         }
 
         return dict[name][sprIndex];
     }
 
 }

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 PBeast · Apr 16, 2015 at 08:38 PM 0
Share

Hi luciano182, thanks for your post! Your answer seems really elegant and it's the only solution i found so far without using the ressources ... unfortunately i'm (not yet) a c# pro and so i have no clue how to use your solution :( how do i pass the sprites ( spritesheet and the names of the sprites in o.0

would be awesome if you could explain this to me :) Thanks in advance !

avatar image Catman · Apr 17, 2015 at 02:16 PM 0
Share

The query should be as follows:

 string query = @"((\w|-|_|\s)+\/)*((\w|-|_|\s)+)(_)(\d+)$";

Note that this code will work if you pass to it a string in the form "PathToAtlas/AtlasName_N" where N is the index of the texture you want in AtlasName.

@PBeast, this solution uses the same Resources folder as the others, look in the inner brackets!

avatar image PBeast · Apr 17, 2015 at 08:53 PM 0
Share

ah ! okay sorry ! $$anonymous$$issed the inner brackets somehow -.-' Sorry for that :X Thank you very much for your response !!!

avatar image
2

Answer by Mefistofel · Nov 20, 2013 at 06:53 AM

Here people also came to the same:

 Sprite[] textures = Resources.LoadAll<Sprite>("Textures");
 string[] names = new string[textures.Length];
  
 for(int ii=0; ii< names.Length; ii++) {
 names[ii] = textures[ii].name;
 }
  
 Sprite sprite = textures[Array.IndexOf(names, "textureName")];


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 golemsmk · Nov 29, 2013 at 10:25 AM

You should use LoadAll() function. For example, you have a "Button" sprite that inside Image folder has been divided into smaller sprites, and they are "Button_1", "Button_2",... then you do this:

 Object[] myTextures = Resources.LoadAll<Sprite>("Image/Button");

You must use Object[], with me, when I tried anything else, I got a error message. Then you can assign myTextures[index] to anything you want, like this:

 spriteRenderer.sprite = myTextures[index];

Goodluck, and have fun.

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 iingmar · Dec 30, 2013 at 10:39 AM 0
Share

It needs to be:

Sprite[] myTextures = Resources.LoadAll("Image/Button");

not Object[]. It works like a charm then!

avatar image golemsmk · Apr 03, 2014 at 07:17 AM 0
Share

You are using C# or Java? With me, this code work on Java.

avatar image
0

Answer by MillZzie · Oct 21, 2014 at 01:43 PM

I keep using this and instead of applying the new sprite it just removes the current one.

gameObject.transform.FindChild("Cat_Head").GetComponent().sprite = Resources.Load("Sprites/RedSprite/Head");

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 gitstar · Mar 02, 2015 at 02:12 PM

I used the following the way..it`s the usefully code. thanks.

   void SetSprite(UI2DSprite uiSprite, Sprite[] sprites, string strKey)
     {
         foreach (Sprite stexture in sprites)
         {
             if (stexture.name == strKey)
             {
                 uiSprite.sprite2D = stexture;
                 break;
             }
         }
     }
 
 ///////////
 // sprite image load change.
 UI2DSprite[] uiSprites = tmpObject.GetComponentsInChildren<UI2DSprite>();
 Sprite[] sprites = Resources.LoadAll<Sprite>("Textures");
 string resName = "icon_favorite_2";
 
 foreach (UI2DSprite uiSprite in uiSprites)
 {
 if(uiSprite.name = "icon")
  SetSprite(uiSprite, sprites , resName);
 
 }






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
  • 1
  • 2
  • ›

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

Unity3d Resources.LoadAll empty array 1 Answer

Custom shader that behaves like Sprite mask 1 Answer

Unity 2DToolKit sprite sort problem 1 Answer

If I use Sprite Atlas, will the images that are part of the atlas still be included in build? 1 Answer

Pixelated sprite when changing position 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