22using System . Collections ;
33using System . Collections . Generic ;
44using System . Linq ;
5- using System . Linq . Expressions ;
65using System . Reflection ;
7- using System . Runtime . Remoting . Messaging ;
8- using System . Xml ;
96using UnityEditor ;
107using UnityEditor . IMGUI . Controls ;
11- using UnityEditor . SceneManagement ;
12- using UnityEditorInternal ;
138using UnityEngine ;
149using UnityEngine . Experimental . Rendering ;
1510using UnityHierarchyFolders . Runtime ;
1611using Object = UnityEngine . Object ;
1712
18- namespace Plugins . UnityHierarchyFolders
19- {
13+ namespace Plugins . UnityHierarchyFolders {
2014 public class HierarchyFolderIcon
2115 {
22- private static Texture2D _openFolderTexture ;
23- private static Texture2D _closedFolderTexture ;
24- private static Texture2D _openFolderSelectedTexture ;
25- private static Texture2D _closedFolderSelectedTexture ;
16+ private static Texture2D openFolderTexture ;
17+ private static Texture2D closedFolderTexture ;
18+ private static Texture2D openFolderSelectedTexture ;
19+ private static Texture2D closedFolderSelectedTexture ;
2620
27- private static bool _isInitialized ;
28- private static bool _hasProcessedFrame ;
21+ private static bool isInitialized ;
22+ private static bool hasProcessedFrame ;
2923
3024 // Reflected members
31- private static PropertyInfo _sceneHierarchyProperty ;
32- private static PropertyInfo _treeViewProperty ;
33- private static PropertyInfo _dataProperty ;
34- private static MethodInfo _getRowsMethod ;
35- private static MethodInfo _isExpandedMethod ;
36- private static PropertyInfo _selectedIconProperty ;
37- private static PropertyInfo _objectPPTRProperty ;
38- private static MethodInfo _getAllSceneHierarchyWindowsMethod ;
25+ private static PropertyInfo prop_sceneHierarchy ;
26+ private static PropertyInfo prop_treeView ;
27+ private static PropertyInfo prop_data ;
28+ private static PropertyInfo prop_selectedIcon ;
29+ private static PropertyInfo prop_objectPPTR ;
3930
31+ private static MethodInfo meth_getRows ;
32+ private static MethodInfo meth_isExpanded ;
33+ private static MethodInfo meth_getAllSceneHierarchyWindows ;
4034
4135 [ InitializeOnLoadMethod ]
4236 static void Startup ( )
4337 {
4438 Initialize ( ) ;
45- EditorApplication . update += RefreshFolderIcons ;
39+ EditorApplication . update += ResetFolderIcons ;
4640 EditorApplication . hierarchyWindowItemOnGUI += RefreshFolderIcons ;
4741 }
4842
4943 private static Texture2D GetTintedTexture ( Texture2D original )
5044 {
5145 var tinted = new Texture2D ( original . width , original . height ,
52- original . graphicsFormat , original . mipmapCount , TextureCreationFlags . MipChain ) ;
46+ original . graphicsFormat , original . mipmapCount , TextureCreationFlags . MipChain ) ;
5347
5448 Graphics . CopyTexture ( original , tinted ) ;
5549 var data = tinted . GetRawTextureData < Color32 > ( ) ;
5650 for ( int index = 0 , len = data . Length ; index < len ; index ++ )
5751 {
5852 Color32 c = data [ index ] ;
5953 byte a = c . a ;
60-
61- Color . RGBToHSV ( c , out float h , out float s , out float v ) ;
6254 c = Color . HSVToRGB ( 0 , 0 , 1 ) ;
6355 c . a = a ;
6456
6557 data [ index ] = c ;
6658 }
6759
68- var aaa = tinted . width * tinted . height * 4 ;
60+ var mipmapSize = tinted . width * tinted . height * 4 ;
6961 var offset = 0 ;
7062 for ( int index = 0 ; index < tinted . mipmapCount ; index ++ )
7163 {
7264 tinted . SetPixelData ( data , index , offset ) ;
73- aaa >>= 2 ;
74- offset += aaa ;
65+ mipmapSize >>= 2 ;
66+ offset += mipmapSize ;
7567 }
7668 tinted . Apply ( ) ;
7769
@@ -80,80 +72,68 @@ private static Texture2D GetTintedTexture(Texture2D original)
8072
8173 private static void Initialize ( )
8274 {
83- if ( _isInitialized )
84- {
85- return ;
86- }
75+ if ( isInitialized ) { return ; }
8776
88- _closedFolderTexture = ( Texture2D ) EditorGUIUtility . IconContent ( "Folder Icon" ) . image ;
89- _openFolderTexture = ( Texture2D ) EditorGUIUtility . IconContent ( "FolderEmpty Icon" ) . image ;
77+ closedFolderTexture = ( Texture2D ) EditorGUIUtility . IconContent ( "Folder Icon" ) . image ;
78+ openFolderTexture = ( Texture2D ) EditorGUIUtility . IconContent ( "FolderEmpty Icon" ) . image ;
9079
91- _openFolderSelectedTexture = GetTintedTexture ( _openFolderTexture ) ;
92- _closedFolderSelectedTexture = GetTintedTexture ( _closedFolderTexture ) ;
80+ openFolderSelectedTexture = GetTintedTexture ( openFolderTexture ) ;
81+ closedFolderSelectedTexture = GetTintedTexture ( closedFolderTexture ) ;
9382
9483 const BindingFlags BindingAll = BindingFlags . Public
9584 | BindingFlags . NonPublic | BindingFlags . Static | BindingFlags . Instance ;
9685
97- //public static List<SceneHierarchyWindow> GetAllSceneHierarchyWindows()
9886 var assembly = typeof ( SceneView ) . Assembly ;
9987
100- var sceneHierarchyWindowType = assembly . GetType ( "UnityEditor.SceneHierarchyWindow" ) ;
101- _getAllSceneHierarchyWindowsMethod = sceneHierarchyWindowType . GetMethod ( "GetAllSceneHierarchyWindows" , BindingAll ) ;
102- _sceneHierarchyProperty = sceneHierarchyWindowType . GetProperty ( "sceneHierarchy" ) ;
88+ var type_sceneHierarchyWindow = assembly . GetType ( "UnityEditor.SceneHierarchyWindow" ) ;
89+ meth_getAllSceneHierarchyWindows = type_sceneHierarchyWindow . GetMethod ( "GetAllSceneHierarchyWindows" , BindingAll ) ;
90+ prop_sceneHierarchy = type_sceneHierarchyWindow . GetProperty ( "sceneHierarchy" ) ;
10391
104- var sceneHierarchyType = assembly . GetType ( "UnityEditor.SceneHierarchy" ) ;
105- _treeViewProperty = sceneHierarchyType . GetProperty ( "treeView" , BindingAll ) ;
92+ var type_sceneHierarchy = assembly . GetType ( "UnityEditor.SceneHierarchy" ) ;
93+ prop_treeView = type_sceneHierarchy . GetProperty ( "treeView" , BindingAll ) ;
10694
107- var treeViewControllerType = assembly . GetType ( "UnityEditor.IMGUI.Controls.TreeViewController" ) ;
108- _dataProperty = treeViewControllerType . GetProperty ( "data" , BindingAll ) ;
95+ var type_treeViewController = assembly . GetType ( "UnityEditor.IMGUI.Controls.TreeViewController" ) ;
96+ prop_data = type_treeViewController . GetProperty ( "data" , BindingAll ) ;
10997
110- var iTreeViewDataSourceType = assembly . GetType ( "UnityEditor.IMGUI.Controls.ITreeViewDataSource" ) ;
111- _getRowsMethod = iTreeViewDataSourceType . GetMethod ( "GetRows" ) ;
112- _isExpandedMethod = iTreeViewDataSourceType . GetMethod ( "IsExpanded" , new Type [ ] { typeof ( TreeViewItem ) } ) ;
98+ var type_iTreeViewDataSource = assembly . GetType ( "UnityEditor.IMGUI.Controls.ITreeViewDataSource" ) ;
99+ meth_getRows = type_iTreeViewDataSource . GetMethod ( "GetRows" ) ;
100+ meth_isExpanded = type_iTreeViewDataSource . GetMethod ( "IsExpanded" , new Type [ ] { typeof ( TreeViewItem ) } ) ;
113101
114- var gameObjectTreeViewItemType = assembly . GetType ( "UnityEditor.GameObjectTreeViewItem" ) ;
115- _selectedIconProperty = gameObjectTreeViewItemType . GetProperty ( "selectedIcon" , BindingAll ) ;
116- _objectPPTRProperty = gameObjectTreeViewItemType . GetProperty ( "objectPPTR" , BindingAll ) ;
102+ var type_gameObjectTreeViewItem = assembly . GetType ( "UnityEditor.GameObjectTreeViewItem" ) ;
103+ prop_selectedIcon = type_gameObjectTreeViewItem . GetProperty ( "selectedIcon" , BindingAll ) ;
104+ prop_objectPPTR = type_gameObjectTreeViewItem . GetProperty ( "objectPPTR" , BindingAll ) ;
117105
118- _isInitialized = true ;
106+ isInitialized = true ;
119107 }
120108
121- private static void RefreshFolderIcons ( )
109+ private static void ResetFolderIcons ( )
122110 {
123111 Initialize ( ) ;
124- _hasProcessedFrame = false ;
112+ hasProcessedFrame = false ;
125113 }
126114
127-
128115 private static void RefreshFolderIcons ( int instanceid , Rect selectionrect )
129116 {
130- if ( _hasProcessedFrame )
131- {
132- return ;
133- }
134-
135- _hasProcessedFrame = true ;
117+ if ( hasProcessedFrame ) { return ; }
118+ hasProcessedFrame = true ;
136119
137- var windows = ( ( IEnumerable ) _getAllSceneHierarchyWindowsMethod . Invoke ( null , new object [ 0 ] ) ) . Cast < EditorWindow > ( ) . ToList ( ) ;
120+ var windows = ( ( IEnumerable ) meth_getAllSceneHierarchyWindows . Invoke ( null , Array . Empty < object > ( ) ) ) . Cast < EditorWindow > ( ) . ToList ( ) ;
138121 foreach ( EditorWindow h in windows )
139122 {
140- var sceneHierarchy = _sceneHierarchyProperty . GetValue ( h ) ;
141- var treeView = _treeViewProperty . GetValue ( sceneHierarchy ) ;
142- var data = _dataProperty . GetValue ( treeView ) ;
123+ var sceneHierarchy = prop_sceneHierarchy . GetValue ( h ) ;
124+ var treeView = prop_treeView . GetValue ( sceneHierarchy ) ;
125+ var data = prop_data . GetValue ( treeView ) ;
143126
144- IList < TreeViewItem > rows = ( IList < TreeViewItem > ) _getRowsMethod . Invoke ( data , new object [ 0 ] ) ;
127+ var rows = ( IList < TreeViewItem > ) meth_getRows . Invoke ( data , Array . Empty < object > ( ) ) ;
145128 foreach ( TreeViewItem item in rows )
146129 {
147- Object itemObject = ( Object ) _objectPPTRProperty . GetValue ( item ) ;
148- if ( ! itemObject || ! Folder . folders . Contains ( itemObject . GetInstanceID ( ) ) )
149- {
150- continue ;
151- }
130+ var itemObject = ( Object ) prop_objectPPTR . GetValue ( item ) ;
131+ if ( ! itemObject || ! Folder . IsFolder ( itemObject ) ) { continue ; }
152132
153- var isExpanded = ( bool ) _isExpandedMethod . Invoke ( data , new object [ ] { item } ) ;
133+ var isExpanded = ( bool ) meth_isExpanded . Invoke ( data , new object [ ] { item } ) ;
154134
155- item . icon = isExpanded ? _openFolderTexture : _closedFolderTexture ;
156- _selectedIconProperty . SetValue ( item , isExpanded ? _openFolderSelectedTexture : _closedFolderSelectedTexture ) ;
135+ item . icon = isExpanded ? openFolderTexture : closedFolderTexture ;
136+ prop_selectedIcon . SetValue ( item , isExpanded ? openFolderSelectedTexture : closedFolderSelectedTexture ) ;
157137 }
158138 }
159139 }
0 commit comments