- Home /
Load Material Texture from File
Hello,
I'm trying to load in a texture file (png) from my resources file, into my material.
Basically, during runtime, a screenshot is taken after an event which is saved in the resource file within Unity Project. I then want to take this saved image, and apply it to a cube within my scene (the material).
Right now, I've taken out the 'take a screenshot' function, and I'm just using a pre-saved image within my resource file:
uvLocation = "Assets/Resources/UV_1";
gameObject.renderer.material.mainTexture = Resources.Load(uvLocation) as Texture;
When I run it, it doesn't load in the texture into the material.
What am I doing wrong?
Answer by ArkaneX · Sep 22, 2013 at 11:41 AM
Resources.Load requires path relative to Assets\\Resources, so in your case you have to use
Resources.Load("UV_1")
To load images which you save at runtime, you must use different method though. You can for example use Texture2D.LoadImage
var filePath = Application.dataPath + "/Resources/UV_1.png";
guiText.text = filePath;
if (System.IO.File.Exists(filePath))
{
guiText.text += " EXISTS!!!";
var bytes = System.IO.File.ReadAllBytes(filePath);
var tex = new Texture2D(1, 1);
tex.LoadImage(bytes);
renderer.material.mainTexture = tex;
}
Please note that you can only load jpg and png images in this case, and in opposite to Resource.Load, you have to use file extension in path.
Btw - you don't have to put these images in Resources folder.
Okay, I tried this:
Application.CaptureScreenshot(Application.dataPath + "/Resources/UV_1.png");
savedImage = Resources.Load("UV_1") as Texture;
gameObject.renderer.material.mainTexture = savedImage;
The new image never seems to load in. When I stop the app, and hit refresh, only then is when the new image file loaded in. I want it to load in at runtime (which is what Resource load is suppose to do isnt it?) Thanks
Seems I missed the clue of your question. Yes, Resources folder is just for this, but these resources must be imported in Unity Editor.
Answer updated.
Answer by Paulo_stopEatingAnimals · Sep 10, 2014 at 07:28 PM
do you have a folder named Resources in Assets? sorry for my bad english
Your answer
![](https://koobas.hobune.stream/wayback/20220613114610im_/https://answers.unity.com/themes/thub/images/avi.jpg)