1
1
// display selected gameobject mesh stats (should work on prefabs,models in project window also)
2
2
3
3
using System . Collections . Generic ;
4
+ using System . Linq ;
4
5
using UnityEditor ;
5
6
using UnityEngine ;
6
7
@@ -14,7 +15,9 @@ public class GetSelectedMeshInfo : EditorWindow
14
15
int totalVertices = 0 ;
15
16
int totalTris = 0 ;
16
17
17
- List < int > top10 = new List < int > ( ) ;
18
+ Dictionary < int , int > topList = new Dictionary < int , int > ( ) ;
19
+ IOrderedEnumerable < KeyValuePair < int , int > > sortedTopList ;
20
+
18
21
MeshFilter [ ] meshes ;
19
22
20
23
[ MenuItem ( "Tools/UnityLibrary/GetMeshInfo" ) ]
@@ -26,58 +29,75 @@ public static void ShowWindow()
26
29
27
30
void OnGUI ( )
28
31
{
32
+ // TODO process all selected gameobjects
29
33
var selection = Selection . activeGameObject ;
30
- if ( selection != null )
34
+
35
+ // if have selection
36
+ if ( selection == null )
37
+ {
38
+ EditorGUILayout . LabelField ( "Select gameobject from scene or hierarchy.." ) ;
39
+
40
+ }
41
+ else
31
42
{
32
43
EditorGUILayout . LabelField ( "Selected: " + selection . name ) ;
33
44
34
45
// update mesh info only if selection changed
35
46
if ( selectionChanged == true )
36
47
{
37
- top10 . Clear ( ) ;
48
+ selectionChanged = false ;
49
+
50
+ // clear old top results
51
+ topList . Clear ( ) ;
38
52
39
53
totalMeshes = 0 ;
40
54
totalVertices = 0 ;
41
55
totalTris = 0 ;
42
56
43
- // get all meshes
57
+ // check all meshes
44
58
meshes = selection . GetComponentsInChildren < MeshFilter > ( ) ;
45
59
for ( int i = 0 , length = meshes . Length ; i < length ; i ++ )
46
60
{
47
61
int verts = meshes [ i ] . sharedMesh . vertexCount ;
48
62
totalVertices += verts ;
49
63
totalTris += meshes [ i ] . sharedMesh . triangles . Length / 3 ;
50
64
totalMeshes ++ ;
51
- top10 . Add ( verts ) ;
65
+ topList . Add ( i , verts ) ;
52
66
}
53
- selectionChanged = false ;
54
67
55
- // sort top10
56
- top10 . Sort ( ) ;
68
+ // sort top list
69
+ sortedTopList = topList . OrderByDescending ( x => x . Value ) ;
57
70
}
58
71
59
72
// display stats
60
73
EditorGUILayout . LabelField ( "Meshes: " + totalMeshes ) ;
61
74
EditorGUILayout . LabelField ( "Vertices: " + totalVertices ) ;
62
75
EditorGUILayout . LabelField ( "Triangles: " + totalTris ) ;
63
76
EditorGUILayout . Space ( ) ;
64
- EditorGUILayout . LabelField ( "TOP 10 " , EditorStyles . boldLabel ) ;
77
+ EditorGUILayout . LabelField ( "TOP 20 " , EditorStyles . boldLabel ) ;
65
78
66
79
// top 10
67
- if ( meshes != null )
80
+ if ( meshes != null && sortedTopList != null )
68
81
{
69
- // start from last index
70
- int from = meshes . Length ;
71
- // until 10 meshes or if less than 10
72
- int to = meshes . Length - Mathf . Min ( 10 , from ) ;
73
- for ( int i = from - 1 ; i >= to ; i -- )
82
+ int i = 0 ;
83
+ foreach ( var item in sortedTopList )
74
84
{
75
- int percent = ( int ) ( top10 [ i ] / ( float ) totalVertices * 100f ) ;
76
- EditorGUILayout . LabelField ( meshes [ i ] . name + " = " + top10 [ i ] + " (" + percent + "%)" ) ;
85
+ int percent = ( int ) ( item . Value / ( float ) totalVertices * 100f ) ;
86
+ EditorGUILayout . BeginHorizontal ( ) ;
87
+ // ping button
88
+ if ( GUILayout . Button ( " " , GUILayout . Width ( 16 ) ) )
89
+ {
90
+ EditorGUIUtility . PingObject ( meshes [ i ] . transform ) ;
91
+ }
92
+ EditorGUILayout . LabelField ( meshes [ i ] . name + " = " + item . Value + " (" + percent + "%)" ) ;
93
+ GUILayout . ExpandWidth ( true ) ;
94
+ EditorGUILayout . EndHorizontal ( ) ;
95
+
96
+ // show only first 20
97
+ if ( ++ i > 20 ) break ;
77
98
}
78
99
}
79
100
}
80
-
81
101
}
82
102
83
103
void OnSelectionChange ( )
0 commit comments