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?

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.

do you have a folder named Resources in Assets?
sorry for my bad english