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
5
Question by Siflou · Nov 15, 2013 at 11:22 AM · spritetexture2dresources.load

Loading a Sprite (Unity 4.3) in resource folder and setting it in sprite renderer

Hey Guys,

I have a SpriteSheet which I imported in Unity so I have a basic Texture2D asset which is separated in multiple sprites which are named like (sprite_0,sprite_1, etc.) Im trying to set the sprite of my SpriteRenderer in code but I want to be able to load a sprite dynamically, not have to link it in the script directly. So I have a Resources and a Textures folder within it. I' trying to get the sprite by doing Sprite s = Resources.Load("Textures/sprite_0") as Sprite. But it is always null. In fact, when I want to load the "Base" sprite named "Sprite", it recognize it as a Texture2D type, but when I want to load a specific sprite in my texture like sprite_0, it just can't find it at all. I have my texture type set to Sprite, and Mode to multiple. Does anybody have an idea ?. Thanks a lot for your time :)

Claude

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

6 Replies

· Add your reply
  • Sort: 
avatar image
12
Best Answer

Answer by Dragon_Mao · Nov 15, 2013 at 12:34 PM

 Sprite[] sprites = Resources.LoadAll<Sprite>("Textures");
Comment
Add comment · Show 8 · 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 ABCpt · Nov 16, 2013 at 08:12 AM 0
Share

How then to get the desired sprite by name?

avatar image Dragon_Mao · Nov 16, 2013 at 08:23 AM 0
Share

Only by index, I suppose. It's not a problem, because name looks like "textureName_spriteIndex".

avatar image ABCpt · Nov 16, 2013 at 08:59 AM 1
Share

Unfortunately in my case, the names do not contain index. I did so:

 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")];

But maybe there's a more elegant way?

avatar image Siflou · Nov 17, 2013 at 01:43 AM 0
Share

Worked like a charm ! Thanks a lot everybody for your time :)

avatar image amjadyahya · Nov 19, 2013 at 02:49 PM 0
Share

@Siflou: Hi, could you post the final code, so we could see how did you solve it, because it's not working for me.. thanks.

Show more comments
avatar image
3

Answer by Shawn-Halwes · Dec 09, 2013 at 09:41 PM

For multi-sprite textures I found this to work:

UnityEngine.Object[] allSprites = AssetDatabase.LoadAllAssetRepresentationsAtPath("Assets/Textures/" + SpriteTextureName + TextureExtension);

Comment
Add comment · Show 4 · 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 Panx · Jan 22, 2014 at 08:03 AM 0
Share

This is pretty great! Saves you the trouble of putting stuff in a Resources folder, too! (I'm neurotic, and feel sooooo much better if I can keep my folders labeled by type)

avatar image razveck Panx · Feb 19, 2017 at 03:20 PM 0
Share

I know this is an old answer, but you can have your resources folders labelled by type. You can have multiple resources folders, each for each type you want. So you can have Prefabs\Resources, Sprites\Resources, Audio\Resources, etc.

avatar image Panx · Jan 27, 2014 at 07:31 AM 0
Share

So, bad news, actually -- this only works in the Editor. Turns out AssetDatabase is name-spaced to UnityEditor, and so if you include this elsewhere in your code it won't build.

avatar image Shawn-Halwes · Jan 27, 2014 at 04:26 PM 0
Share

Correct. The implication was that you use custom editor code to hook up the references ahead of time.

avatar image
1

Answer by lxem · Dec 16, 2013 at 09:27 AM

You can also define sprites in advance.

 public Sprite[] sprites;

Then you can bind sprites in Unity Editor, and set SpriteRenderer.sprite in runtime like this.

 SpriteRenderer sr = GetComponent<SpriteRenderer>();
 sr.sprite = this.sprites[Random.Range(0, this.sprites.GetLength(0) - 1)];

For some situation this is enough.

I hope this can help.

Regards.

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 fabien0102 · Jun 15, 2016 at 01:37 PM

You can use System.Linq to find easily sprite into texture ;)

For example:

 private Sprite GetLevelSprite(string textureName)
         {
             Sprite[] textures = Resources.LoadAll<Sprite>("MySpriteSheet");
             return textures.Where(t => t.name == textureName).First<Sprite>();       
         }

GetLevelSprite("plop") will return the sprite with /Assets/Resources/MySpriteSheet/plop path ;)

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 ArchAngelus · Feb 12, 2014 at 05:43 PM

If you want to access a particular sprite by a more meaningful name like "Rocket" or "Zombie", I would create Prefabs out of the sprites and load them up with Resources.Load().

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

32 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

Related Questions

TexturePacker 0 Answers

Image distorts when created in C# 3 Answers

Draw tilemap with Texture2D.SetPixels: Performance and Memory? 0 Answers

sprite border 0 Answers

Change the color of a black sprite?,How can i change the color of a black sprite? 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