Difficulty loading alpha textures at runtime

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.

This is what happens when I’m using unity remote. In this case I’m loading the image using www.LoadImageIntoTexture.


At the right, you cans see the actual texture. It’s supposed to be (programmer art) a blood spatter with an intentionally white background (Originally, I thought it was just an alpha issue).

Anyway, it’s obviously correctly finding the right texture. Because the shader is diffuse it’s dark. But setting it to Sprite-Default just makes it a transparent whiteish/redish square.

Clicking on the Sprite under sprite renderer just gives me whatever the final result is.

Right now, here’s the code:

default:
        {
            print ("Custom Name: "+detail);
            if(detail != customOld)
            {
                print ("It is a custom detail to build");
                Texture2D tex = new Texture2D(128,128);
                byte[] bytes = System.IO.File.ReadAllBytes(dir+detail+".png");
                print (dir+detail+".png");
                tex.LoadImage(bytes);
                tex.wrapMode = TextureWrapMode.Clamp;
                tex.Compress (true);
//yes, I hacked in this LoadImage method. It sort of undoes everything before it.
                LoadImage(dir+detail+".png");


                SpriteRenderer mySprite = customDetail.GetComponent<SpriteRenderer>();

                mySprite.sprite = Sprite.Create(tex, new Rect(0f,0f,128f,128f),new Vector2(.5f,.5f),256f);
                mySprite.material.mainTexture = tex;
                print ("Sprite: "+mySprite.sprite);
                customDetail.name = detail;
                mySprite.name = detail;
                customDetail.GetComponent<Detail>().detailName = detail;
                customOld = detail;

             

             
             
            }
            detailToBuild = customDetail;
            canBuild = false;
            canDetail = true;

         
            print (detailToBuild);
         
            return;
        }
        }
    }

    IEnumerator LoadImage(string path)
    {
        WWW www = new WWW(path);
        yield return www;
        emptyTexture = new Texture2D(128,128, TextureFormat.DXT5, false);
        www.LoadImageIntoTexture(emptyTexture);
        customDetail.GetComponent<SpriteRenderer>().sprite = Sprite.Create(emptyTexture, new Rect(0f,0f,128f,128f),new Vector2(.5f,.5f),256f);

    }

In the middle bottom you can see the texture being correctly loaded from another script for a different purpose. I can’t for the life of me figure out why that one works and this doesn’t.

For the blood spatter in the middle I instantiate a new object; for the one that’s not working I’m trying to modify a prefab directly. Does that matter?