Transparency, Render Queue, Depth, Sorting ZWrite Problem - on Build

I have the same problem, and have looked at articles/posts everywhere trying to figure out. I cannot figure out the solution either.
It may be:
1. Render Queue problem?
2. A shader problem?
3. Something needs to be adjusted in the settings?
** I don’t think its a bug, then I’ve been seeing posts about this all the way back to 2015.
** Code seems to work fine, because it works okay inside unity…just not on build.

Attached is the Video and Code of what is happening

0yvst

// Update rendering mode
                    if (visible)
                    {
                        // Opaque
                        material.SetFloat("_Mode", 0f);
                        material.SetInt("_ZWrite", 1);
                        material.EnableKeyword("_ALPHATEST_ON");
                        material.DisableKeyword("_ALPHABLEND_ON");
                        material.EnableKeyword("_ALPHAPREMULTIPLY_ON");
                    }
                    else
                    {
                        // Transparent
                        material.SetFloat("_Mode", 3f);
                        material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
                        material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
                        material.SetInt("_ZWrite", 0);
                        material.DisableKeyword("_ALPHATEST_ON");
                        material.EnableKeyword("_ALPHABLEND_ON");
                        material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
                        material.renderQueue = 3000;
                    }

                    // Update color
                    if (material.HasProperty("_Color"))
                    {
                        var color = material.color;
                        color.a = (visible ? 1f : 0.2f);
                        material.color = color;
                    }

You’re enabling features on the material that are for shader variations that are getting stripped from the build. You need to use a ShaderVariantCollection to make sure they’re included. Run in the editor for a while, then create one from the Graphics settings menu by clicking on Save to asset… at the bottom of the inspector window.
https://docs.unity3d.com/Manual/OptimizingShaderLoadTime.html

1 Like

That worked! For others wondering how exactly it got fixed see below:

  • I went to Edit > Project Settings > GraphicSettings

  • Hit Reset (on the gear)

  • Scroll down to “Save to Asset” a NewShaderVariants

  • On the NewShaderVariants

  • Click the + under Standard (Specular Setup)

  • Add whatever shader variant you had in your code, In my case it was “AlphaTest”, “AlphaBlend”,AlphaPremultiply" / ForwardBaseDirectional_Alpha***

  • Go back to Edit > Project Settings > GraphicSettings

  • PreloadShaders > Size 1

  • Element 0 load up your NewShaderVariants you just created, and edited.

Thats it!