I am creating a C# script that applies an imported texture to a plane (Game Object) when the program starts. For that, I am copying the user choosen image to the assets folder. Then I change it to .byte extension, that's because I am using TextAsset to read and set the object texture.
Now what i consider that is failing is the texture Load, this is the full script code:
using UnityEngine;
using System.Collections;
using System.IO;
public class setplant : MonoBehaviour {
GameObject plant;
void Start () {
plant = GameObject.Find("plant");
string currentdirectory = System.IO.Directory.GetCurrentDirectory();
string pathconfig = (Path.GetFullPath(Path.Combine(currentdirectory, "..\\Pick_And_Build\\Launcher\\Launcher\\bin\\Debug\\tempfiles\\"))+"instanciateprogram.dat");
StreamReader reader = File.OpenText(pathconfig);
string img = reader.ReadLine();
reader.Close();
string textureresourcepath = Directory.GetCurrentDirectory() + "\\Assets\\" + Path.GetFileNameWithoutExtension(img) + ".byte";
if(!File.Exists(textureresourcepath))
File.Copy(img, textureresourcepath);
TextAsset imageasset = UnityEditor.AssetDatabase.LoadAssetAtPath(Path.GetFileName(textureresourcepath), typeof(TextAsset)) as TextAsset;
Texture2D tex = new Texture2D(2, 2);
tex.LoadImage(imageasset.bytes);
plant.GetComponent<Renderer>().material.mainTexture = tex;
}
void Update () {
}
}
Note: The image is copied to the Assets folder so the directory system is working.
The output for this is just a Plane with the Default-Material instead of the image texture-based material. Any tips?
SOLUTION
It was a stupid bad extension issue. It is not .byte ... ".bytes" is the correct one. Thank you anyway for the people that tried to help me.