This is a cross post from my Unity Answers question (http://answers.unity3d.com/questions/822094/difficulty-loading-alpha-textures-at-runtime.html)
I’ve tried a couple of different methods to load a texture from file at runtime (Android, but I don’t think that’s relevant) and I haven’t been having any luck with them. I’m thinking there might be a simple “switch” I’m missing somewhere.
Before this I never even declared what type of format.
Texture2D tex = new Texture2D(1,1, TextureFormat.DXT5, false);
byte[] bytes = System.IO.File.ReadAllBytes(dir+detail+".png");
print (dir+detail+".png");
tex.LoadImage(bytes);
tex.wrapMode = TextureWrapMode.Clamp;
tex.Compress (true);
Someone suggested this on the Unity Answers page:
tex = new Texture2D(128,128, TextureFormat.DXT5, false);
var www = new WWW(dir+"/"+myTile+".png");
yield return www;
www.LoadImageIntoTexture(tex);
In this one I did a Get/Set pixels because it helped someone else with a completely different problem, but I figured why not.
Texture2D tex = new Texture2D(128,128, TextureFormat.DXT5, false);
byte[] img = System.IO.File.ReadAllBytes(dir+"/"+myTile+".png"); Debug.Log(dir+"/"+myTile+".png");
tex.LoadImage(img);
tex.ReadPixels(new Rect(0,0,tex.width,tex.height), 0,0);
tex.wrapMode = TextureWrapMode.Clamp;
tex.Compress (true);
tex.Apply();
I’ve tried all three here and I can say that they “work”, although the texture doesn’t appear as it should. I can load a texture (essentially a red square with one side having a black line), while other ones either appear white or not at all (those are on the unity answers page). It’s definitely finding the texture. In the player none of these work well (Maybe an issue with Unity Remote?) but on Android they seem to work if they don’t have a lot of white or have transparency.