Skip to content

Instantly share code, notes, and snippets.

@ThaDewey
Created July 13, 2017 02:39
Show Gist options
  • Save ThaDewey/39b2f54376e1516bc8079cecb824ad11 to your computer and use it in GitHub Desktop.
Save ThaDewey/39b2f54376e1516bc8079cecb824ad11 to your computer and use it in GitHub Desktop.
A Simple Polygon counter for Unity
using UnityEngine;
using UnityEditor;
using System.Collections;
public class PolyCount : EditorWindow {
long numPolys = 0;
bool deep = false;
bool showList = false;
Vector2 scroll = new Vector2();
ArrayList childrenCounts = new ArrayList();
[MenuItem ("Window/PolyCount")]
static void Init () {
// Get existing open window or if none, make a new one:
PolyCount window = (PolyCount)EditorWindow.GetWindow (typeof (PolyCount));
window.Show();
}
void OnSelectionChange() {
Transform[] selected = Selection.transforms;
numPolys = 0;
foreach (Transform t in selected) {
MeshFilter m = t.gameObject.GetComponent<MeshFilter>() as MeshFilter;
if (m != null) {
numPolys += m.sharedMesh.triangles.Length;
}
if (deep) {
MeshFilter[] childrenFilters = t.gameObject.GetComponentsInChildren<MeshFilter>() as MeshFilter[];
foreach(MeshFilter mf in childrenFilters) {
numPolys += mf.sharedMesh.triangles.Length;
}
}
}
numPolys /= 3;
Repaint();
}
void genList() {
childrenCounts.Clear();
Transform[] selected = Selection.transforms;
GameObject current;
foreach (Transform t in selected) {
for (int i = 0; i < t.childCount; i++) {
current = t.GetChild(i).gameObject;
MeshFilter m = current.GetComponent<MeshFilter>() as MeshFilter;
int currentPolys = 0;
if (m != null) {
currentPolys += m.sharedMesh.triangles.Length;
}
if (deep) {
MeshFilter[] childrenFilters = current.GetComponentsInChildren<MeshFilter>() as MeshFilter[];
foreach(MeshFilter mf in childrenFilters) {
currentPolys += mf.sharedMesh.triangles.Length;
}
}
currentPolys /= 3;
childrenCounts.Add(new MyPolyCount(current, currentPolys));
}
}
childrenCounts.Sort(new PolyCountCompare());
showList = true;
}
void OnGUI () {
deep = EditorGUILayout.Toggle("Include Children?", deep);
EditorGUILayout.LabelField("Poly Count: " + numPolys);
if (GUILayout.Button("List Children")) {
genList();
}
showList = EditorGUILayout.Toggle("Show List?", showList);
if (showList) {
scroll = EditorGUILayout.BeginScrollView(scroll);
for (int i = 0; i < childrenCounts.Count; i++) {
MyPolyCount o = (MyPolyCount)childrenCounts[i];
o.display();
if (o.remove) {
childrenCounts.RemoveAt(i);
}
}
EditorGUILayout.EndScrollView();
}
}
class MyPolyCount {
public GameObject go;
public int count = 0;
public bool remove = false;
public MyPolyCount(GameObject g, int c) {
go = g;
count = c;
}
public void display() {
EditorGUILayout.BeginHorizontal();
if (go == null) {
remove = true;
GUILayout.Label("(deleted)");
} else if (GUILayout.Button(go.name)) {
Selection.activeGameObject = go;
}
GUILayout.Label(count + "");
EditorGUILayout.EndHorizontal();
}
public override string ToString() {
return go.name + "\t\t" + count;
}
}
class PolyCountCompare : IComparer {
public int Compare(object a, object b) {
MyPolyCount pcA = (MyPolyCount)a;
MyPolyCount pcB = (MyPolyCount)b;
return (pcB.count - pcA.count);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment