Toggle shader keywords in a script
Check if a keyword is enabled or disabled, and enable and disable it.
阅读时间3 分钟最后更新于 23 天前
Use a C# script to enable or disable keywords, and check whether a keyword has local or global scope.
Toggle a keyword
To change the state of a keyword, use the API. For example, .
MaterialMaterial.EnableKeyword()Toggle keywords in multiple shaders
By default, HLSL shader 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:
- Create a object with the same name as the keywords you want to enable or disable.
GlobalKeyword - Use the API to enable or disable the global keyword, which adjusts the HLSL keyword with the same name in all shaders. For example,
Shader.Shader.EnableKeyword
Prevent global keyword objects affecting shaders
To prevent global keyword objects affecting shader keywords, give the shader keyword local scope instead of global scope.
Add to the keyword directive. For example:
_local#pragma shader_feature_local RED
Now the shader isn't affected by global APIs. You can only enable or disable the keyword using APIs like .
ShaderMaterialMaterial.EnableKeyword()Check the scope of a keyword
To check the scope of a keyword in a shader, select the shader in the Project window, then in the window open the Keywords dropdown. Unity displays keywords with global scope under Overridable, and keywords with local scope under Not Overridable.
Inspector
?
You can also use the LocalKeyword.isOverridable API.
To check if a keyword is enabled, do the following:
- If the keyword is overridable, check whether a C# global keyword with the same name exists. If so, check the state of the keyword using the API. For example,
Shader.Shader.IsKeywordEnabled() - If the keyword isn't overridable, check the state of a keyword using the API. For example,
Material.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); } } }}
Change a keyword with a command buffer
To enable or disable a local or global keyword with a Command Buffer, use or .
CommandBuffer.EnableKeywordCommandBuffer.DisableKeyword