31

My project has multiple sprites located in Assets\Sprites which I want to load using C# script.

I have tested this:

Sprite myFruit = Resources.Load <Sprite> ("Graphics_3");

But myFruit is still null.

9 Answers 9

70

Resources.Load will search for a directory in Assets/Resources.

If you want to put it to Sprites directory then put it inside Resources (ex. Assets/Resources/Sprites).

Then you can just load it like this:

Sprite myFruit = Resources.Load <Sprite> ("Sprites/Graphics_3");

Also make sure that you've set your image type to Sprite in the inspector.

If you want to load multiple sprites, use this:

Sprite[] myFruit = Resources.LoadAll <Sprite> ("Sprites/Graphics_3");  

See this for more details.

5
  • If I load a single sprite, it worked! But I test with multiple sprite, It did't work exaccly, and the scenes was paused!! Jul 27, 2014 at 6:25
  • I've editted my answer, perhaps that's what you want
    – Jay Kazama
    Jul 27, 2014 at 6:31
  • 4
    Keep in mind that Resources.Load() will not only search in Assets/Resources, but in any directory named Resources under any other directory, e.g. Assets/Sprites/Resources, Assets/Foobar/Plugin/Resources, etc.
    – Parziphal
    Feb 25, 2015 at 18:06
  • Thanks. Searched a lot, but this answer worked like a charm. Nov 11, 2017 at 17:45
  • 2
    ..and this answer saved somebody else in 2019 :) Thanks a lot dude
    – Claus
    Oct 25, 2019 at 10:02
3

Place awesome.png in Assets/Resources/ (you can have subfolders), and use:

GetComponent<SpriteRenderer>().sprite = 
    Resources.Load<Sprite>("awesome");  // No file extension.

http://docs.unity3d.com/ScriptReference/Resources.html

There's also LoadAll that "Loads all assets in a folder or file at path in a Resources folder."

1

I know this is an old post but, if it still does not work by just loading the resources, then we have to add the texture as well. I did it in this way.

Texture2D texture = Resources.Load<Texture2D>("Sprites/imageName");
Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.zero);
btnImage.image.sprite = sprite;
1

I required a different solution for this problem. When i carefully inspected my sprite, I saw the dropdown "Sprite Mode" was empty (even though that shouldn't be even possible I think). Once I selected single and applied the changes, the problem went away.

1
  • Welcome to Stackoverflow. Please enrich your answer by providing some code!
    – MD Zand
    Dec 5, 2022 at 17:36
0

You need to enter the full path for the asset. In this case, try using the path "Sprites/Graphics_3".

0
    Sprite sp = Resources.LoadAll<Sprite> ("Sprites/AI-Avtar") [2] as Sprite;
1
  • 1
    as Sprite would be redundant here because you already loading by type: (LoadAll<Sprite>)
    – Jviaches
    Oct 29, 2018 at 1:16
0

Resources.Load are searching in the directory "Assets/Resources" That's why you need to do

_sprites = Resources.LoadAll<Sprite>(spritesPath);

or

_sprites = Resources.Load<Sprite>(spritesPath);

with spritesPath as relative path. If you need to load all from folder "Assets/Resources/Sprites", you need to write only "Sprites".

after this you can just do the following:

var sprite = sprites[0];

or

var sprite = _sprites.Where(a => a.name == "Sprite_Name_Needed").First();
0

Unity's scripting reference doesn't says that you need write <Sprite> right after Load. So I had problem with loading sprites, although my sprite was in Resources directory.

0

I just used Resources.Load to load my sprite and found the result is Texture2D. So I Use Sprite.Create to create a new sprite with Textur2D to fix this problem.

1
  • You can change image type to be Sprite in import settings and you wont need to create a new one every time
    – pacukluka
    May 18, 2020 at 19:15

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.