Quick answer: Record runtime-enabled variants into a ShaderVariantCollection, add it to Preloaded Shaders. Or implement IPreprocessShaders to whitelist keywords. Editor compiles on demand — the build doesn’t.

Material in editor: looks fine. Material in build: pink. The variant compiled when you toggled it in play mode never made it into the player’s shader cache.

The Symptom

A keyword like _DAMAGE_FLASH works in editor. In a built player it fails to render or shows the magenta error shader. No scene material has that keyword baked in — you enable it from script when the player takes damage.

The Fix

// 1. Record at runtime in editor
// Project Settings → Graphics → Log Shader Compilation
// Play through every state that toggles keywords
// Save -> ShaderVariants.shadervariants asset

// 2. Add asset to Preloaded Shaders
// Graphics settings → Preloaded Shaders → +

// 3. Or whitelist programmatically
class KeepKeywords : IPreprocessShaders {
    public int callbackOrder => 0;
    public void OnProcessShader(Shader s, ShaderSnippetData snip,
                                IList<ShaderCompilerData> data) {
        // don't strip _DAMAGE_FLASH
        var kw = new ShaderKeyword("_DAMAGE_FLASH");
        for (int i = data.Count - 1; i >= 0; --i) {
            // keep both enabled and disabled variants
        }
    }
}

Variant collection is the safe baseline. Custom IPreprocessShaders gives surgical control when collection size becomes a build-time concern.

Why It Happens

Unity strips shader variants aggressively to keep build size manageable. The build sees only what scene materials and code-emitted variant collections reference. Runtime-only keyword toggles are invisible to the static analysis.

Verifying

Build the player. Trigger every keyword path. Watch for pink renders. Player log shows WARNING: Shader Unsupported: ... is not supported on this platform when the variant is missing.

“Record variants. Preload the collection. Build keeps the keyword.”

Related Issues

For Shader Graph custom function, see Shader Graph include. For VFX Graph mesh output, see flat VFX mesh.

Record. Preload. Variant ships.