keyword | 要启用的 LocalKeyword。 |
keyword | 要启用的 LocalKeyword 的名称。 |
为该计算着色器启用局部着色器关键字。
着色器关键字决定 Unity 使用哪些着色器变体。有关使用 局部着色器关键字 和 全局着色器关键字 以及它们如何交互的信息,请参阅 使用 C# 脚本的着色器关键字。
如果您传入一个 LocalKeyword,并且它在 keywordSpace 中不存在,则此函数无效。如果您传入一个字符串,并且具有该 name
的 LocalKeyword 在 keywordSpace 中不存在,则此函数无效。
采用字符串作为参数的此函数版本比采用 LocalKeyword 作为参数的版本慢。如果您多次调用此函数,最佳实践是创建一个 LocalKeyword 结构体,将其缓存并使用它。
注意:LocalKeyword
特定于单个 Shader 或 ComputeShader 实例。您不能将其与其他 Shader 或 ComputeShader 实例一起使用,即使它们声明了名称相同的关键字。
以下示例创建一个名为 EXAMPLE_FEATURE_ON
的 LocalKeyword
结构体,并将其缓存。它提供了启用和禁用它的函数。
using UnityEngine; using UnityEngine.Rendering;
public class ComputeKeywordExample : MonoBehaviour { public ComputeShader computeShader; private LocalKeyword exampleFeatureKeyword;
void Start() { // Create and cache the LocalKeyword exampleFeatureKeyword = new LocalKeyword(computeShader, "EXAMPLE_FEATURE_ON"); }
public void EnableExampleFeature() { computeShader.EnableKeyword(exampleFeatureKeyword); }
public void DisableExampleFeature() { computeShader.DisableKeyword(exampleFeatureKeyword); } }
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.