Skip to content

Commit 7a5919d

Browse files
authored
optimize GetSelectedMeshInfo (loop meshes only if selection changed), add TOP10 verts list
1 parent e8a5cde commit 7a5919d

File tree

1 file changed

+49
-11
lines changed

1 file changed

+49
-11
lines changed

Assets/Scripts/Editor/Tools/GetSelectedMeshInfo.cs

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11
// display selected gameobject mesh stats (should work on prefabs,models in project window also)
22

3+
using System.Collections.Generic;
34
using UnityEditor;
45
using UnityEngine;
56

67
namespace UnityLibrary
78
{
89
public class GetSelectedMeshInfo : EditorWindow
910
{
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+
1020
[MenuItem("Tools/UnityLibrary/GetMeshInfo")]
1121
public static void ShowWindow()
1222
{
@@ -17,34 +27,62 @@ public static void ShowWindow()
1727
void OnGUI()
1828
{
1929
var selection = Selection.activeGameObject;
20-
2130
if (selection != null)
2231
{
2332
EditorGUILayout.LabelField("Selected: " + selection.name);
2433

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)
3236
{
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();
3657
}
3758

3859
// display stats
3960
EditorGUILayout.LabelField("Meshes: " + totalMeshes);
4061
EditorGUILayout.LabelField("Vertices: " + totalVertices);
4162
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+
}
4279
}
4380

4481
}
4582

4683
void OnSelectionChange()
4784
{
85+
selectionChanged = true;
4886
// force redraw window
4987
Repaint();
5088
}

0 commit comments

Comments
 (0)