I’m having some trouble with a mesh that is rendering with alpha fine in my viewport not being rendered with alpha when I run it in my Windows build.
I have a skinned character that I’m using for an enemy that is using the standard shader for rendering. When it is killed, I play a fall over animation and then want it to slowly fade out. I’m doing this by changing the shader’s color’s alpha each Update():
if (!init)
{
init = true;
//Switch from opaque to transparent
Material m = meshRend.materials[0];
m.SetFloat("_Mode", 2);
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;
return false;
}
elapsedTime += Time.deltaTime;
float val = Mathf.Max(1 - (elapsedTime / waitTime), 0);
meshRend.material.color = new Color(1, 1, 1, val);
This works fine when I’m running in the Unity editor window, but when I’m running my windows build, the alpha fade doesn’t happen (although the Z test does appear to be disabled once my init section is run).
I read in another thread that this might be due to Unity not compiling shaders with options that are not be used. I tried including the Standard shader in the list of shaders in Project Settings/Graphics/Always Included Shaders (as per one suggestion), but that just caused Unity to freeze during the build. I also tried creating a standard shader material that rendered with transparency, set it’s alpha to .5 and assigned it to a dummy cube in my scene (to force Unity to render using a Standard shader with alpha). However, while the cube did render with transparency, my character is still not fading out.