UI Toolbox 徽标UI Toolbox
支持

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

按钮和内置控件

Unity 自己的控件在工作区中会采用主题:Button、Toggle、RadioButtonGroup、Slider、MinMaxSlider、TextField、DropdownField、Foldout、ProgressBar、ToggleButtonGroup 和 GroupBox。本页列出可添加到 Button 或 GroupBox 的类,以及有自己规则的内置控件。ButtonsDemo 和 Styles 示例场景在工作区中展示了它们。

这些类是 USS 类,所以在 UXML 和代码中都可使用。ButtonStyle 为每个类提供一个常量。

一行 Default、Save、Delete、Cancel、More 和 Terms 按钮变体,下方是 Small、Large、按钮组、加载中的 Saving 按钮、滑块和进度条一行 Default、Save、Delete、Cancel、More 和 Terms 按钮变体,下方是 Small、Large、按钮组、加载中的 Saving 按钮、滑块和进度条

按钮变体

没有类的 Button 使用表面颜色。变体类改变它的含义:

<ui:Button text="Default" />
<ui:Button text="Save" class="tb-button--primary" />
<ui:Button text="Delete" class="tb-button--danger" />
<ui:Button text="Cancel" class="tb-button--outline" />
<ui:Button text="More" class="tb-button--ghost" />
<ui:Button text="Read the terms" class="tb-button--link" />
save.AddToClassList(ButtonStyle.PRIMARY_CLASS);
类 常量 外观
tb-button--primary ButtonStyle.PRIMARY_CLASS 主要操作:--tb-color-accent 搭配 --tb-color-accent-text。
tb-button--danger ButtonStyle.DANGER_CLASS 删除或失去某些东西的操作:--tb-color-danger 搭配 --tb-color-danger-text。
tb-button--outline ButtonStyle.OUTLINE_CLASS 无填充,边框为 --tb-color-border。
tb-button--ghost ButtonStyle.GHOST_CLASS 无填充,无边框。指针下显示填充。
tb-button--link ButtonStyle.LINK_CLASS 文本为 --tb-color-accent,无填充,无边框。

按钮尺寸

<ui:Button text="Small" class="tb-button--sm" />
<ui:Button text="Default" />
<ui:Button text="Large" class="tb-button--lg tb-button--primary" />
<ui:Button text="+" class="tb-button--icon" />
<ui:Button text="+" class="tb-button--icon tb-button--lg" />
类 常量 外观
tb-button--sm ButtonStyle.SMALL_CLASS 高度至少 18 px,使用 --tb-font-size-sm。
tb-button--lg ButtonStyle.LARGE_CLASS 高度至少 44 px,使用 --tb-font-size-lg。
tb-button--icon ButtonStyle.ICON_CLASS 24 px 的方形,用于一个图标或一个字符。搭配 tb-button--sm 为 18 px,搭配 tb-button--lg 为 32 px。

尺寸类可与每个变体类一起使用,也可用于 ToggleButton。若要显示图片,请设置按钮的 iconImage 并让文本为空。

加载状态

ButtonStyle.SetLoading 将按钮显示为忙碌。按钮被禁用,并在文本前显示一个旋转指示器:

ButtonStyle.SetLoading(save, true);
await SaveAsync();
ButtonStyle.SetLoading(save, false);
成员 说明
ButtonStyle.SetLoading(button, loading) True 禁用按钮并显示旋转指示器。False 启用它并去掉旋转指示器。
ButtonStyle.IsLoading(button) 按钮显示旋转指示器期间为 True。
类 元素
tb-button--loading 加载期间位于按钮上。
tb-button__spinner 旋转指示器,一个 12 px、没有固定值的 ProgressRing。它采用变体的文本颜色。
tb-button__text 按钮加载期间的文本。

按钮组合

带类 tb-button-set 的容器把它的按钮连在一个框里。框有边框和圆角,两个按钮之间显示一条线:

<ui:VisualElement class="tb-button-set">
    <ui:Button text="Left" />
    <ui:Button text="Center" />
    <ui:Button text="Right" />
</ui:VisualElement>

<ui:VisualElement class="tb-button-set tb-button-set--vertical">
    <ui:Button text="Top" />
    <ui:Button text="Bottom" />
</ui:VisualElement>
类 常量 元素
tb-button-set ButtonStyle.SET_CLASS 容器。它的按钮排成一行。
tb-button-set--vertical ButtonStyle.SET_VERTICAL_CLASS 同样位于容器上:按钮排成一列。

ToggleButtonDemo 场景把聊天框的 Bold 和 Italic 按钮连成一个组合。

ToggleButtonGroup

Unity 的 ToggleButtonGroup 显示一行按钮,其中一个或多个处于开启状态。在工作区中,它采用分段外观:一个圆角框容纳按钮,一条细线分隔两个按钮,只有两个外侧端是圆的。开启的按钮使用强调色。选择变化时,布局不会移动:

<ui:ToggleButtonGroup label="View" name="view">
    <ui:Button text="List" />
    <ui:Button text="Icons" />
    <ui:Button text="Details" />
</ui:ToggleButtonGroup>
var view = root.Q<ToggleButtonGroup>("view");
view.RegisterValueChangedCallback(evt => ShowList(evt.newValue[0]));

