此着色器的本地关键字空间。
着色器关键字决定 Unity 使用哪些着色器变体。有关使用本地着色器关键字和全局着色器关键字以及它们如何交互的信息,请参见使用 C# 脚本的着色器关键字。keywordSpace
包含
此示例迭代图形着色器中本地关键字空间中的本地着色器关键字。它确定它们是否被全局着色器关键字覆盖,并打印其状态。
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, // 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); } } } }