Why does Resources.Load <Sprite> return null?

跟風遠走 提交于 2019-12-02 22:02:11
Jay Kazama

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.

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."

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

    Sprite sp = Resources.LoadAll<Sprite> ("Sprites/AI-Avtar") [2] as Sprite;

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();

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!