Resources.Load doesn't work
Hello!
I spent hours trying to achieve what I want, but it doesn't want to work.
I'm trying, to Resources.Load() an image from my Resources directory and display it on an Image then.
This works, prints "Texture loaded":
var texture = Resources.Load<Texture>("Categories/Matchsticks/1");
if (texture != null) {
print("Texture loaded");
}
else {
print("Missing image file");
}
So yes, the path is correct, the file is present. But... This doesn't work, prints "Missing image file":
var texture = Resources.Load<Texture2D>("Categories/Matchsticks/1");
if (texture != null) {
print("Texture loaded");
}
else {
print("Missing image file");
}
The only difference is Texture to Texture2D
I need to load that texture in order to create a Sprite (Sprite.Create(Texture2D, Rect)) and display the image, on Unity, Android and IOS, explaining why I use the Resources folder and Resources.Load()
I did try with other methods, they all fail
var texture = Resources.Load("Categories/Matchsticks/1", typeof(Texture2D));
var texture = Resources.Load("Categories/Matchsticks/1", typeof(Texture2D)) as Texture2D;
Have you tried the following?
var texture = Resources.Load("Categories/$$anonymous$$atchsticks/1", typeof(Texture));
var sprite = Sprite.Create(texture as Texture2D, Rect);
Yes I did, this does load the file, as it Loads a Texture.
But the sprite is not created for some reason
var texture = Resources.Load(string.Format("Categories/{0}/{1}", category.GetName(), level.GetFilename()), typeof(Texture));
if (texture != null) {
print("Texture loaded");
_guessImageHolder.GetComponent<Image>().sprite = Sprite.Create(texture as Texture2D, new Rect(0, 0, 345, 345), new Vector2(0.5f, 0.5f));
But if I do
var bytes = File.ReadAllBytes(string.Format("Assets/Resources/Categories/{0}/{1}.png", category.GetName(), level.GetFilename()));
var texture2D = new Texture2D(2, 2);
if (texture2D.LoadImage(bytes)) {
_guessImageHolder.GetComponent<Image>().sprite = Sprite.Create(texture2D, new Rect(0, 0, texture2D.width, texture2D.height), new Vector2(0.5f, 0.5f));
_gameScreen.SetActive(true);
}
It works... In Unity of course, not on Android, the reason why I want to switch to resources.load()
Answer by Psychokiller1888 · Dec 14, 2017 at 07:09 PM
Ok, after more than 7 hours fighting, I found the solution.... And it's pretty dump... We copied these images back and forth, and somehow, the file lost their texture types...
Click on the image and make sure that the import settings texture type is correctly set
Loading a resource is then as easy as:
var texture = Resources.Load<Texture2D>("path relative to Resources folder to the image, without extension");
if (texture != null) {
Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
}
I've seen this question many times on here and stackoverflow, never found the answer... This topic put me on the track though: https://stackoverflow.com/questions/38757654/resources-loadsprite-returns-null-only-for-certain-files.
Hope my self answer helps others!
You just saved me at least seven hours. Thanks for answering your own question ins$$anonymous$$d of disappearing into the mist Atlas Shrugged style.
You just saved me more than seven hours...thank you. (And there are far worse ear worms than Talking Heads 77.)
Your answer
![](https://koobas.hobune.stream/wayback/20220612144600im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to import .blend file without loosing texture quality? 0 Answers
Change Texture 0 Answers
Memory leak with creating a new Texture2D 0 Answers
TerrainLayer at runtime 2 Answers