lyh916

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
  201 随笔 :: 0 文章 :: 12 评论 :: 21万 阅读

参考链接:

https://docs.unity3d.com/Manual/TreeViewAPI.html

 

效果如下:

 

SimpleTreeView.cs

在添加TreeViewItem节点时,要注意id要唯一,根节点的depth必须为-1

复制代码
 1 using System.Collections.Generic;
 2 using UnityEditor.IMGUI.Controls;
 3 
 4 class SimpleTreeView : TreeView
 5 {
 6     public SimpleTreeView(TreeViewState treeViewState)
 7         : base(treeViewState)
 8     {
 9         Reload();
10     }
11 
12     protected override TreeViewItem BuildRoot()
13     {
14         // BuildRoot is called every time Reload is called to ensure that TreeViewItems 
15         // are created from data. Here we create a fixed set of items. In a real world example,
16         // a data model should be passed into the TreeView and the items created from the model.
17 
18         // This section illustrates that IDs should be unique. The root item is required to 
19         // have a depth of -1, and the rest of the items increment from that.
20         var root = new TreeViewItem { id = 0, depth = -1, displayName = "Root" };
21         var allItems = new List<TreeViewItem>
22         {
23             new TreeViewItem {id = 1, depth = 0, displayName = "Animals"},
24             new TreeViewItem {id = 2, depth = 1, displayName = "Mammals"},
25             new TreeViewItem {id = 3, depth = 2, displayName = "Tiger"},
26             new TreeViewItem {id = 4, depth = 2, displayName = "Elephant"},
27             new TreeViewItem {id = 5, depth = 2, displayName = "Okapi"},
28             new TreeViewItem {id = 6, depth = 2, displayName = "Armadillo"},
29             new TreeViewItem {id = 7, depth = 1, displayName = "Reptiles"},
30             new TreeViewItem {id = 8, depth = 2, displayName = "Crocodile"},
31             new TreeViewItem {id = 9, depth = 2, displayName = "Lizard"},
32         };
33 
34         // Utility method that initializes the TreeViewItem.children and .parent for all items.
35         SetupParentsAndChildrenFromDepths(root, allItems);
36 
37         // Return root of the tree
38         return root;
39     }
40 }
复制代码

 

SimpleTreeViewWindow.cs

复制代码
 1 using UnityEditor;
 2 using UnityEditor.IMGUI.Controls;
 3 using UnityEngine;
 4 
 5 class SimpleTreeViewWindow : EditorWindow
 6 {
 7     // SerializeField is used to ensure the view state is written to the window 
 8     // layout file. This means that the state survives restarting Unity as long as the window
 9     // is not closed. If the attribute is omitted then the state is still serialized/deserialized.
10     [SerializeField] TreeViewState m_TreeViewState;
11 
12     //The TreeView is not serializable, so it should be reconstructed from the tree data.
13     SimpleTreeView m_SimpleTreeView;
14 
15     void OnEnable()
16     {
17         // Check whether there is already a serialized view state (state 
18         // that survived assembly reloading)
19         if (m_TreeViewState == null)
20             m_TreeViewState = new TreeViewState();
21 
22         m_SimpleTreeView = new SimpleTreeView(m_TreeViewState);
23     }
24 
25     void OnGUI()
26     {
27         m_SimpleTreeView.OnGUI(new Rect(0, 0, position.width, position.height));
28     }
29 
30     // Add menu named "My Window" to the Window menu
31     [MenuItem("TreeView Examples/Simple Tree Window")]
32     static void ShowWindow()
33     {
34         // Get existing open window or if none, make a new one:
35         var window = GetWindow<SimpleTreeViewWindow>();
36         window.titleContent = new GUIContent("My Window");
37         window.Show();
38     }
39 }
复制代码

 

添加节点的另一种方法(其优点是可以先不指定depth):

复制代码
 1 using System.Collections.Generic;
 2 using UnityEditor.IMGUI.Controls;
 3 
 4 class SimpleTreeView : TreeView
 5 {
 6     public SimpleTreeView(TreeViewState treeViewState)
 7         : base(treeViewState)
 8     {
 9         Reload();
10     }
11 
12     protected override TreeViewItem BuildRoot()
13     {
14         var root = new TreeViewItem { id = 0, depth = -1, displayName = "Root" };
15         var animals = new TreeViewItem { id = 1, displayName = "Animals" };
16         var mammals = new TreeViewItem { id = 2, displayName = "Mammals" };
17         var tiger = new TreeViewItem { id = 3, displayName = "Tiger" };
18         var elephant = new TreeViewItem { id = 4, displayName = "Elephant" };
19         var okapi = new TreeViewItem { id = 5, displayName = "Okapi" };
20         var armadillo = new TreeViewItem { id = 6, displayName = "Armadillo" };
21         var reptiles = new TreeViewItem { id = 7, displayName = "Reptiles" };
22         var croco = new TreeViewItem { id = 8, displayName = "Crocodile" };
23         var lizard = new TreeViewItem { id = 9, displayName = "Lizard" };
24 
25         root.AddChild(animals);
26         animals.AddChild(mammals);
27         animals.AddChild(reptiles);
28         mammals.AddChild(tiger);
29         mammals.AddChild(elephant);
30         mammals.AddChild(okapi);
31         mammals.AddChild(armadillo);
32         reptiles.AddChild(croco);
33         reptiles.AddChild(lizard);
34 
35         SetupDepthsFromParentsAndChildren(root);
36 
37         return root;
38     }
39 }
复制代码

 

posted on   艰苦奋斗中  阅读(760)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示