Version: Unity 6.0 (6000.0)
语言 : 中文
SliderInt
TabView

Tab

Tab 元素表示 TabView 中的单个选项卡。在窗口或菜单中,可以使用选项卡对相关内容进行分组。

使 Tab 可关闭

要关闭 Tab,请将 closable 属性设置为 true。当 Tab 可关闭时,Tab 上会显示一个关闭图标。如果用户选择关闭图标,则 Tab 将关闭。

将图标添加到 Tab

您可以向 Tab 添加图标,使其更具视觉吸引力。图标可以是项目中存在的图像资源,例如纹理、渲染纹理、精灵或矢量。有关如何引用图像资源的信息,请参阅资源

执行以下操作之一可使用__ UI__(即用户界面,User Interface)让用户能够与您的应用程序进行交互。Unity 目前支持三种 UI 系统。更多信息
See in Glossary
Builder 将图标添加到 Tab:

  • 转到 Tab 的 Inspector 面板,从 Icon Image 下拉列表中选择图标。
  • 将图标从 Assets 窗口拖动到 Tab 的 Inspector 面板中的 Icon Image 字段。

要使用 UXML 为 Tab 添加图标,请将图像源添加到 icon-image 属性:

<ui:Tab name="Tab" text="Tab text" icon-image="/path/to/image-file.png" />

要使用 C# 为 Tab 添加图标,请将图像源分配给 iconImage 属性:

Tab myTab = new Tab();
var TabIconImage = Resources.Load<Texture2D>("image-file");

myTab.text = "Tab text";
myTab.iconImage = TabIconImage;

示例

以下 UXML 示例将使用 Tab 创建 TabView:

<UXML xmlns="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements">
    <TabView>
       <Tab label="UXML Tab A">
           <Label text="UXML tab: This is some content for the first Tab." />
       </Tab>
       <Tab label="UXML Tab B">
           <Label text="UXML tab: This is some content for the second Tab." />
       </Tab>
    </TabView>
</UXML>

以下 C# 示例说明了 TabView 及其 Tab 的一些可自定义功能:

/// <sample>
// Create a TabView with Tabs that only contains a label.
var csharpTabViewWithLabels = new TabView() { style = { marginTop = 15 } }; // marginTop not required, only for demonstration purposes.
var tabOne = new Tab("One");
tabOne.Add(new Label("Tab with labels only: This is some content for the first Tab.") { style = { marginTop = 10 } });
csharpTabViewWithLabels.Add(tabOne);
var tabTwo = new Tab("Two");
tabTwo.Add(new Label("Tab with labels only: This is some content for the second Tab.") { style = { marginTop = 10 } });
csharpTabViewWithLabels.Add(tabTwo);
container.Add(csharpTabViewWithLabels);

// Create a TabView with Tabs that only contains an icon.
var csharpTabViewWithIcons = new TabView() { style = { marginTop = 15 } }; // marginTop not required, only for demonstration purposes.
var tabIconConnect = new Tab(EditorGUIUtility.FindTexture("CloudConnect"));
tabIconConnect.Add(new Label("Tab with icons only: This is some content for the first Tab.") { style = { marginTop = 10 } });
csharpTabViewWithIcons.Add(tabIconConnect);
var tabIconStore = new Tab(EditorGUIUtility.FindTexture("Asset Store"));
tabIconStore.Add(new Label("Tab with icons only: This is some content for the second Tab.") { style = { marginTop = 10 } });
csharpTabViewWithIcons.Add(tabIconStore);
container.Add(csharpTabViewWithIcons);

// Create a TabView with Tabs that only contains an icon and a label.
var csharpTabViewWithIconsAndLabels = new TabView() { style = { marginTop = 15 } }; // marginTop not required, only for demonstration purposes.
var tabConnect = new Tab("Connect", EditorGUIUtility.FindTexture("CloudConnect"));
tabConnect.Add(new Label("Tab with an icon and a labels: This is some content for the first Tab.") { style = { marginTop = 10 } });
csharpTabViewWithIconsAndLabels.Add(tabConnect);
var tabStore = new Tab("Store", EditorGUIUtility.FindTexture("Asset Store"));
tabStore.Add(new Label("Tab with an icon and a labels: This is some content for the second Tab.") { style = { marginTop = 10 } });
csharpTabViewWithIconsAndLabels.Add(tabStore);
container.Add(csharpTabViewWithIconsAndLabels);

