Version: 2020.3

TreeView.useScrollView

切换到手册
protected bool useScrollView ;

描述

在绘制 TreeView 内容时,它是否会包含在 ScrollView 中?

启用后,将剔除 TreeView 的内容,因此不会绘制 ScrollView 之外的任何内容。如果 TreeView 包含在外部 ScrollView(例如 Inspector 窗口)中,则禁用此项会允许 TreeView 使用外部 ScrollView 执行其剔除。

using UnityEngine;

public class ExampleBehaviourScript : MonoBehaviour { // This is our example list. public string[] exampleList; }

要使用以下脚本,请将其添加到 Editor 目录。

using System.Collections.Generic;
using UnityEditor;
using UnityEditor.IMGUI.Controls;
using UnityEngine;

// Simple TreeView that draws a single list property. class NewBehaviourScriptEditorTreeView : TreeView { SerializedProperty m_Property;

public NewBehaviourScriptEditorTreeView(SerializedProperty property) : base(new TreeViewState()) { m_Property = property; showBorder = true; showAlternatingRowBackgrounds = true; useScrollView = false; // We are using the Inspector ScrollView

MultiColumnHeaderState.Column[] columns = new MultiColumnHeaderState.Column[2]; for (int i = 0; i < columns.Length; ++i) { columns[i] = new MultiColumnHeaderState.Column(); columns[i].minWidth = 50; columns[i].width = 100; columns[i].headerTextAlignment = TextAlignment.Center; columns[i].canSort = false; } columns[0].headerContent = new GUIContent("Index"); columns[1].headerContent = new GUIContent("Value"); var multiColState = new MultiColumnHeaderState(columns); multiColumnHeader = new MultiColumnHeader(multiColState); multiColumnHeader.ResizeToFit(); Reload(); }

protected override TreeViewItem BuildRoot() { int arraySize = m_Property.arraySize;

var root = new TreeViewItem { id = -1, depth = -1, displayName = "Root" }; var allItems = new List<TreeViewItem>(arraySize); for (int i = 0; i < arraySize; ++i) { var item = new TreeViewItem(i, 0, i.ToString()); allItems.Add(item); }

SetupParentsAndChildrenFromDepths(root, allItems); return root; }

protected override void RowGUI(RowGUIArgs args) { var prop = m_Property.GetArrayElementAtIndex(args.item.id); for (int i = 0; i < args.GetNumVisibleColumns(); ++i) { int col = args.GetColumn(i); var rect = args.GetCellRect(i);

if (col == 0) { GUI.Label(rect, args.row.ToString()); } else { EditorGUI.PropertyField(rect, prop, GUIContent.none); } } } }

// Shows how we can use a TreeView to draw a large list of items. [CustomEditor(typeof(ExampleBehaviourScript))] [CanEditMultipleObjects] public class NewBehaviourScriptEditor : Editor { NewBehaviourScriptEditorTreeView m_TreeView; SerializedProperty m_Size;

void OnEnable() { var listProperty = serializedObject.FindProperty("exampleList"); m_Size = serializedObject.FindProperty("exampleList.Array.size"); m_TreeView = new NewBehaviourScriptEditorTreeView(listProperty); Undo.undoRedoPerformed += m_TreeView.Reload; }

void OnDisable() { if (m_TreeView != null) Undo.undoRedoPerformed -= m_TreeView.Reload; }

public override void OnInspectorGUI() { serializedObject.Update(); EditorGUI.BeginChangeCheck(); int newSize = EditorGUILayout.IntField("Size", m_Size.intValue); if (EditorGUI.EndChangeCheck()) { m_Size.intValue = newSize; m_TreeView.Reload(); }

var rect = EditorGUILayout.GetControlRect(false, m_TreeView.totalHeight); m_TreeView.OnGUI(rect); serializedObject.ApplyModifiedProperties(); } }