- Home /
Transparency working in the editor but not when I build
Hey guys. I'm dealing with a problem for days and I getting desperate. The code that it's above is working completely fine on the editor. Even when I change the Shader from "Standard" to "Transparency" for example, or even when I change the color. But when I want to build it to Android or iOS it doesn't show me the transparency that i see in the editor, it looks opaque and i don't know why. Any thoughts from what I'm doing wrong? Thanks a lot!
Material m = new Material(Shader.Find("Standard"));
m.SetFloat("_Mode", 3);
m.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
m.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
m.SetInt("_ZWrite", 0);
m.DisableKeyword("_ALPHATEST_ON");
m.EnableKeyword("_ALPHABLEND_ON");
m.DisableKeyword("_ALPHAPREMULTIPLY_ON");
m.renderQueue = 3000;
plane.GetComponent<Renderer>().material = m;
//mat.color = new Color(192, 217, 217, 1.0f);
m.color = new Color(255, 255, 255, 0.5f);
This is likely the result of Unity's Shader compilation process, coupled with making the overly-complex "Standard" Shader to begin with. The key reference point is Definition type: “multi compile” or “shader feature” on the Shader Keywords page.
The Standard Shader has a large number of togglable features (every "Keyword" in the script in this question, plus many more) which expand into exponentially more permutations of resulting Shader.
Historically, "multi_compile" meant that every possible permutation of a shader was built into the final game. With the huge number of combinations of compilation settings in the Standard Shader (~1024+), however, they implemented a way of only using variants that are actually implemented into the built game. Doing this, however, means needing to try and detect ones that have been "used" by checking Materials easily visible to the editor.
What this means in the end is that you basically can't configure a Standard Shader exclusively at runtime. You would want to prepare a Shader variant collection to ensure that all the permutations your game would support will be included in the build.
That was totally it. I looked in the inspector in debug mode at the Shader Keywords field of every material of gameobjects when they become transparent in runtime, and added all the combinations I could find to the Shader Variant Collection. Now my build is working fine. Thanks!
Another "solution" I found before was this, which worked on a test project, but caused the main one to crash everytime upon loading on mobile. I may understand why now. I think it must be loading every single combination of the standard shader. A test project with only a cube and a slider to change opacity may withstand it, but a game with many things more, not so much.
Your answer
Follow this Question
Related Questions
Transparent Detail on mobile 0 Answers
Glass material or shader for mobile? iOS Android 1 Answer
Specular bug on iOS/Android using Standard shader 2 Answers
Realistic metalic on mobile 0 Answers
Fluid Shader 2 Answers