How to Enable/Disable Effects at Runtime
Last updated
Last updated
There are 2 ways of achieving this:
[Use 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 previous section.
[Not recommended way] This other way is less efficient, messier and will cause sprites 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 and test it on the target platform. If sprites disappear at some point make sure to have some material in your project that includes the same set of effects than the sprite that isn’t showing. If you must use this feature I recommend having all the effects you’ll need enabled when you are in the editor and to disable them in the Start method. This will prevent any error in the final build. This method consists on enabling and disabling the shader compilation flags at runtime, so Unity will compile and replace the shader at runtime. To do so you first need to have a reference to the material and then 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 errors you can find the effect name by hovering it with the mouse in the Material Inspector: