Version: 6000.3
语言: 中文
滑块内部
选项卡视图

标签

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

使选项卡可关闭

要使其可以关闭选项卡,请将closable属性设置为true.当选项卡可关闭时,选项卡上会出现一个关闭图标。如果用户选择关闭图标,则选项卡将关闭。

向选项卡添加图标

您可以向选项卡添加图标,使其更具视觉吸引力。图标可以是项目中存在的图像资源,例如纹理、渲染纹理(Render Texture一种特殊类型的纹理,在运行时创建和更新。要使用它们,请先创建一个新的渲染纹理,并指定要渲染到其中的摄像机之一。然后,你可以在材质中使用渲染纹理,就像使用常规纹理一样。更多信息
请参阅术语表
,精灵A 2D 图形对象。如果你习惯于在3D中工作,精灵本质上只是标准纹理,但有一些特殊的技术可以组合和管理精灵纹理,以提高开发过程中的效率和便利性。更多信息
请参阅术语表
或矢量。有关如何引用图像资产的信息,请参阅资产可在游戏或项目中使用的任何媒体或数据。资产可能来自在 Unity 外部创建的文件,例如 3D 模型、音频文件或图像。您还可以在 Unity 中创建一些资产类型,例如动画师控制器、混音器或渲染纹理。更多信息
请参阅术语表
.

执行下列作之一,使用 UI Builder 将图标添加到选项卡:

  • 从选项卡的检查器一个 Unity 窗口,显示有关当前选定游戏对象、资产或项目设置的信息,允许您检查和编辑值。更多信息
    请参阅术语表
    面板中,从图标图像下拉列表中选择一个图标。
  • 将图标从“资源”窗口拖动到选项卡的“检查器”面板中的“图标图像”字段。

要为带有 UXL 的选项卡添加图标,请将图像的源添加到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示例创建了带有选项卡的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 及其选项卡的一些可自定义功能:

/// <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 工具包>示例

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

-创建选项卡式菜单

C# 基类和命名空间

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

成员 UXML 属性

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

名字 类型 描述
closeable boolean 添加关闭选项卡功能的属性。
默认值
false.将此值设置为true以允许用户关闭选项卡视图中的选项卡。
icon-image Object 设置选项卡标题的图标。
label string 设置选项卡标题的标签。

继承的UXML属性

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

名字 类型 描述
focusable boolean 如果为 false,则不会聚焦元素。

只有当元素的 canGrabFocus 属性为 true 时,才能聚焦该元素。
tabindex int 用于对焦点环中的可聚焦元素进行排序的整数。必须大于或等于零。

设置
tabIndex值小于0(例如,−1) 从焦点环和选项卡导航中删除元素。

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

名字 类型 描述
content-container string 添加子元素的逻辑容器。如果将子项添加到此元素,则该子项将改为添加到此元素的内容容器中。

迭代
VisualElement.Children元素的,则使用元素的内容容器层次结构而不是元素本身。这可能会导致意外结果,例如,如果元素不直接位于内容容器的层次结构中,则导航事件会忽略元素。指IFocusRing了解更多信息。

如果内容容器与元素本身相同,则子元素将直接添加到元素中。对于大多数元素来说都是如此,但可以被更复杂的类型覆盖。

ScrollView例如,具有与自身不同的内容容器。在这种情况下,添加到滚动视图的子元素将改为添加到其内容容器元素中。虽然物理父级 (VisualElement.Hierarchy.parent) 是滚动视图的内容容器元素,它们的逻辑父元素 (VisualElement.parent) 仍然引用滚动视图本身。由于滚动视图的一些可聚焦子项不是其逻辑层次结构的一部分,例如Scroller元素,在使用顺序导航时,默认情况下不考虑这些可聚焦的子项。如果默认导航规则与您的需求不符,请参阅如何更改接下来关注的元素,了解解决方法解决方案的示例。
data-source Object 将数据源分配给此 VisualElement,该 VisualElement 将替代任何继承的数据源。此数据源由所有子级继承。
data-source-path string 从数据源到值的路径。
data-source-type System.Type 可分配给此 VisualElement 的可能数据源类型。
当设计时无法指定有效数据源时,UI 生成器仅将
此信息用作提示,以对数据源路径字段进行一些补全。
enabled boolean 如果VisualElement在本地启用。
如果 VisualElement 的父级之一隐式禁用,则
不会更改此标志。要验证这一点,请使用
enabledInHierarchy.
language-direction UIElements.LanguageDirection 指示元素文本的方向性。该值将传播到元素的子项。

设置
languageDirectionRTL只能获得文本反转等基本的 RTL 支持。要获得更全面的 RTL 支持,例如换行、自动换行或文本整形,您必须启用高级文本生成器
name string 此 VisualElement 的名称。

使用此属性可以编写面向特定元素的 USS 选择器。标准做法是为元素指定一个唯一的名称。
picking-mode UIElements.PickingMode 确定此元素是可以作为指针事件的目标还是由IPanel.Pick查询。

如果出现以下情况,则无法拾取元素:

- 它们不可见- 它们的
style.display设置为DisplayStyle.None

拾取模式为PickingMode.Ignore永远不要接收悬停伪状态。
style string VisualElement.
返回
的样式数据是根据 USS 文件或在 C# 中写入此对象的内联样式计算得出的,不表示完全解析的样式,例如 VisualElement 的最终高度和宽度。要访问这些完全解析的样式,请使用
resolvedStyle.



有关如何使用此属性和所有受支持的 USS 属性的信息,请参阅在 C# 脚本中应用样式USS 属性参考手册页。
tooltip string 用户将鼠标悬停在元素上一小段时间后,要在信息框内显示的文本。这仅在编辑器UI中受支持。
usage-hints UIElements.UsageHints 提示值的组合,用于指定VisualElement.只有当VisualElement还不是Panel.曾经是Panel,则此属性实际上变为只读,尝试更改它将引发异常。适当的规范UsageHints驱动系统根据预期的使用模式就如何处理或加速某些作做出更好的决策。请注意,这些提示不会影响行为或视觉结果,而只会影响面板和其中元素的整体性能。建议始终考虑指定适当的UsageHints,但请记住,某些UsageHints在某些情况下(例如,由于目标平台上的硬件限制),可能会在内部忽略。
view-data-key string 用于视图数据持久性,例如树展开状态、滚动位置或缩放级别。

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

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 当标签为空或空时,标题内图标的 USS 类名称。
tabHeaderLabelUssClassName .unity-tab__header-label 标头标签的 USS 级名称。
tabHeaderEmptyLabeUssClassName .unity-tab__header-label--empty 当值为空或空时,标头标签的 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 级名称。

您还可以使用 Inspector 或 UI Toolkit Debugger 中的 Matching Selectors 部分来查看哪些 USS 选择器会影响VisualElement在其层次结构的各个层面。

其他资源

滑块内部
选项卡视图