对于始终选一项的分段控件,例如模式或密度设置,请设置 is-multiple-selection="false" 和 allow-empty-selection="false"。在工作区之外,给该组添加类 tb-segmented,即可得到同样的外观。

Unity 类 外观
unity-button-group 框:边框为 --tb-segmented-border,圆角为 --tb-segmented-radius。
unity-button-group__button 一个分段:--tb-segmented-fill 搭配 --tb-segmented-text,指针下为 --tb-segmented-fill-hover。
unity-button-group__button:checked 开启的分段:--tb-segmented-picked 搭配 --tb-segmented-picked-text。
unity-button-group__button:focus 有焦点的分段在其边框中显示焦点环。
unity-toggle-button-group__empty-label 没有按钮的组的文本:--tb-color-text-muted。

--tb-segmented-* 变量默认取主题的标记,因此外观跟随浅色、深色、游戏和高对比度主题。一个组可以设置自己的值:

.density-group {
    --tb-segmented-radius: 4px;
    --tb-segmented-picked: rgb(46, 125, 50);
}

左和右(方向键或左摇杆)在分段之间移动焦点,Enter 或确认键选中有焦点的分段。这些按键从不关闭窗口。已禁用的组或已禁用的分段保持禁用外观。

图标

带有 iconImage 的分段在其文本前显示图标,颜色与文本相同。请用白色在透明背景上绘制图标,这样着色就能给它任何颜色。给只有图标的组添加类 tb-segmented--icons,可去掉每个图标后的间隙:

<ui:ToggleButtonGroup name="mode"
        is-multiple-selection="false"
        allow-empty-selection="false">
    <ui:Button name="modeSystem" text="System" />
    <ui:Button name="modeLight" text="Light" />
    <ui:Button name="modeDark" text="Dark" />
</ui:ToggleButtonGroup>
var mode = root.Q<ToggleButtonGroup>("mode");
mode.Q<Button>("modeLight").iconImage =
    Background.FromTexture2D(sunIcon);
// 每个分段一个标志位:0b010 选中 Light
mode.value = new ToggleButtonGroupState(0b010, 3);
mode.RegisterValueChangedCallback(evt => ApplyMode(evt.newValue));

在设置卡片中

一行设置就是一个 Card,把组作为标题栏的操作。带有类 tb-card__action 的子元素位于标题右侧:

<tb:Card title="Density" description="The space around each line.">
    <ui:ToggleButtonGroup name="density"
            class="tb-card__action"
            is-multiple-selection="false"
            allow-empty-selection="false">
        <ui:Button text="Comfortable" />
        <ui:Button text="Compact" />
    </ui:ToggleButtonGroup>
</tb:Card>

在代码中,用 card.Action.Add(density) 添加该组。

对于只有开和关的单个按钮,请使用 ToggleButton。ToggleButtonDemo 场景有一个 Segmented 标签页,其中有图标的 Mode 组和设置卡片中的 Density 组,并把一个组用于物品栏的武器栏位。

MinMaxSlider

Unity 的 MinMaxSlider 用两个滑块选取一个范围。在工作区中,轨道和滑块与 Slider 一致,两个滑块之间的范围使用强调色:

<ui:MinMaxSlider
    label="Price"
    low-limit="0"
    high-limit="100"
    min-value="20"
    max-value="60" />
Unity 类 在工作区中的外观
unity-min-max-slider__tracker 轨道:--tb-color-surface-alt,边框为 --tb-color-border。滑动条因按键或游戏手柄而获得焦点期间,边框为 --tb-color-accent。
unity-min-max-slider__dragger 两个滑块之间的范围:--tb-color-accent。
unity-min-max-slider__min-thumb、__max-thumb 滑块:--tb-color-border,指针下为 --tb-color-surface-hover。

GroupBox

给 Unity 的 GroupBox 添加类 tb-group-box,得到标题位于顶线上的边框:

<ui:GroupBox text="Audio" class="tb-group-box">
    <ui:Slider label="Music" low-value="0" high-value="100" />
    <ui:Slider label="Effects" low-value="0" high-value="100" />
</ui:GroupBox>
类 外观
tb-group-box --tb-color-border 的边框,--tb-radius-md 的圆角,顶部留有放标题的外边距。
tb-group-box > .unity-group-box__label 标题,粗体,位于顶线上。它的背景是窗口的颜色 --tb-color-surface,所以线在标题下方中断。

在背景不同的面板上,请给标题设置相同的背景:

.my-panel .tb-group-box > .unity-group-box__label {
    background-color: var(--tb-color-surface-alt);
}

对于标题位于边框内的一组表单字段,请使用 FormField 的类 tb-field-set。

改变外观

在类前面写上 .unity-button,这样规则会压过控件库的规则。把样式表放在主题样式表之后。

/* Round primary buttons */
.unity-button.tb-button--primary { border-radius: 12px; }

/* A wider large button */
.unity-button.tb-button--lg { padding-left: 24px; padding-right: 24px; }

/* The range of a MinMaxSlider in the success color */
.tb-workspace .unity-min-max-slider__dragger {
    background-color: var(--tb-color-success);
}

其他 Unity 控件的规则,请参见工作区中的 Unity 控件。