Manage keywords in Shader Graph
Get started with adding and managing keywords in a Shader Graph.
Add a keyword in a Shader Graph
To add a keyword in a Shader Graph:
Add a keyword in the Blackboard and define the keyword parameters according to your needs.
Add a Keyword Node in the graph from the keyword you defined in the Blackboard.
Unity declares the keyword in the final shader code.
Make the shader behavior conditional on the keyword
To make the shader behavior conditional on the keyword in the Shader Graph:
Create the upstream Shader Graph branches that define the various behaviors you want to use conditionally.
Connect each branch to a different input port of the Keyword Node according to the keyword value you want to use for later toggling.
Connect the output port of the Keyword Node to the node port you want to apply the conditional graph part to.
Unity adds all the conditions and branches in the final shader code.
Toggle the shader keyword in the Editor
To be able to control a keyword from the Material Inspector, make sure to enable Generate Material Property in the keyword parameters in the graph.
Toggle the shader keyword in a script
To enable a Boolean keyword from a script, use EnableKeyword
on the keyword's Reference Name. DisableKeyword
disables the keyword. To learn more about Boolean keywords, refer to Shader variants and keywords.
When controlling an Enum keyword via script with a Material.EnableKeyword
or Shader.EnableKeyword
function, enter the state label in the format {REFERENCE}_{REFERENCESUFFIX}
. For example, if your reference name is MYENUM and the desired entry is OPTION1, then you would call Material.EnableKeyword("MYENUM_OPTION1")
. When you select an option, you must also disable the other options to see the effect.
Note: By default, when you add a keyword, Unity adds an underscore to the start of Reference Name. As a result, a keyword with the name MYENUM has the reference name _MYENUM.