How to Enable/Disable Effects at Runtime
There are 2 ways of achieving this.
Option 1: Change the property values
Section titled “Option 1: Change the property values”Use this one. Every effect has a value combination that makes it look deactivated, usually its blend or amount property at 0, though it varies by effect. So the clean way is to enable in the editor every effect the material will ever use, and then change the property values, either by animating them or from a script.
mat.SetFloat("_HitBlend", 0f); // effect enabled, nothing visiblemat.SetFloat("_HitBlend", 1f); // fully onNothing recompiles, nothing hitches, and the material keeps rendering with the same shader variant all the way through. See Scripting for a worked example.
Option 2: Toggle the shader keywords (not recommended)
Section titled “Option 2: Toggle the shader keywords (not recommended)”This other way is less efficient and messier, and it can make materials invisible or turn objects pink in the final build.
Here is why. Each set of enabled effects is a separate compiled shader variant, and Unity only builds the variants your project actually uses. Switching any keyword, on or off, moves the material onto a different variant, and if nothing in your project uses that particular set, it was never built. The editor compiles variants on demand, so this always looks correct in play mode and only shows up on the device.
Material mat = GetComponent<Renderer>().material;
mat.EnableKeyword("_EMISSION_ON");mat.DisableKeyword("_EMISSION_ON");Making sure the variants exist
Section titled “Making sure the variants exist”If you go this route, every combination you can end up on has to be in the build:
- Keep a material in your project for each combination you will switch to. Unity builds the variants its materials use, so a saved material with that exact set of effects guarantees the variant ships. If an effect disappears or an object turns pink in a build, this is the first thing to check.
- Include the variants explicitly, through a Shader Variant Collection added to the preloaded shaders in Graphics settings, or whatever variant inclusion your project already has.
This asset also gives you a third option the others do not: baking the keywords into a dedicated shader variant, which avoids the problem by producing a shader with those effects compiled in. A baked shader can’t toggle anything, so you switch by assigning a different baked shader.
Then test on the target platform. Unless you understand how Unity strips shader variants and you have a reason option 1 will not work, stay on option 1.
Finding the keyword names
Section titled “Finding the keyword names”Every effect keyword is listed on Effects and Properties Breakdown, including the sub-toggles. Those have a float property, but the keyword is what switches them, so set the keyword from script.
From version 2.81 you can also copy a keyword straight from the Material Inspector: right click the effect’s toggle and pick Copy keyword. An effect with several modes lists the keyword of every mode, with the current one ticked. A sub-toggle copies its float property name instead, so take its keyword from the Breakdown.
Keywords in this asset are underscore prefixed: _EMISSION_ON, not EMISSION_ON. Effects with several modes have one keyword per mode rather than an on and off, so a Light Model is switched by enabling _LIGHTMODEL_TOON rather than by toggling a single keyword.
