1+ using System ;
2+ using System . Collections . Generic ;
3+ using UnityEngine . SceneManagement ;
4+ using UnityEngine . UIElements ;
5+
6+ namespace OC . Editor
7+ {
8+ public static class HierarchyFactory
9+ {
10+ public static List < TreeViewItemData < HierarchyItem > > CreateTreeViewData < T > ( Scene scene , Func < T , string [ ] > getHierarchyLevels ) where T : IComponent
11+ {
12+ var components = new List < T > ( ) ;
13+
14+ foreach ( var rootGameObject in scene . GetRootGameObjects ( ) )
15+ {
16+ components . AddRange ( rootGameObject . GetComponentsInChildren < T > ( ) ) ;
17+ }
18+
19+ var root = new HierarchyItem ( scene . name , null ) ;
20+
21+ foreach ( var component in components )
22+ {
23+ var level = getHierarchyLevels ( component ) ;
24+
25+ var parent = root ;
26+
27+ for ( var i = 0 ; i < level . Length - 1 ; i ++ )
28+ {
29+ HierarchyItem view = null ;
30+ if ( parent . HasChildren )
31+ {
32+ foreach ( var child in parent . Children )
33+ {
34+ if ( ! string . Equals ( child . Name , level [ i ] ) ) continue ;
35+ view = child ;
36+ break ;
37+ }
38+ }
39+
40+ if ( view == null )
41+ {
42+ view = new HierarchyItem ( level [ i ] , null ) ;
43+ parent . Children . Add ( view ) ;
44+ }
45+
46+ parent = view ;
47+ }
48+
49+ parent . Children . Add ( new HierarchyItem ( component . Component . name , component . Component ) ) ;
50+ }
51+
52+ return ResampleTreeViewData ( root ) ;
53+ }
54+
55+ public static List < TreeViewItemData < HierarchyItem > > FilterByName ( this List < TreeViewItemData < HierarchyItem > > source , string name )
56+ {
57+ var root = new HierarchyItem ( "root" , null ) ;
58+
59+ foreach ( var item in source )
60+ {
61+ root . Children . AddRange ( GetChildrenByFilteredName ( item . data , name ) ) ;
62+ }
63+
64+ return ResampleTreeViewData ( root ) ;
65+ }
66+
67+ private static List < TreeViewItemData < HierarchyItem > > ResampleTreeViewData ( HierarchyItem treeViewData )
68+ {
69+ var result = new List < TreeViewItemData < HierarchyItem > > ( ) ;
70+ var count = 0 ;
71+
72+ foreach ( var rootChild in treeViewData . Children )
73+ {
74+ result . Add ( CreateTreeItem ( rootChild , ref count ) ) ;
75+ }
76+
77+ return result ;
78+ }
79+
80+ private static TreeViewItemData < HierarchyItem > CreateTreeItem ( HierarchyItem item , ref int count )
81+ {
82+ if ( item . HasChildren )
83+ {
84+ var itemDataIndex = count ++ ;
85+
86+ var children = new List < TreeViewItemData < HierarchyItem > > ( ) ;
87+ foreach ( var child in item . Children )
88+ {
89+ children . Add ( CreateTreeItem ( child , ref count ) ) ;
90+ }
91+ return new TreeViewItemData < HierarchyItem > ( itemDataIndex , item , children ) ;
92+ }
93+ else
94+ {
95+ return new TreeViewItemData < HierarchyItem > ( count ++ , item ) ;
96+ }
97+ }
98+
99+ private static List < HierarchyItem > GetChildrenByFilteredName ( HierarchyItem source , string name )
100+ {
101+ var result = new List < HierarchyItem > ( ) ;
102+
103+ if ( source . HasChildren )
104+ {
105+ foreach ( var child in source . Children )
106+ {
107+ result . AddRange ( GetChildrenByFilteredName ( child , name ) ) ;
108+ }
109+ }
110+ else
111+ {
112+ if ( source . Name . Contains ( name , StringComparison . OrdinalIgnoreCase ) ) result . Add ( source ) ;
113+ }
114+
115+ return result ;
116+ }
117+ }
118+ }
0 commit comments