// Create a TabView that allows re-ordering of the tabs.
var csharpReorderableTabView = new TabView() { reorderable = true, style = { marginTop = 10 } }; // marginTop not required, only for demonstration purposes.
var tabA = new Tab("Tab A");
tabA.Add(new Label("Reorderable tabs: This is some content for Tab A") { style = { marginTop = 10 } });
csharpReorderableTabView.Add(tabA);
var tabB = new Tab("Tab B");
tabB.Add(new Label("Reorderable tabs: This is some content for Tab B") { style = { marginTop = 10 } });
csharpReorderableTabView.Add(tabB);
var tabC = new Tab("Tab C");
tabC.Add(new Label("Reorderable tabs: This is some content for Tab C") { style = { marginTop = 10 } });
csharpReorderableTabView.Add(tabC);
container.Add(csharpReorderableTabView);

// Create a TabView with closeable tabs.
var closeTabInfoLabel = new Label($"Last tab closed: None");
void UpdateLabel(string newLabel) => closeTabInfoLabel.text = $"Last tab closed: {newLabel}";
var cSharpCloseableTabs = new TabView() { style = { marginTop = 10 } }; // marginTop not required, only for demonstration purposes.
var closeableTabA = new Tab("Title A") { closeable = true };
closeableTabA.closed += (tab) => { UpdateLabel(tab.label); };
closeableTabA.Add(new Label("Closeable tabs: This is some content for Tab A") { style = { marginTop = 10 } });
cSharpCloseableTabs.Add(closeableTabA);
var closeableTabB = new Tab("Title B") { closeable = true };
closeableTabB.closed += (tab) => { UpdateLabel(tab.label); };
closeableTabB.Add(new Label("Closeable tabs: This is some content for Tab B") { style = { marginTop = 10 } });
cSharpCloseableTabs.Add(closeableTabB);
var closeableTabC = new Tab("Title C") { closeable = true };
closeableTabC.closed += (tab) => { UpdateLabel(tab.label); };
closeableTabC.Add(new Label("Closeable tabs: This is some content for Tab C") { style = { marginTop = 10 } });
cSharpCloseableTabs.Add(closeableTabC);
container.Add(cSharpCloseableTabs);
container.Add(closeTabInfoLabel);

// Create a TabView and apply custom styling to specific areas of their tabs.
var csharpCustomStyledTabView = new TabView() { style = { marginTop = 15 }, classList = { "some-styled-class" }}; // marginTop not required, only for demonstration purposes.
var customStyledTabOne = new Tab("One");
customStyledTabOne.Add(new Label("Custom styled tabs: This is some content for the first Tab."));
csharpCustomStyledTabView.Add(customStyledTabOne);
var customStyledTabTwo = new Tab("Two");
customStyledTabTwo.Add(new Label("Custom styled tabs: This is some content for the second Tab."));
csharpCustomStyledTabView.Add(customStyledTabTwo);
container.Add(csharpCustomStyledTabView);
/// </sample>

要在 Unity 中实时试用此示例,请转到窗口 (Window) > UI 工具包 (UI Toolkit) > 示例 (Samples)

有关更多示例,请参阅以下内容:

-创建选项卡式菜单

C# 基类和命名空间

C# 类Tab
命名空间UnityEngine.UIElements
基类VisualElement

成员 UXML 属性

此元素具有以下成员属性:

名称 类型 描述
closeable boolean 一个添加了关闭选项卡功能的属性。

默认值为 false。将此值设置为 true 可允许用户在选项卡视图中关闭选项卡。
icon-image Object 设置选项卡标题的图标。
label string 设置选项卡标题的标签。

继承 UXML 属性

此元素从其基类继承以下属性:

名称 类型 描述
focusable boolean 如果元素可以聚焦,则为 true。
tabindex int 用于对焦点环中可获得焦点的元素排序的整数。必须大于或等于零。

此元素还从 VisualElement 继承以下属性:

名称 类型 描述
content-container string 向其添加子元素,通常与元素本身相同。
data-source Object 为此 VisualElement 分配一个数据源,此数据源将覆盖任何已继承的数据源。此数据源由所有子项继承。
data-source-path string 从数据源到值的路径。
data-source-type System.Type 可分配给此 VisualElement 的可能数据源的类型。

