- Home /
Loading image from external directory at runtime
Hi, I'm trying to load a jpg image to then put into file, but I'm getting a NullReference exception at line 25 (tex.loadimage). I can't figure out the reason, can you?
Sprite map;
Texture2D tex;
WWW www;
bool canLoad = true;
void Start () {
map = gameObject.GetComponent <Sprite> ();
}
void Update () {
if (canLoad) {
if (Input.GetKeyUp (KeyCode.P)) {
StartCoroutine (GitMap ("file://" + Application.dataPath.Remove (20) + "/myMap.jpg"));
//A mess, but I found no other way to get relative directory to desktop
Debug.Log (www.url);
//This shows that the directory is correct (file myMap on Desktop)
if (www != null) {
Debug.Log (www.bytes);
}
tex.LoadImage (www.bytes); //this is the problem.
map = Sprite.Create (tex, new Rect (0, 0, tex.width, tex.height), new Vector2 (0.5f, 0.5f));
}
}
IEnumerator GitMap (string PATH) {
www = new WWW (PATH);
yield return www;
}
Comment
Your answer
