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);
Material mat = new Material(Shader.Find("Standard"));
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.
Answer by Hotdug
Jul 30, 2015 at 08:12 PM
I asked on the forum instead and got an answer:http://forum.unity3d.com/threads/standard-material-shader-ignoring-setfloat-property-_mode.344557/
If anyone sees this I recommend everybody asks on the forum instead of on answers.unity3d.com in the future because this place is a desert compared to over there. In addition you don't need to wait for a moderator to approve your questions (or answers).
The depressing answer is:
Material m = new Material(Shader.Find("Standard"));
m.renderQueue = 3000;
That's right, eight lines of code for something that should be one line. Unity should put a .ChangeProperty(string, value) method into the Material class that does this work for you if you hand it the right keyword (shaders would override ChangeProperty methods and listen to their own keywords and execute their own code depending on what is being given). Then just make a note of supported strings in the shader's documentation.
Considering how much confusion I've seen online from _Mode not working it would definitely be something that people would appreciate. Most seem to think, as I did, that it was a bug when chaging _Mode did nothing except updating the drop-down list in the inspector.
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
To help users navigate the site we have posted a site navigation guide.
If you are a new user to Unity Answers, check out our FAQ for more information.
Make sure to check out our Knowledge Base for commonly asked Unity questions.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
Answers and Comments
2 People are following this question.
How can I prevent lights overlapping in intensity?
Problem using multiple materials
Tracing in Unity3D
use RenderTexture on a sphere correctly?
Scrambled textures on first run of game only.