仅当在设计时无法指定有效数据源时,UI Builder 才使用此信息作为补全数据源路径字段的提示。
language-direction UIElements.LanguageDirection 指示元素文本的方向性。该值将传播到元素的子项。

将 languageDirection 设置为 RTL,即可通过反转文本、处理换行和单词自动换行的方式,提供对从右到左 (RTL) 语言的基本支持。但是,它不提供全面的 RTL 支持,因为这需要文本整形(包括字符重新排序)和 OpenType 字体功能支持。未来更新计划提供全面的 RTL 支持,这将涉及额外的 API 来处理语言、脚本和字体功能规范。

要增强此属性的 RTL 功能,用户可以在 Unity 资源商店中探索可用的第三方插件,并使用 ITextElementExperimentalFeatures.renderedText
name string 此 VisualElement 的名称。

使用此属性可编写针对特定元素的 USS 选择器。标准做法是为元素指定唯一名称。
picking-mode UIElements.PickingMode 确定是否可以在 mouseEvents 或 IPanel.Pick 查询期间选择此元素。
style string 设置 VisualElement 样式值。
tooltip string 用户将光标悬停在元素上一小段时间后信息框内显示的文本。仅在编辑器 UI 中受支持。
usage-hints UIElements.UsageHints 用于指定 VisualElement 高级预期使用模式的提示值组合。仅当 VisualElement 尚未成为 Panel 的一部分时,才能设置此属性。一旦成为 Panel 的一部分,此属性实际上就变为只读,尝试更改此属性将显示异常。适当的 UsageHints 规范会促使系统根据预期的使用模式,做出更好的决策,以处理或加速某些操作。请注意,这些提示不会影响行为或视觉效果,只会影响面板及其元素的整体性能。建议始终考虑指定适当的 UsageHints,但请记住,在某些情况下(例如,由于目标平台上的硬件限制),某些 UsageHints 可能会在内部被忽略。
view-data-key string 用于 View Data 持久性,例如树展开状态、滚动位置或缩放级别。

此键用于保存和加载 View Data 存储中的视图数据。如果未设置此键,则会对关联的 VisualElement 禁用持久性。有关更多信息,请参阅 View Data 持久性

USS 类

下表列出了所有 C# 公共属性名称及其相关的 USS 选择器。

C# 属性 USS 选择器 描述
ussClassName .unity-tab 此类型元素的 USS 类名称。
tabHeaderUssClassName .unity-tab__header 此类型标题的 USS 类名称。
tabHeaderImageUssClassName .unity-tab__header-image 标题内图标的 USS 类名称。
tabHeaderEmptyImageUssClassName .unity-tab__header-image--empty 当值为 null 时,标题内图标的 USS 类名称。
tabHeaderStandaloneImageUssClassName .unity-tab__header-image--standalone 当标签为空或为 null 时,标题内图标的 USS 类名称。
tabHeaderLabelUssClassName .unity-tab__header-label 标题标签的 USS 类名称。
tabHeaderEmptyLabeUssClassName .unity-tab__header-label--empty 当值为空或为 null 时,标题标签的 USS 类名称。
tabHeaderUnderlineUssClassName .unity-tab__header-underline 标题活动状态下划线的 USS 类名称。
contentUssClassName .unity-tab__content-container 此类型容器元素的 USS 类名称。
draggingUssClassName .unity-tab--dragging 此类型拖动状态的 USS 类名称。
reorderableUssClassName .unity-tab__reorderable 可重新排序选项卡元素的 USS 类名称。
reorderableItemHandleUssClassName .unity-tab__reorderable-handle 可重新排序选项卡中拖动句柄的 USS 类名称。
reorderableItemHandleBarUssClassName .unity-tab__reorderable-handle-bar 可重新排序选项卡中拖动句柄条的 USS 类名称。
closeableUssClassName .unity-tab__header__closeable 可关闭选项卡的 USS 类名称。
closeButtonUssClassName .unity-tab__close-button 可关闭选项卡中关闭按钮的 USS 类名称。
disabledUssClassName .unity-disabled 本地被禁用元素的 USS 类名称。

还可以使用检视器中的匹配选择器 (Matching Selectors) 部分或 UI 工具包调试器来查看哪些 USS 选择器在其层级视图的每个级别上影响 VisualElement 的组件。

其他资源

SliderInt
TabView