文档

Unity Engine


User Manual

Script Reference

Unity Engine


Changing how shaders work using keywords

Resources and techniques for writing shader code so it uses conditionals to do different things under different circumstances.
阅读时间1 分钟最后更新于 1 个月前

Resources and techniques for adding shader keywords, using them to create branches and shader variants, and toggling them in the Unity Editor or in a script.

Page

Description

Shader keywords workflowLearn about defining shader keywords to create shaders that share some common code, but have different functionality when you enable or disable a keyword.
How Unity compiles branching shadersChoose dynamic branching or shader variants.
Declare shader keywordsUse a
#pragma
directive in HLSL code to declare keywords.
Make shader behavior conditional on keywordsUse an
if
statement in HLSL code to mark parts of your shader code conditional based on whether you enable or disable a shader keyword.
Toggle shader keywords in the EditorAdd material properties for shader keywords.
Toggle shader keywords in a scriptCheck if a keyword is enabled or disabled, and enable and disable it.
Add built-in keyword setsUse Unity shader directive shortcuts to create sets of shader keywords and variants.

Additional resources