#unity/日常积累 # TreeView class in UnityEditor.IMGUI.Controls ## 描述 TreeView 是一种 IMGUI 控件,可用于为 Editor 工具创建树视图、列表视图和多列表格。 对于项的行内容渲染、拖动逻辑、选择逻辑、搜索、排序和重命名,均可进行自定义。为确保 TreeView 之间的一致性,以下功能不可自定义:折叠箭头渲染、选择渲染、拖动标记渲染。 [BuildRoot](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView.BuildRoot.html) 是一个很好的起点。 ## 变量 [baseIndent](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView-baseIndent.html) 用于所有行中树折叠箭头和内容之前的缩进。 [cellMargin](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView-cellMargin.html) 使用 MultiColumnHeader 时,此值可调整为所有列(除了树折叠列)提供的单元格矩形。 [columnIndexForTreeFoldouts](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView-columnIndexForTreeFoldouts.html) 使用 MultiColumnHeader 时,此值应设置为显示折叠箭头的列的索引。 [customFoldoutYOffset](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView-customFoldoutYOffset.html) 折叠箭头的自定义垂直偏移量。 [depthIndentWidth](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView-depthIndentWidth.html) 该值返回折叠应针对每个递增深度值缩进的距离。 [extraSpaceBeforeIconAndLabel](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView-extraSpaceBeforeIconAndLabel.html) 用于控制默认图标和标签前的间距的值。例如,可用于将切换按钮置于内容左侧。 [foldoutOverride](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView-foldoutOverride.html) 注册此属性的回调以重载 TreeView 中的 Foldout 按钮。 [foldoutWidth](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView-foldoutWidth.html) 内置折叠箭头的宽度。 [getNewSelectionOverride](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView-getNewSelectionOverride.html) 注册此字段的回调以重载 TreeView 如何处理选择更改以响应按键和鼠标点击。 [hasSearch](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView-hasSearch.html) TreeView 的当前搜索状态。 [isDragging](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView-isDragging.html) 如果用户当前正在拖动 TreeView 中的一个或多个项,则为 true;否则为 false。 [isInitialized](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView-isInitialized.html) 通过调用 Reload() 对 TreeView 进行初始化。因此,在首次调用 Reload() 之前,均返回 false。 [multiColumnHeader](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView-multiColumnHeader.html) 获取 TreeView 的 MultiColumnHeader。如果创建 TreeView 时未使用 MultiColumnHeader,则为 null。 [rootItem](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView-rootItem.html) TreeView 的隐藏根项(永不渲染)。 [rowHeight](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView-rowHeight.html) 用于 TreeView 中每个行的固定高度(如果尚未重载 GetCustomRowHeight)。 [searchString](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView-searchString.html) TreeView 的当前搜索字符串。 [showAlternatingRowBackgrounds](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView-showAlternatingRowBackgrounds.html) 启用此选项可显示交替的行背景颜色。 [showBorder](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView-showBorder.html) 启用此选项可显示 TreeView 的边框。 [showingHorizontalScrollBar](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView-showingHorizontalScrollBar.html) 如果显示水平滚动条,则返回 true;否则返回 false。 [showingVerticalScrollBar](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView-showingVerticalScrollBar.html) 如果显示垂直滚动条,则返回 true;否则返回 false。 [state](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView-state.html) TreeView 的状态(展开状态、选择、滚动等) [totalHeight](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView-totalHeight.html) 返回 TreeView 行高、MultiColumnHeader 高度(如果已使用)和边框(如果已使用)的总和。 [treeViewControlID](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView-treeViewControlID.html) TreeView 用于获取键盘控件焦点的 controlID。 [treeViewRect](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView-treeViewRect.html) 正在将 TreeView 渲染到的矩形。 [useScrollView](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView-useScrollView.html) 在绘制 TreeView 内容时,它是否会包含在 ScrollView 中? ## 构造函数 [TreeView](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView-ctor.html) 构建时 TreeView 时始终需要一个状态对象,并且可以选择一个多列标题对象(如果需要标题)。 ## 公共函数 [BeginRename](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView.BeginRename.html) 显示 TreeViewItem 的重命名覆盖。 [CollapseAll](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView.CollapseAll.html) 折叠 TreeView 中的所有展开项。 [EndRename](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView.EndRename.html) 如果已显示重命名覆盖,则结束重命名。如果在未显示重命名覆盖的情况下进行调用,此方法将不会执行任何操作。 [ExpandAll](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView.ExpandAll.html) 展开 TreeView 中的所有折叠项。 [FrameItem](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView.FrameItem.html) 此方法将显示 ID 为 id 的项(通过展开此项的上级)并确保其在 ScrollView 中可见。 [GetExpanded](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView.GetExpanded.html) 返回当前在 TreeView 中展开的 TreeViewItem ID 的列表。 [GetRows](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView.GetRows.html) 这是已内置于 BuildRows 中的 TreeViewItem 的列表。 [GetSelection](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView.GetSelection.html) 返回当前选中的 TreeViewItem ID 的列表。 [HasFocus](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView.HasFocus.html) 如果 TreeView 及其 EditorWindow 具有键盘焦点,则返回 true。 [HasSelection](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView.HasSelection.html) 如果 TreeView 有选择,则返回 true。 [IsExpanded](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView.IsExpanded.html) 如果 ID 为 id 的 TreeViewItem 当前已展开,则返回 true。 [IsSelected](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView.IsSelected.html) 如果 ID 为 id 的 TreeViewItem 当前已选中,则返回 true。 [OnGUI](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView.OnGUI.html) 这是 TreeView 的主 GUI 方法,将会处理和绘制 TreeViewItem。 [Reload](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView.Reload.html) 调用此方法可强制 TreeView 重新加载其数据。这进而又会导致调用 BuildRoot 和 BuildRow。 [Repaint](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView.Repaint.html) 请求重绘渲染 TreeView 的窗口。 [SelectAllRows](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView.SelectAllRows.html) 选择 TreeView 中的所有行。 [SetExpanded](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView.SetExpanded.html) 设置单个要展开或折叠的 TreeViewItem。 [SetExpandedRecursive](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView.SetExpandedRecursive.html) 展开或折叠具有 id 的项下的所有项。 [SetFocus](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView.SetFocus.html) 调用此函数可将键盘焦点更改到 TreeView。 [SetFocusAndEnsureSelectedItem](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView.SetFocusAndEnsureSelectedItem.html) 调用此函数可将键盘焦点更改到 TreeView 并确保已选择一个项。使用此函数可启用 TreeView 的键导航。 [SetSelection](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView.SetSelection.html) 设置 TreeView 的选中项。 ## 受保护的函数 [AddExpandedRows](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView.AddExpandedRows.html) 将完整树的展开行添加到输入列表。只有当完整树内置于 BuildRoot 时才使用此方法。 [AfterRowsGUI](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView.AfterRowsGUI.html) 在所有行均已调用其 RowGUI 后调用此方法。 [BeforeRowsGUI](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView.BeforeRowsGUI.html) 在任意行调用其 RowGUI 之前调用此方法。 [BuildRoot](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView.BuildRoot.html) 需要实现的抽象方法。默认情况下,此方法应创建 TreeViewItem 的完整树并返回根。 [BuildRows](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView.BuildRows.html) 重载此方法可控制行的生成方式。 [CanBeParent](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView.CanBeParent.html) 重载此方法可控制允许哪些项成为父项。 [CanChangeExpandedState](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView.CanChangeExpandedState.html) 重载此方法可控制某个项能否通过键或鼠标来展开或折叠。 [CanMultiSelect](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView.CanMultiSelect.html) 重载此方法可控制项能否成为多重选择的一部分。 [CanRename](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView.CanRename.html) 重载此方法可控制项能否通过键盘快捷键或单击已选中的项来进行重命名。 [CanStartDrag](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView.CanStartDrag.html) 每当单击和拖动 TreeViewItem 时,将调用此函数。默认情况,返回 false。 [CenterRectUsingSingleLineHeight](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView.CenterRectUsingSingleLineHeight.html) 修改输入矩形,以便它居中并拥有等于 EditorGUIUtility.singleLineHeight 的高度。 [CommandEventHandling](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView.CommandEventHandling.html) 系统会自动调用此函数来处理“SelectAll”和“FrameSelection” 的 ExecuteCommand 事件。重载此函数可扩展或避免 Command 事件。 [ContextClicked](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView.ContextClicked.html) 重载此方法可处理任何项外部(但仍在 TreeView 矩形内)的上下文单击事件。 [ContextClickedItem](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView.ContextClickedItem.html) 重载此函数可处理针对项(ID 为 TreeViewItem.id)的上下文单击事件。 [DoesItemMatchSearch](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView.DoesItemMatchSearch.html) 重载此函数可扩展或更改搜索行为。 [DoubleClickedItem](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView.DoubleClickedItem.html) 重载此方法可处理针对项的双击事件。 [ExpandedStateChanged](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView.ExpandedStateChanged.html) 重载以在项展开或折叠时收到通知。这是一般通知,告知展开状态已更改。 [FindItem](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView.FindItem.html) 根据 ID 查找 TreeViewItem。 [FindRowOfItem](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView.FindRowOfItem.html) Returns the row of the given TreeViewItem. [FindRows](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView.FindRows.html) 可用于使用当前行从 TreeViewItem ID 转换为 TreeViewItem。 [GetAncestors](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView.GetAncestors.html) 举例而言,此方法可用于显示当前在某个折叠项下的各个项。 [GetCellRectForTreeFoldouts](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView.GetCellRectForTreeFoldouts.html) 适用于多列设置的 Utility。此方法将根据 columnIndexForTreeFoldouts 定义的列矩形剪辑输入 rowRect,以获得显示折叠箭头的单元格矩形。 [GetContentIndent](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView.GetContentIndent.html) 返回项的水平内容偏移量。这是内容应开始的位置(在折叠箭头之后)。 [GetCustomRowHeight](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView.GetCustomRowHeight.html) 重载以控制各个行高。 [GetDescendantsThatHaveChildren](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView.GetDescendantsThatHaveChildren.html) 返回 ID 为 id 的项的具有子项的所有后代。 [GetFirstAndLastVisibleRows](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView.GetFirstAndLastVisibleRows.html) 返回 TreeView 的滚动视图中显示的第一个和最后一个行索引。 [GetFoldoutIndent](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView.GetFoldoutIndent.html) 返回项的水平折叠偏移量。这是渲染折叠箭头的位置。 [GetRenameRect](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView.GetRenameRect.html) 如果 RowGUI 中使用了自定义 GUI 处理,则重载此方法。此方法适用于显示重命名覆盖的控件。 [GetRowRect](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView.GetRowRect.html) 获取行矩形。 [HandleDragAndDrop](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView.HandleDragAndDrop.html) 重载此函数可控制 TreeView 的拖放行为。 [KeyEvent](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView.KeyEvent.html) 重载此方法可在 TreeView 具有键盘焦点时处理事件。 [RefreshCustomRowHeights](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView.RefreshCustomRowHeights.html) 根据 GetCustomRowHeight 返回的高度,刷新自定义行矩形的缓存。 [RenameEnded](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView.RenameEnded.html) 当重命名结束时调用,结束形式包括用户完成了重命名过程、重命名覆盖失去焦点或已使用 EndRename 关闭。 [RowGUI](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView.RowGUI.html) 重载此方法可为 TreeView 中的行添加自定义 GUI 内容。 [SearchChanged](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView.SearchChanged.html) 重载此方法可获得关于搜索字符串更改的通知。 [SelectionChanged](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView.SelectionChanged.html) 重载此方法可获得关于选择更改的通知。 [SelectionClick](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView.SelectionClick.html) 在 RowGUI 中使用此方法可执行鼠标单击逻辑。 [SetupDragAndDrop](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView.SetupDragAndDrop.html) 当 CanStartDrag 返回 true 时调用此函数。 [SingleClickedItem](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView.SingleClickedItem.html) 重载此方法可处理针对某个项的单击事件。 [SortItemIDsInRowOrder](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView.SortItemIDsInRowOrder.html) 返回按在 TreeView 中显示的顺序存储的列表。 ## 静态函数 [CreateChildListForCollapsedParent](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView.CreateChildListForCollapsedParent.html) 创建虚拟 TreeViewItem 列表。适用于重载 BuildRows 以防止构建完整项树的情况。 [IsChildListForACollapsedParent](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView.IsChildListForACollapsedParent.html) 用于检查 childList 是否与 CreateChildListForCollapsedParent 方法返回的列表完全相同的 Utility 方法。 [SetupDepthsFromParentsAndChildren](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView.SetupDepthsFromParentsAndChildren.html) 使用输入 TreeViewItem 的深度为其所有后代 TreeViewItem 设置正确深度的 Utility 方法。 [SetupParentsAndChildrenFromDepths](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView.SetupParentsAndChildrenFromDepths.html) 使用已设置的顺序和深度值来初始化行的所有父属性和子属性的 Utility 方法。 ## 委托 [DoFoldoutCallback](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView.DoFoldoutCallback.html) 用于重载 TreeView 折叠箭头的回调签名。请参阅 foldoutOverride。 [GetNewSelectionFunction](https://docs.unity.cn/cn/2020.3/ScriptReference/IMGUI.Controls.TreeView.GetNewSelectionFunction.html) 这是一个回调,决定了 TreeView 如何处理选择更改以响应按键和鼠标点击。