- Home /
How to load an image from file path?
I have a .txt file with an image path.
I need to load this image into a texture in-game;
What I'm trying:
static void showCard(Card card) {
Texture2D cardImage = (Texture2D)Resources.LoadAssetAtPath(Application.dataPath + card.image, typeof(Texture2D));
cardObject.renderer.material.mainTexture = cardImage;
}
card.image returns a string with the path, like "/Cards/A/ironman.jpg" Those images are stored in my "Assets/Cards" folder
It doesn't work, cardImage is aways null. I'm not sure if I'm doing this right, if not I would like a suggestion on how to do it.
Answer by Graham-Dunnett · May 26, 2014 at 08:40 PM
Either:
a) the image is stored in the Resources
folder in the editor, and built into a special file for use in the game, in which case you do not need the give the Application.dataPath
, since Unity will know where to find the image if you just give it the path. Or
b) you are trying to load a texture from the file system, in which case you should use the WWW
class.