Version: 6000.3
语言: 中文
纹理采样器
将信息传递到 HLSL 中的着色器编译器

在着色器中包含另一个 HLSL 文件

在 HLSL 中,#include指令是一种预处理器指令。它们指示编译器将一个 HLSL 文件的内容包含在另一个文件中。它们包含的文件称为包含文件

在 Unity 中,常规#include指令的工作方式与标准 HLSL 中相同。有关常规的更多信息#include指令,请参阅 HLSL 文档:include 指令

Unity 还提供了一个额外的特定于 Unity#include_with_pragmas命令。这#include_with_pragmas指令的工作原理与常规#include指令,但它也允许您使用#pragma指令。这意味着#include_with_pragmas指令允许您共享#pragma多个文件之间的指令。

使用 include_with_pragmas 指令

注意:使用#include_with_pragmas指令,则必须启用缓存着色器预处理器

此示例演示如何使用特定于 Unity 的#include_with_pragmas启用通用工作流改进的指令:切换着色器在 GPU 上运行的程序。更多信息
请参阅术语表
调试多个着色器,无需每次都编辑每个着色器源文件。

以下行演示了包含文件的内容。它包含启用着色器调试的单个编译指示:

// Comment out the following line to disable shader debugging
#pragma enable_d3d11_debug_symbols

在要调试的每个着色器中,添加一个#include_with_pragmas指向包含文件的指令。将指令放在另一个附近#pragma指令,如下所示:

// Example pragma directives
#pragma target 4.0
#pragma vertex vert
#pragma fragment frag

// Replace path-to-include-file with the path to the include file 
#include_with_pragmas "path-to-include-file"

// The rest of the HLSL code goes here

现在,当你想要为使用包含文件的所有着色器打开和关闭着色器调试时,只需更改包含文件中的一行。当 Unity 重新编译着色器时,它会包含修改后的行。

注意:如果着色器文件使用#include导入包含#include_with_pragmas指令,Unity 忽略#pragma指令#include_with_pragmas指令引用。

纹理采样器
将信息传递到 HLSL 中的着色器编译器