UI Toolbox 徽标UI Toolbox
支持

本页为译文。技术支持和视频教程主要使用英语,内容以英文页面为准。 英文版

PropertyGrid

UIToolbox 中的 PropertyGrid 显示 Target 的公共字段和属性,并可编辑它们。每一行是成员类型的编辑器。搜索框用于过滤行。"A-Z" 按钮在类别折叠区和一个按字母排序的列表之间切换。底部的面板显示选中行的说明。

分为 Audio、Graphics 和 Gameplay 组的设置,带搜索框和所选行的帮助文本分为 Audio、Graphics 和 Gameplay 组的设置,带搜索框和所选行的帮助文本

<tb:PropertyGrid
    name="grid"
    sort-mode="Categorized"
    show-search="true"
    show-description="true" />
class Settings
{
    [Category("Text"),
        Description(
            "The text in the box.")] public string caption = "Hello";
    [Category("Size"), Range(40, 200)] public int width = 120;
    public Color fill = Color.blue;
    // no public setter: read-only row
    public int Edits { get; internal set; }
    [DisplayName("Health")] public int MaxHealth { get; set; }
    // a dropdown of the texts of sizes()
    [Choices(nameof(sizes))] public string Size = "M";
    public bool Outline;
    [EnabledIf(nameof(Outline))] public float OutlineWidth = 1;
    private string[] sizes() => new[] { "S", "M", "L" };
}

grid.Target = new Settings();
grid.PropertyValueChanged += (item, old, now) =>
    Debug.Log($"{item.Name}: {old} > {now}");
// call item.SetValue(v) to write
grid.RegisterEditor(typeof(Color), item => myColorPicker(item));
特性 效果
[Category] 该行所在的折叠区。默认是 "Misc"。
[Description]、[Tooltip] 行的工具提示和说明面板的文字。
[DisplayName](仅属性)、[InspectorName] 行的标签。否则是成员名称,经过美化:maxHealth 变成 "Max Health"。C# 不允许在字段上使用 [DisplayName];字段请使用 [InspectorName]。
[Choices(member)] string 或 int 的下拉列表。member 是同一对象的字段、属性或无参数的方法(任何访问级别),返回一个集合。string 取所选的文本,int 取它的索引。网格在每次更改后重新读取该列表。
[Selector] 用 OptionSelector 代替 [Choices] 行的下拉列表,或代替枚举行的枚举字段。带 flags 的枚举保留它的开关。在网格上设置 UseSelectors(use-selectors)可对所有行这样做。
[EnabledIf(member, values)] 只有在 member 为 true 或等于 values 之一时,该行才启用。网格在每次更改后重新检查。
[Range] 用于 int、float、double、short、ushort、byte、sbyte、long、uint 和 ulong 的滑块。
[Multiline]、[TextArea] string 的多行文本字段。
[ReadOnly(true)] 禁用的行。readonly 字段和没有公共 setter 的属性同样被禁用。
[Browsable(false)]、[HideInInspector]、[Obsolete] 该成员没有行。
类型 编辑器
int、float IntegerField、FloatField;带 [Range] 时为 SliderInt、Slider
long、double、string LongField、DoubleField、TextField(延迟:值在 Enter 或失去焦点时更改)。带 [Range] 的 double 得到 Slider;带 [Range] 的 long 得到 SliderInt,超出 int 限制时得到 long 滑块。
uint、ulong UnsignedIntegerField、UnsignedLongField;带 [Range] 时为 SliderInt,超出 int 限制时为 long 滑块。
short、ushort、byte、sbyte IntegerField;带 [Range] 时为 SliderInt。值会被限制在类型的范围内。
bool Toggle
enum EnumField;[Flags] 枚举每个标志得到一个开关
Vector2 到 Vector4、Vector2Int、Vector3Int、Rect、RectInt、Bounds、BoundsInt 对应的 UI Toolkit 字段
Color 紧凑的 ColorPicker:行中的一个色块。点击色块会在弹出窗口中打开完整的取色器。
AnimationCurve CurveField:曲线的预览。点击会在弹出窗口中打开曲线编辑器。
Gradient GradientField:颜色的预览。点击会在弹出窗口中打开渐变编辑器。
DateTime、TimeSpan DatePicker、TimeSpanPicker
你自己的类或结构体 折叠区,每个成员一行。null 对象显示 "null",直到代码设置它并调用 Refresh()。
其他类型 显示 ToString() 的只读文本字段。集合显示其类型和数量,例如 "List (3)"。
成员 说明
Target 要编辑的对象。设置时会构建各行并清除撤销历史。
SortMode(sort-mode) PropertyGridSort:Categorized 或 Alphabetical。
DeclaredCategoryOrder(declared-category-order) True:类别按其第一个成员在代码中的顺序显示。False(默认):A 到 Z。
ShowSearch、ShowDescription(show-search、show-description) 工具栏和说明面板。
SearchText 搜索文本。它匹配标签和成员名称。
ShowClearButton(show-clear-button) 开(默认):搜索框有文本时显示 "x" 按钮,点击会清除文本。在搜索框中按 Escape 也会清除文本。见清除按钮。
Items、GetItem(path)、GetEditor(path)、SelectedItem 各行。路径是成员名称,或指向嵌套成员的点路径,例如 "stats.health"。元素路径带有索引,例如 "stops[2]" 或 "legs[0].health"。PropertyGridItem 有 Name、Path、Parent、Children、DisplayName、Category、Description、ValueType、IsReadOnly、Range、Index(成员为 -1)、GetValue() 和 SetValue(v),以及与特性相关的 IsEnabled(其 [EnabledIf] 条件为 false 时为 false)、UsesSelector 和 GetChoices()(其 [Choices] 成员的文本)。
InsertElement(index, value)、RemoveElement(index) 用于数组或列表的项:插入或移除一个元素。value 为 null 时给出元素类型的新元素。
MoveElement(from, to) 用于数组或列表的项:把一个元素移到另一个索引;其他元素随之移位。一次移动是一个撤销步骤。索引相同、索引超出集合或列表只读时返回 false。只读的数组成员会就地重新排序。每个元素行有一个 "▲" 和一个 "▼" 按钮;第一行不能上移,最后一行不能下移。
RegisterEditor(type, factory) 替换某个类型及可赋值给它的类型的编辑器。第一个匹配的注册生效。
Refresh() 重新读取值,用于网格之外所做的更改。
Undo()、Redo()、CanUndo、CanRedo 历史。Ctrl+Z 撤销;Ctrl+Y 和 Ctrl+Shift+Z 重做。
PropertyValueChanged(item, old, new) 在编辑、撤销和重做之后发送。

