Version: Unity 6.0 (6000.0)
语言 : 中文
可绑定元素参考
绑定系统实现细节

可绑定的数据类型和字段

本节介绍 PropertyField 支持的数据类型转换和字段。

数据类型转换

UI 工具包中的大多数内置控件都实现了特定数据类型的 INotifyValueChanged 接口。例如,DoubleField 实现了 INotifyValueChanged<Double>,这意味着可以将 DoubleField 控件绑定到 double 类型的属性。您可以使用绑定系统绑定更多数据类型。例如,可以将 int 类型的属性绑定到 DoubleField

下表列出了可以绑定的源和目标数据类型:

源数据类型 INotifyValueChanged 的目标数据类型
long
  • long
  • int
  • string
  • float
int
  • int
  • string
  • float
double
  • double
  • float
float
  • float
  • double
char
  • char
  • string

注意:为了防止数据丢失,请使用 PropertyField 或使用适当的数据类型,如下面的 Fields supported by PropertyField 表所示。

PropertyField 支持的字段

填充 Inspector 窗口时,如果某个类型的自定义编辑器不存在,绑定系统将调用 InspectorElement.FillDefaultInspector()。这会为 SerializedObject 上的每个 SerializedProperty 创建一个 PropertyField

每个 PropertyField 下面都有一个匹配的控件,例如表示 intIntegerField 或表示 boolToggle。如果属性有子属性,则 PropertyField 具有 Foldout 控件。该 Foldout 在每个子属性下都有相应的默认控件。如果为属性创建了自定义 PropertyDrawer,则会改用从 PropertyDrawerCreatePropertyGUI() 方法返回的视觉树。

注意

创建自定义__ UI__(即用户界面,User Interface)让用户能够与您的应用程序进行交互。Unity 目前支持三种 UI 系统。更多信息
See in Glossary
时,除非找不到特定控件,否则不要使用 PropertyField。例如,要在视觉树中创建控件并将其绑定到 Color,请使用 ColorField,不要使用 PropertyField。这使得视觉树更加轻量级,操作速度更快。

使用 PropertyField 会产生额外的开销,因为它会解析绑定后要使用的具体字段。相关的额外步骤可能会影响性能。但是,PropertyField 对于某些用途是有益的。例如,它支持自定义绘制器,您可以将其用于以个性化方式处理特定属性。这样无需猜测绘制的字段类型,从而减少错误。

下表列出了受 PropertyField 支持的字段及其数据类型:

字段 数据类型
BoundsField Bounds
BoundsIntField BoundsInt
ColorField Color
CurveField AnimationCurve
FloatField float
GradientField Gradient
Hash128Field Hash128
IntegerField int
IntegerField int(对于 ArraySize)
LayerMaskField unit
LongField long
ObjectField UnityEngine.Object
PopupField<string> Enum
RectField Rect
RectIntField RectInt
TextField string
TextField,且 maxLength=1 char
Toggle bool
Vector2Field Vector2
Vector2IntField Vector2Int
Vector3Field Vector3
Vector3IntField Vector3Int
Vector4Field Vector4

其他资源

可绑定元素参考
绑定系统实现细节