本页面包含有关在 ShaderLab 代码中使用 Name
代码块为通道指定名称的信息。有关定义通道的信息,请参阅 ShaderLab:定义一个通道。有关 Shader 对象的工作原理以及 Shader 对象、子着色器与通道之间关系的信息,请参阅 Shader 对象基础。
通道可以具有名称。您需要在 UsePass
命令及某些 C# API 中按名称引用一个通道。通道的名称在帧调试器工具中可见。
功能名称 | 内置渲染管线 | 通用渲染管线 (URP) | 高清渲染管线 (HDRP) | 自定义 SRP |
---|---|---|---|---|
ShaderLab:Name 代码块 | 是 | 是 | 是 | 是 |
要在 ShaderLab 中为通道指定名称,您可以在 Pass
代码块内放置一个 Name
代码块。
签名 | 功能 |
---|---|
Name "<name>" |
设置通道的名称。 |
在内部,Unity 将名称转换为大写。在 ShaderLab 代码中引用名称时,必须使用大写变体;例如,如果值是 “example”,您必须使用 EXAMPLE 进行引用。
如果同一个子着色器中有多个通道具有相同的名称,则 Unity 使用代码中的第一个通道。
To access the name of a Pass from C# scripts, you can use APIs such as Material.FindPass, Material.GetPassName, or ShaderData.Pass.Name.
注意:Material.GetShaderPassEnabled 和 Material.SetShaderPassEnabled 不按名称引用通道;而是使用 LightMode 标签的值引用通道。
此示例代码创建了一个名为 ContainsNamedPass 的 Shader 对象,其中包含名为 ExampleNamedPass 的通道。
Shader "Examples/ContainsNamedPass"
{
SubShader
{
Pass
{
Name "ExampleNamedPass"
// 此处是定义通道的代码的其余部分。
}
}
}
然后您可以使用以下 C# 代码查询此通道的名称:
using UnityEngine;
public class GetPassName : MonoBehaviour
{
// 将此脚本放置在具有 MeshRenderer 组件的游戏对象上
void Start() {
// 获取材质
var material = GetComponent<MeshRenderer>().material;
// 获取为该材质分配的 Shader 对象的
// 活动子着色器中第一个通道的名称
var passName = material.GetPassName(0);
// 将名称打印到控制台
Debug.Log(passName);
}
}
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.