着色器可以定义 Unity 材质检视面板中由美术师设置的参数列表。着色器文件中的 Properties 代码块将定义这些属性。
Properties { Property [Property ...]}
定义属性代码块。在大括号内,多个属性定义如下。
name ("display name", Range (min, max)) = number
name ("display name", Float) = number
name ("display name", Int) = number
这些全都定义一个具有默认值的数字(标量)属性。Range
格式使该属性显示为一个滑动条,范围在 min 到 max 之间。
name ("display name", Color) = (number,number,number,number)
name ("display name", Vector) = (number,number,number,number)
使用给定 RGBA 分量的默认值定义颜色属性,或使用默认值定义 4D 矢量属性。颜色属性会显示拾色器,并根据颜色空间按需进行调整(请参阅着色器程序中的属性)。矢量属性显示为四个数字字段。
name ("display name", 2D) = "defaulttexture" {}
name ("display name", Cube) = "defaulttexture" {}
name ("display name", 3D) = "defaulttexture" {}
Defines a 2D Texture, cubemap or 3D (volume) property respectively.
着色器中的每个属性均通过 name 引用(在 Unity 中,着色器属性名称通常以下划线开头)。属性在材质检视面板中将显示为 display name。每个属性都在等号后给出默认值:
稍后在着色器的固定函数部分中,可使用括在方括号中的属性名称来访问属性值:[name]。例如,可通过声明两个整数属性(例如“SrcBlend“和”DstBlend”)来使混合模式由材质属性驱动,然后让 Blend 命令使用它们:Blend [_SrcBlend] [_DstBlend]
。
Properties
代码块中的着色器参数被序列化为材质数据。着色器程序实际上可以有更多参数(如矩阵、矢量和浮点数),这些参数在运行时从代码中在材质上设置,但如果它们不是 Properties 代码块的一部分,则不会保存它们的值。这对于完全由脚本代码驱动的值最有用(使用 Material.SetFloat 和类似函数)。
在属性前面,可指定可选的特性(用方括号括起)。这些是 Unity 可以识别的特性,或者它们可以指示您自己的 MaterialPropertyDrawer 类 来控制它们在材质检视面板中的呈现方式。Unity 可以识别的特性包括:
[HideInInspector]
- does not show the property value in the Material inspector.[NoScaleOffset]
- material inspector will not show Texture tiling/offset fields for Texture properties with this attribute.[Normal]
- indicates that a Texture property expects a normal-map.[HDR]
- indicates that a Texture property expects a high-dynamic range (HDR) Texture.[Gamma]
- 表示在 UI 中将浮点/矢量属性指定为 sRGB 值(就像颜色一样),并且可能需要根据使用的颜色空间进行转换。请参阅着色器程序中的属性。[PerRendererData]
- indicates that a property will be coming from per-renderer data in the form of a MaterialPropertyBlock. Material inspector shows these properties as read-only.[MainTexture]
- 表示一个属性 (property) 是材质的主纹理。默认情况下,Unity 将属性 (property) 名称为 _MainTex
的纹理视为主纹理。如果您的纹理具有其他属性 (property) 名称,但您希望 Unity 将这个纹理视为主纹理,请使用此属性 (attribute)。如果您多次使用此属性 (attribute),则 Unity 会使用第一个属性 (property),而忽略后续属性 (property)。[MainColor]
- 表示一个属性 (property) 是材质的主色。默认情况下,Unity 将属性 (property) 名称为 _Color
的颜色视为主色。如果您的颜色具有其他属性 (property) 名称,但您希望 Unity 将这个颜色视为主色,请使用此属性 (attribute)。如果您多次使用此属性 (attribute),则 Unity 会使用第一个属性 (property),而忽略后续属性 (property)。// 水着色器的属性
Properties
{
_WaveScale ("Wave scale", Range (0.02,0.15)) = 0.07 // 滑动条
_ReflDistort ("Reflection distort", Range (0,1.5)) = 0.5
_RefrDistort ("Refraction distort", Range (0,1.5)) = 0.4
_RefrColor ("Refraction color", Color) = (.34, .85, .92, 1) // 颜色
_ReflectionTex ("Environment Reflection", 2D) = "" {} // 纹理
_RefractionTex ("Environment Refraction", 2D) = "" {}
_Fresnel ("Fresnel (A) ", 2D) = "" {}
_BumpMap ("Bumpmap (RGB) ", 2D) = "" {}
}
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.