1
1
// display selected gameobject mesh stats (should work on prefabs,models in project window also)
2
2
3
+ using System . Collections . Generic ;
3
4
using UnityEditor ;
4
5
using UnityEngine ;
5
6
6
7
namespace UnityLibrary
7
8
{
8
9
public class GetSelectedMeshInfo : EditorWindow
9
10
{
11
+ bool selectionChanged = false ;
12
+
13
+ int totalMeshes = 0 ;
14
+ int totalVertices = 0 ;
15
+ int totalTris = 0 ;
16
+
17
+ List < int > top10 = new List < int > ( ) ;
18
+ MeshFilter [ ] meshes ;
19
+
10
20
[ MenuItem ( "Tools/UnityLibrary/GetMeshInfo" ) ]
11
21
public static void ShowWindow ( )
12
22
{
@@ -17,34 +27,62 @@ public static void ShowWindow()
17
27
void OnGUI ( )
18
28
{
19
29
var selection = Selection . activeGameObject ;
20
-
21
30
if ( selection != null )
22
31
{
23
32
EditorGUILayout . LabelField ( "Selected: " + selection . name ) ;
24
33
25
- int totalMeshes = 0 ;
26
- int totalVertices = 0 ;
27
- int totalTris = 0 ;
28
-
29
- // get all meshes
30
- var meshes = selection . GetComponentsInChildren < MeshFilter > ( ) ;
31
- for ( int i = 0 , length = meshes . Length ; i < length ; i ++ )
34
+ // update mesh info only if selection changed
35
+ if ( selectionChanged == true )
32
36
{
33
- totalVertices += meshes [ i ] . sharedMesh . vertexCount ;
34
- totalTris += meshes [ i ] . sharedMesh . triangles . Length / 3 ;
35
- totalMeshes ++ ;
37
+ top10 . Clear ( ) ;
38
+
39
+ totalMeshes = 0 ;
40
+ totalVertices = 0 ;
41
+ totalTris = 0 ;
42
+
43
+ // get all meshes
44
+ meshes = selection . GetComponentsInChildren < MeshFilter > ( ) ;
45
+ for ( int i = 0 , length = meshes . Length ; i < length ; i ++ )
46
+ {
47
+ int verts = meshes [ i ] . sharedMesh . vertexCount ;
48
+ totalVertices += verts ;
49
+ totalTris += meshes [ i ] . sharedMesh . triangles . Length / 3 ;
50
+ totalMeshes ++ ;
51
+ top10 . Add ( verts ) ;
52
+ }
53
+ selectionChanged = false ;
54
+
55
+ // sort top10
56
+ top10 . Sort ( ) ;
36
57
}
37
58
38
59
// display stats
39
60
EditorGUILayout . LabelField ( "Meshes: " + totalMeshes ) ;
40
61
EditorGUILayout . LabelField ( "Vertices: " + totalVertices ) ;
41
62
EditorGUILayout . LabelField ( "Triangles: " + totalTris ) ;
63
+ EditorGUILayout . Space ( ) ;
64
+ EditorGUILayout . LabelField ( "TOP 10" , EditorStyles . boldLabel ) ;
65
+
66
+ // top 10
67
+ if ( meshes != null )
68
+ {
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 -- )
74
+ {
75
+ int percent = ( int ) ( top10 [ i ] / ( float ) totalVertices * 100f ) ;
76
+ EditorGUILayout . LabelField ( meshes [ i ] . name + " = " + top10 [ i ] + " (" + percent + "%)" ) ;
77
+ }
78
+ }
42
79
}
43
80
44
81
}
45
82
46
83
void OnSelectionChange ( )
47
84
{
85
+ selectionChanged = true ;
48
86
// force redraw window
49
87
Repaint ( ) ;
50
88
}
0 commit comments