Transparency not working in Windows build

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.

I figured it out. FIrst I created a new Shader Variant Collecton object in my project, then I edited it to add an entry for a standard shader with a ForwardBase DIRECTIONAL_ALPHA_ON. Then I editied my Project Settings/Graphics/ Shader Preloading to include a reference to my Shader Variant Collection in the Preloaded Shaders array.

The Unity docs go into more details:

Hi, I have the same issue (i want to make an object transparent when the player is hidden, like a roof when the player is in the house).
I use the same code as you from “Material m = …” to “…queue = 3000”.
I tried to create a new Shader Variant Collection, I had theses keyword :

  1. ForwardBase DIRECTIONAL_ALPHATEST_ON
  2. ForwardBase DIRECTIONAL_ALPHABEND_ON
  3. ForwardBase DIRECTIONAL_ALPHAPREMULTIPLY_ON

And then I save it and added it in the graphic settings as the doc says.

But the problem is still there.
Did you remenber if you did anything else ? or maybe how to try to debug this.
Thanks :slight_smile:

In version 2017.4.16 it also does not work in the Editor.