包含此页的版本:
不含此页的版本:
可以将 TabView 元素中的多个 Tab 元素分组,以创建基于选项卡的导航系统。
您可以使用 UI Builder、UXML 或 C# 创建 TabView。
若要使用 C# 创建 TabView,请创建 TabView 对象的新实例,然后向其添加 Tab 元素。例如:
var tabView = new TabView("Title text");
var tab1 = new Tab("Tab 1");
var tab2 = new Tab("Tab 2");
var tab3 = new Tab("Tab 3");
tabView.Add(tab1);
tabView.Add(tab2);
tabView.Add(tab3);
若要使选项卡可使用 TabView 重新排序,请将reorderable属性设置为true.
要在编辑器UI中保留TabView的Tab顺序,请分配一个唯一的view-data-key添加到 TabView 及其包含的 Tab 元素。这view-data-key存储选项卡的状态。如果您离开了view-data-key空,则重新加载文档时选项卡状态不会保留。更多信息,请参见查看数据持久性。
以下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# 类:TabView
命名空间:UnityEngine.UIElements
基类:VisualElement
此元素具有以下成员属性:
| 名字 | 类型 | 描述 |
|---|---|---|
reorderable |
boolean |
向选项卡添加拖动支持的属性。 默认值 为 false.将此值设置为true以允许用户在选项卡视图中重新排序选项卡。 |
此元素从其基类继承以下属性:
| 名字 | 类型 | 描述 |
|---|---|---|
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 |
指示元素文本的方向性。该值将传播到元素的子项。 设置 languageDirection自RTL只能获得文本反转等基本的 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 手册中的查看数据持久性。 |
下表列出了所有 C# 公共属性名称及其相关的 USS 选择器。
| C# 属性 | USS 选择器 | 描述 |
|---|---|---|
ussClassName |
.unity-tab-view |
此类元素的 USS 级名称。 |
contentContainerUssClassName |
.unity-tab-view__content-container |
此类型内容容器的 USS 类名称。 |
reorderableUssClassName |
.unity-tab-view__reorderable |
可重新排序的选项卡视图的 USS 类名称。 |
verticalUssClassName |
.unity-tab-view__vertical |
垂直选项卡视图的 USS 类名称。 |
viewportUssClassName |
.unity-tab-view__content-viewport |
USS 类名称,用于此类型的内容视口。 |
nextButtonUssClassName |
.unity-tab-view__next-button |
USS 级名称,用于此类型的“滚动下一步”按钮。 |
previousButtonUssClassName |
.unity-tab-view__previous-button |
USS 级名称,用于此类型的滚动上一个按钮。 |
disabledUssClassName |
.unity-disabled |
本地禁用元素的 USS 级名称。 |
您还可以使用 Inspector 或 UI Toolkit Debugger 中的 Matching Selectors 部分来查看哪些 USS 选择器会影响VisualElement在其层次结构的各个层面。