Skip to content

Commit e1c6de5

Browse files
authored
fix top10 sorting, add gameobject ping button, show top20
1 parent ac11c0e commit e1c6de5

File tree

1 file changed

+38
-18
lines changed

1 file changed

+38
-18
lines changed

Assets/Scripts/Editor/Tools/GetSelectedMeshInfo.cs

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

33
using System.Collections.Generic;
4+
using System.Linq;
45
using UnityEditor;
56
using UnityEngine;
67

@@ -14,7 +15,9 @@ public class GetSelectedMeshInfo : EditorWindow
1415
int totalVertices = 0;
1516
int totalTris = 0;
1617

17-
List<int> top10 = new List<int>();
18+
Dictionary<int, int> topList = new Dictionary<int, int>();
19+
IOrderedEnumerable<KeyValuePair<int, int>> sortedTopList;
20+
1821
MeshFilter[] meshes;
1922

2023
[MenuItem("Tools/UnityLibrary/GetMeshInfo")]
@@ -26,58 +29,75 @@ public static void ShowWindow()
2629

2730
void OnGUI()
2831
{
32+
// TODO process all selected gameobjects
2933
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
3142
{
3243
EditorGUILayout.LabelField("Selected: " + selection.name);
3344

3445
// update mesh info only if selection changed
3546
if (selectionChanged == true)
3647
{
37-
top10.Clear();
48+
selectionChanged = false;
49+
50+
// clear old top results
51+
topList.Clear();
3852

3953
totalMeshes = 0;
4054
totalVertices = 0;
4155
totalTris = 0;
4256

43-
// get all meshes
57+
// check all meshes
4458
meshes = selection.GetComponentsInChildren<MeshFilter>();
4559
for (int i = 0, length = meshes.Length; i < length; i++)
4660
{
4761
int verts = meshes[i].sharedMesh.vertexCount;
4862
totalVertices += verts;
4963
totalTris += meshes[i].sharedMesh.triangles.Length / 3;
5064
totalMeshes++;
51-
top10.Add(verts);
65+
topList.Add(i, verts);
5266
}
53-
selectionChanged = false;
5467

55-
// sort top10
56-
top10.Sort();
68+
// sort top list
69+
sortedTopList = topList.OrderByDescending(x => x.Value);
5770
}
5871

5972
// display stats
6073
EditorGUILayout.LabelField("Meshes: " + totalMeshes);
6174
EditorGUILayout.LabelField("Vertices: " + totalVertices);
6275
EditorGUILayout.LabelField("Triangles: " + totalTris);
6376
EditorGUILayout.Space();
64-
EditorGUILayout.LabelField("TOP 10", EditorStyles.boldLabel);
77+
EditorGUILayout.LabelField("TOP 20", EditorStyles.boldLabel);
6578

6679
// top 10
67-
if (meshes != null)
80+
if (meshes != null && sortedTopList != null)
6881
{
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)
7484
{
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;
7798
}
7899
}
79100
}
80-
81101
}
82102

83103
void OnSelectionChange()

0 commit comments

Comments
 (0)