使用 ToggleButtonGroup 从一组逻辑上的 Button 元素中启用单选或多选。ToggleButtonGroup 由一个标签和一组可交互的 Button 元素组成。
注意:要将元素与 Inspector一个 Unity 窗口,显示当前选中的 GameObject、资源或项目设置的信息,允许您检查和编辑这些值。 更多信息
参见 术语表 窗口中的其他字段对齐,只需将 .unity-base-field__aligned
USS 类应用于它。有关更多信息,请参阅 BaseField
。
您可以使用 UI(用户界面) 允许用户与您的应用程序交互。Unity 目前支持三种 UI 系统。 更多信息
参见 术语表 构建器、UXML 和 C# 创建 ToggleButtonGroup。
UI 构建器
在 UI 构建器中创建 ToggleButtonGroup
要在 UXML 中创建 ToggleButtonGroup,请将 ToggleButtonGroup 元素添加到 UXML 文档中,并添加一组 Button 元素作为其子元素。例如
C#
要在 C# 中创建 ToggleButtonGroup,请创建 ToggleButtonGroup 对象的新实例,并添加一组 Buttons。例如
ToggleButtonGroup myElement = new ToggleButtonGroup("Label text");
myElement.Add(new Button("Button 1"));
myElement.Add(new Button("Button 2"));
myElement.Add(new Button("Button 3"));
默认情况下,您一次只能选择一个按钮。要启用多选,请将 is-multiple-selection
设置为 true
。
要启用空选择,请将 allow-empty-selection
设置为 true
。
以下 UXML 示例创建了一个带有 Button 的 ToggleButtonGroup
<UXML xmlns="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements">
<ToggleButtonGroup label="UXML Toggle Button Group">
<Button text="A"/>
<Button text="B"/>
<Button text="C"/>
<Button text="D"/>
<Button text="E"/>
</ToggleButtonGroup>
</UXML>
以下 C# 示例说明了 ToggleButtonGroup 及其 Button 的一些可自定义功能
/// <sample>
// Create custom buttons with Text value only.
var csharpToggleButtonGroupWithButtonTextOnly = new ToggleButtonGroup("C# Toggle Button Group Buttons with Text");
csharpToggleButtonGroupWithButtonTextOnly.Add(new Button() { text = "one", tooltip = "custom button one" });
csharpToggleButtonGroupWithButtonTextOnly.Add(new Button() { text = "two", tooltip = "custom button two" });
container.Add(csharpToggleButtonGroupWithButtonTextOnly);
// Create custom buttons with IconImage value only.
var csharpToggleButtonGroupWithButtonIconOnly = new ToggleButtonGroup("C# Toggle Button Group Buttons with Icons");
csharpToggleButtonGroupWithButtonIconOnly.Add(new Button() { iconImage = EditorGUIUtility.FindTexture("d_PlayButton"), tooltip = "Play" });
csharpToggleButtonGroupWithButtonIconOnly.Add(new Button() { iconImage = EditorGUIUtility.FindTexture("d_PauseButton"), tooltip = "Pause" });
csharpToggleButtonGroupWithButtonIconOnly.Add(new Button() { iconImage = EditorGUIUtility.FindTexture("d_StepButton"), tooltip = "Step" });
container.Add(csharpToggleButtonGroupWithButtonIconOnly);
// Create custom buttons with IconImage and Text.
var csharpToggleButtonGroupWithButtonIconAndText = new ToggleButtonGroup("C# Toggle Button Group Buttons with Icons and Text");
csharpToggleButtonGroupWithButtonIconAndText.Add(new Button() { iconImage = EditorGUIUtility.FindTexture("d_PlayButton"), text = "Play", tooltip = "Play" });
csharpToggleButtonGroupWithButtonIconAndText.Add(new Button() { iconImage = EditorGUIUtility.FindTexture("d_PauseButton"), text = "Pause", tooltip = "Pause" });
csharpToggleButtonGroupWithButtonIconAndText.Add(new Button() { iconImage = EditorGUIUtility.FindTexture("d_StepButton"), text = "Step", tooltip = "Step" });
container.Add(csharpToggleButtonGroupWithButtonIconAndText);
// Create custom buttons with IconImage, Text and AllowEmptySelection.
var csharpToggleButtonGroupSingleSelectionAndAllowEmptySelection = new ToggleButtonGroup("C# Toggle Button Group Buttons with Allow Empty Selection") { allowEmptySelection = true };
csharpToggleButtonGroupSingleSelectionAndAllowEmptySelection.Add(new Button() { iconImage = EditorGUIUtility.FindTexture("d_PlayButton"), text = "Play", tooltip = "Play" });
csharpToggleButtonGroupSingleSelectionAndAllowEmptySelection.Add(new Button() { iconImage = EditorGUIUtility.FindTexture("d_PauseButton"), text = "Pause", tooltip = "Pause" });
csharpToggleButtonGroupSingleSelectionAndAllowEmptySelection.Add(new Button() { iconImage = EditorGUIUtility.FindTexture("d_StepButton"), text = "Step", tooltip = "Step" });
container.Add(csharpToggleButtonGroupSingleSelectionAndAllowEmptySelection);
// Create a ToggleButtonGroup that allows multiple active selections.
var csharpToggleButtonGroupAllowMultiSelect = new ToggleButtonGroup("C# Toggle Button Group with Multiple Selection Enabled") { isMultipleSelection = true };
csharpToggleButtonGroupAllowMultiSelect.Add(new Button() { text = "X", tooltip = "tooltip text for X" });
csharpToggleButtonGroupAllowMultiSelect.Add(new Button() { text = "Y", tooltip = "tooltip text for Y" });
csharpToggleButtonGroupAllowMultiSelect.Add(new Button() { text = "Z", tooltip = "tooltip text for Z" });
container.Add(csharpToggleButtonGroupAllowMultiSelect);
// Create a ToggleButtonGroup that allows multiple active selections and allow empty selection.
var csharpToggleButtonGroupAllowMultiSelectWithAllowEmptySelection = new ToggleButtonGroup("C# Toggle Button Group with Multiple Selection and Allow Empty Selection") { isMultipleSelection = true, allowEmptySelection = true };
csharpToggleButtonGroupAllowMultiSelectWithAllowEmptySelection.Add(new Button() { text = "X", tooltip = "tooltip text for X" });
csharpToggleButtonGroupAllowMultiSelectWithAllowEmptySelection.Add(new Button() { text = "Y", tooltip = "tooltip text for Y" });
csharpToggleButtonGroupAllowMultiSelectWithAllowEmptySelection.Add(new Button() { text = "Z", tooltip = "tooltip text for Z" });
container.Add(csharpToggleButtonGroupAllowMultiSelectWithAllowEmptySelection);
// Create ToggleButtonGroup with a custom class that sets the text's font style to Bold.
var csharpToggleButtonGroupWithCustomClass = new ToggleButtonGroup("C# Toggle Button Group with a Custom Class");
csharpToggleButtonGroupWithCustomClass.AddToClassList("my-custom-style");
csharpToggleButtonGroupWithCustomClass.Add(new Button() { text = "Button A", tooltip = "Bolded font Button A" });
csharpToggleButtonGroupWithCustomClass.Add(new Button() { text = "Button B", tooltip = "Bolded font Button B" });
container.Add(csharpToggleButtonGroupWithCustomClass);
/// </sample>
要在 Unity 中实时尝试此示例,请转到 窗口 > UI 工具包 > 示例。
C# 类:ToggleButtonGroup
命名空间:UnityEngine.UIElements
基类:BaseField_1
此元素具有以下成员属性
名称 | 类型 | 描述 |
---|---|---|
allow-empty-selection |
布尔值 |
设置为 true 时,允许所有按钮都取消选中。 当属性值为 false 时,控件会自动将第一个可用的按钮设置为选中状态。 |
is-multiple-selection |
布尔值 |
是否可以选择所有按钮。 |
此元素从其基类继承以下属性
名称 | 类型 | 描述 |
---|---|---|
binding-path |
字符串 |
要绑定的目标属性的路径。 |
focusable |
布尔值 |
如果元素可以聚焦,则为 True。 |
label |
字符串 |
表示字段旁边将显示的标签的字符串。 |
tabindex |
整数 |
用于在焦点环中对可聚焦元素进行排序的整数。必须大于或等于零。 |
value |
UIElements.ToggleButtonGroupState |
与字段关联的值。 |
此元素还从 VisualElement
继承以下属性
名称 | 类型 | 描述 |
---|---|---|
content-container |
字符串 |
子元素添加到其中,通常与元素本身相同。 |
data-source |
对象 |
为该 VisualElement 分配数据源,覆盖任何继承的数据源。此数据源由所有子元素继承。 |
data-source-path |
字符串 |
从数据源到值的路径。 |
data-source-type |
System.Type |
可以分配给此 VisualElement 的数据源的可能类型。 此信息仅由 UI 构建器用作提示,以便在设计时无法指定有效数据源时为数据源路径字段提供一些补全。 |
language-direction |
UIElements.LanguageDirection |
指示元素文本的方向性。该值将传播到元素的子元素。 将 languageDirection 设置为 RTL 通过反转文本并适当地处理换行和自动换行来添加对从右到左 (RTL) 的基本支持。但是,它不提供全面的 RTL 支持,因为这需要文本整形,包括字符的重新排序和 OpenType 字体功能支持。全面的 RTL 支持计划在未来的更新中提供,这将涉及其他 API 来处理语言、脚本和字体功能规范。 为了增强此属性的 RTL 功能,用户可以在 Unity Asset Store 中探索可用的第三方插件并使用 ITextElementExperimentalFeatures.renderedText |
name |
字符串 |
此 VisualElement 的名称。 使用此属性编写针对特定元素的 USS 选择器。标准做法是为元素指定唯一名称。 |
picking-mode |
UIElements.PickingMode |
确定在鼠标事件或 IPanel.Pick 查询期间是否可以选择此元素。 |
style |
字符串 |
设置 VisualElement 样式值。 |
tooltip |
字符串 |
在用户将鼠标悬停在元素上一小段时间后,在信息框内显示的文本。这仅在编辑器 UI 中受支持。 |
usage-hints |
UIElements.UsageHints |
指定 VisualElement 高级预期使用模式的提示值的组合。此属性只能在 VisualElement 尚未成为 Panel 的一部分时设置。一旦成为 Panel 的一部分,此属性实际上就成为只读的,尝试更改它将引发异常。正确 UsageHints 的规范驱动系统做出更好的决策,关于如何根据预期的使用模式处理或加速某些操作。请注意,这些提示不会影响行为或视觉结果,而只会影响面板及其内部元素的整体性能。建议始终考虑指定正确的 UsageHints ,但请记住,在某些情况下某些 UsageHints 可能会在内部被忽略(例如,由于目标平台上的硬件限制)。 |
view-data-key |
字符串 |
用于视图数据持久性,例如树展开状态、滚动位置或缩放级别。 此键用于从视图数据存储中保存和加载视图数据。如果您不设置此键,则关联的 VisualElement 将禁用持久性。有关更多信息,请参阅 视图数据持久性。 |
下表列出了所有 C# 公共属性名称及其相关的 USS 选择器。
C# 属性 | USS 选择器 | 描述 |
---|---|---|
ussClassName |
.unity-toggle-button-group |
此类型的元素的 USS 类名。 |
containerUssClassName |
.unity-toggle-button-group__container |
此类型的容器元素的 USS 类名。 |
ussClassName |
.unity-base-field |
此类型的元素的 USS 类名。 |
labelUssClassName |
.unity-base-field__label |
此类型的元素中标签的 USS 类名。 |
inputUssClassName |
.unity-base-field__input |
此类型的元素中输入元素的 USS 类名。 |
noLabelVariantUssClassName |
.unity-base-field--no-label |
此类型的元素的 USS 类名,当没有标签时。 |
labelDraggerVariantUssClassName |
.unity-base-field__label--with-dragger |
此类型的元素中标签的 USS 类名,当在其上附加了拖动器时。 |
mixedValueLabelUssClassName |
.unity-base-field__label--mixed-value |
显示混合值的元素的 USS 类名 |
alignedFieldUssClassName |
.unity-base-field__aligned |
在 Inspector 元素中对齐的元素的 USS 类名 |
disabledUssClassName |
.unity-disabled |
本地禁用元素的 USS 类名。 |
您还可以使用 Inspector 或 UI 工具包调试器中的匹配选择器部分 来查看哪些 USS 选择器影响其层次结构中每个级别的 VisualElement
的组件。
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.