- Home /
When i click reset button this function is called.Here i want to load the Last texture saved by user by name -usertexture.png.Now whenever i load this image into unity and apply it to model it just turns black. I am new to unity :D
using UnityEngine; using System.Collections;
public class FirstLoad : TexturePainter {
public GameObject gameobj;
void Update () {
}
public IEnumerator LoadUserTexture()
{
string fileName = "UserTexture.png";
string fullPath = Application.persistentDataPath + "\\UserdEdited\\"+ "UserTexture.png";
WWW www = new WWW("file:///" + fullPath);
yield return www;
gameobj.GetComponent<MeshRenderer>().material.mainTexture = www.texture;
}
public void loadtexture()
{
StartCoroutine("LoadUserTexture");
}
}
Are you sure the file is found? I haven't used www but usually Unity uses forward slashes as you wrote here: WWW("file:///"
but in the fullPath you use backslashes. Also depending on the material already applied to the object, you might need a new material.
yes sir the file is found , I used www because it is the only method i got to load an image from an outside folder other than resources folder and use it as a texture! I had attached the screenshots what happens when i click reset button.
Answer by malodia · Jan 21, 2017 at 06:52 AM
This is what worked for me you can still edit this and make it simpler ! just to complete my question I posted the answer for future newbies like me xD
using UnityEngine; using System.Collections; using System.IO;
public class FirstLoad : MonoBehaviour { public GameObject gameobj;
public IEnumerator LoadUserTexture()
{
//fullpath is the path where your texture/image file is saved.
string fullpath = Application.persistentDataPath + "/UserEdited"+ "/UserTexture.png";
Texture2D tex = null;
byte[] fileData;
if (File.Exists(fullpath))
{
fileData = File.ReadAllBytes(fullpath);
tex = new Texture2D(2, 2);
tex.LoadImage(fileData);
}
yield return null;
//gameobj is the object on which you want to apply the texture.
gameobj.GetComponent<Renderer>().material.mainTexture =tex;
}
public void loadtexture()
{
StartCoroutine("LoadUserTexture");
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612102343im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
m_InstanceID != 0 when downloading texture from the server 0 Answers
All Geometry is rendering black on Tegra 3 1 Answer
Problem during Unity import 0 Answers
loading a texture every frame 0 Answers
Black spots on textures 0 Answers