How to Enable/Disable Effects at Runtime
Last updated
Last updated
There are 2 ways of achieving this:
All effects have a property value combination that makes them look deactivated (usually by reducing the amount or blend property to 0, but it may vary depending on the effect). So the most clean way of deactivating and activating effects is by enabling all the effects you’ll use and then dynamically changing the property values either by animating the properties or by modifying the values by script as seen in the Scripting section.
This other way is less efficient, messier and will cause materials to become invisible in the final build if you set a combination of effects that isn’t included in some other material in your project. So be warned, use this with caution (in fact you should probably avoid this option and use option 1 instead) and test it on the target platform. If material effects disappear or you get a graphics error (object turns pink) at some point make sure to have some material in your project that includes the same set of effects than the material that isn’t showing.
This method consists on enabling and disabling the shader compilation flags at runtime, so Unity will compile and replace the shader at runtime (on a final build shaders can’t be compiled, so a shader variant with the new keywords will need to be available to avoid the errors mentioned). If you are sure to have a shader variant for the resulting toggle combination you can use the Enable/Disable Keyword method like so:
(Keyword names of every effect can be found at the Effects and Properties Breakdown section)
If you really want to use this feature and you really know what you are doing and how to prevent the aforementioned errors you can also find the effect name by hovering it with the mouse in the Material Inspector: