Use a C# script to enable or disable keywords, and check whether a keyword has local or global scope.
To change the state of a keyword, use the Material
API. For example, Material.EnableKeyword()
.
By default, HLSL shaderA program that runs on the GPU. More info
See in Glossary keywords have global scope. As a result, to toggle keywords with the same name across multiple shaders, you can use a C# global keyword object.
Follow these steps:
GlobalKeyword
object with the same name as the keywords you want to enable or disable.Shader
API to enable or disable the global keyword, which adjusts the HLSL keyword with the same name in all shaders. For example, Shader.EnableKeyword
.
Note: When you create a new GlobalKeyword
, Unity updates its internal mapping between global and local keyword space for all loaded shaders. This can be a CPU-intensive operation. To reduce the impact of this operation, try to create all global keywords after application startup, while your application is loading.
To prevent global keyword objects affecting shader keywords, give the shader keyword local scope instead of global scope.
Add _local
to the keyword directive. For example:
#pragma shader_feature_local RED
Now the shader isn’t affected by global Shader
APIs. You can only enable or disable the keyword using Material
APIs like Material.EnableKeyword()
.
Note: If you use Fallback or UsePass in a shader, its keyword scope overrides the keyword scopes of the referenced shaders.
To check the scope of a keyword in a shader, select the shader in the Project window, then in the InspectorA Unity window that displays information about the currently selected GameObject, asset or project settings, allowing you to inspect and edit the values. More info
See in Glossary window open the Keywords dropdown. Unity displays keywords with global scope under Overridable, and keywords with local scope under Not Overridable.
You can also use the LocalKeyword.isOverridable API.
Note: The term Local
in LocalKeyword
means the API represents an HLSL keyword rather than a C# keyword object. It doesn’t relate to the scope of the keyword.
To check if a keyword is enabled, do the following:
Shader
API. For example, Shader.IsKeywordEnabled()
.Material
API. For example, Material.IsKeywordEnabled()
.This following code demonstrates how to check whether Unity considers a keyword enabled or disabled for a material:
using UnityEngine;
using UnityEngine.Rendering;
public class KeywordExample : MonoBehaviour
{
public Material material;
void Start()
{
CheckShaderKeywordState();
}
void CheckShaderKeywordState()
{
// Get the instance of the Shader class that the material uses
var shader = material.shader;
// Get all the local keywords that affect the Shader
var keywordSpace = shader.keywordSpace;
// Iterate over the local keywords
foreach (var localKeyword in keywordSpace.keywords)
{
// If the local keyword is overridable (i.e., it was declared with a global scope),
// and a global keyword with the same name exists and is enabled,
// then Unity uses the global keyword state
if (localKeyword.isOverridable && Shader.IsKeywordEnabled(localKeyword.name))
{
Debug.Log("Local keyword with name of " + localKeyword.name + " is overridden by a global keyword, and is enabled");
}
// Otherwise, Unity uses the local keyword state
else
{
var state = material.IsKeywordEnabled(localKeyword) ? "enabled" : "disabled";
Debug.Log("Local keyword with name of " + localKeyword.name + " is " + state);
}
}
}
}
To enable or disable a local or global keyword with a Command Buffer, use CommandBuffer.EnableKeyword
or CommandBuffer.DisableKeyword
.
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.