- Home /
Difficulty loading alpha textures at runtime
I'm using the following excerpt to load an image from a file at run time:
if(detail != customOld)
{
print ("It is a custom detail to build");
Texture2D tex = new Texture2D(1,1, TextureFormat.ARGB32, false);
byte[] bytes = System.IO.File.ReadAllBytes(dir+detail+".png");
print (dir+detail+".png");
tex.LoadImage(bytes);
tex.wrapMode = TextureWrapMode.Clamp;
tex.Compress (true);
customDetail.GetComponent<SpriteRenderer>().sprite = Sprite.Create(tex, new Rect(0f,0f,128f,128f),new Vector2(.5f,.5f),256f);
customDetail.name = detail;
customDetail.GetComponent<Detail>().detailName = detail;
customOld = detail;
}
detailToBuild = customDetail;
It works, except seemingly when the pictures have alpha. After I changed the texture being loaded to one that was solid the texture loaded as expected. After I changed the original texture to have no transparency the result was a white square. However, the texture that did load correctly shows all of the detail.
What gives?
Edit:
White background -- shows White square.
Transparent background -- shows nothing.
Try using WWW.LoadImageIntoTexture to load your texture (use DXT5 ins$$anonymous$$d of DXT1 from the the provided example) ins$$anonymous$$d of System.IO, and see if it solves your issue.
tex = new Texture2D(128,128, TextureFormat.DXT5, false);
var www = new WWW(dir+"/"+myTile+".png");
yield return www;
www.LoadImageIntoTexture(tex);
Had the same results. I've also tried doing what's below because I thought maybe mipmaps had something to do with it.
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.wrap$$anonymous$$ode = TextureWrap$$anonymous$$ode.Clamp; tex.Compress (true);
tex.Apply();
Your answer
Follow this Question
Related Questions
Blending two Texture2D 1 Answer
Determine if a Texture2D has alpha 3 Answers
Texture2D: pixel Specific alpha not working? 4 Answers
Changing color of texture with transparency efficiently 0 Answers