In my code I create a texture and fill it with colors (some are 100% transparent, others are 100% opaque). I then create a material and put the texture in it. Finally a quad is created and the material is used on its renderer.
Color32 colors = ... //length: 250000 Texture2D t = new Texture2D(500, 500, TextureFormat.ARGB32, false); t.SetPixels32(colors); t.Apply(); Material mat = new Material(Shader.Find("Standard")); mat.SetFloat("_Mode", 2); mat.mainTexture = t; GameObject go = GameObject.CreatePrimitive(PrimitiveType.Quad); go.transform.localScale = new Vector3(500, 500, 1); go.GetComponent<MeshRenderer>().material = mat;
The problem is that alpha is ignored, all colors are 100% opaque. If I select the quad while playing I can see the properties of the material in Unity’s inspector. Shader is “Standard” and the Rendering Mode is indeed “Fade”.
If I manually select “Fade” again from the drop down menu the transparent pixels in the texture immediately become transparent. If I set the inspector to Debug and manually go to Saved Properties > Floats > Element 10 (which is “_Mode”) I can see that “Second” is indeed set to “2” (which means “Fade”). If I manually change this number to 1 (“Cutout”) nothing happens. If I manually set it back to 2 nothing happens as well, all pixels remain opaque and alpha is still ignored.
If I set it to 3 (for “Transparent”) nothing happens. If I then change the inspector back to “Normal” still nothing happens, except that the Material’s Rendering Mode shows as “Transparent” in the Inspector. If I then click the drop down list and select the already-selected “Transparent” option again then the alpha values are respected and some colors in the texture becomes non-opaque.
**Is this a bug or is something weird required when using SetFloat? **
Because SetFloat(“_Mode”, 2); does not work on the Standard shader. It seems like we can’t use the Standard shader as intended from script.