PropertyGridDemo 场景展示了由网格编辑的一只史莱姆,每个标签展示一项功能:对四个字段的初次观察;带说明的类别;范围滑块;枚举和 [Flags] 枚举;零食列表;嵌套的跳跃;带 Refresh 的只读行;撤销和重做;搜索和排序;以及带糖果外观的自定义编辑器。

样式

类 元素
tb-property-grid 网格(带边框;会增长)。
tb-property-grid__toolbar 带搜索框和排序按钮的行。
tb-property-grid__search 搜索框。
tb-property-grid__sort、tb-property-grid__sort--active "A-Z" 按钮,以及处于字母排序时的按钮(强调色)。
tb-property-grid__body 带各行的滚动视图。
tb-property-grid__category 类别折叠区(备用表面上的粗体页眉)。
tb-property-grid__row 一行:编辑器字段。标签占该行的 40%。
tb-property-grid__row--selected 选中的行(悬停表面,左侧强调色边框)。
tb-property-grid__long-input long 滑块中的精确数字字段(110 px,不收缩)。
tb-property-grid__nested 嵌套对象的折叠区。其内容按中等间距缩进。
tb-property-grid__collection 数组或列表的折叠区。它同时带有 tb-property-grid__nested。
tb-property-grid__element 带元素行及其移除按钮的一行。
tb-property-grid__move-up、tb-property-grid__move-down 元素的 "▲" 和 "▼" 按钮(20 x 18 px)。在集合两端它们被禁用。
tb-property-grid__remove 元素的 "-" 按钮(20 x 18 px)。
tb-property-grid__add 元素下方的 "+ Add" 按钮。
tb-property-grid__flags [Flags] 枚举的开关(可换行的一行)。
tb-property-grid__empty 显示 "No object"、"No properties" 或 "No properties match the search" 消息的 EmptyState。
tb-property-grid__description、__description-title、__description-text 说明面板(至少 48 px 高)、它的粗体标题和柔和文字。