- Home /
How could I count my poly / tri ?
Hello everyone,
I am a 3d artist, currently working on a test to get a job.
This test has a polygon limitation. If I enable the 'stats' window in the camera tab, it gives me every rendered triangles which is not what I need. I need to know how many polygons / triangles I have from my meshes, so yes I could just multiply the amount of triangles by the number of items I duplicated etc (count it manually) but it would take useless time.
Every software has that I guess it's somewhere in here I just can't find it?
Thank you for your help
Adrien
Answer by flashframe · Apr 30, 2016 at 03:09 PM
Hi, I wrote a little Editor Window that does what you want. Put this script in a folder called Editor, and access the panel by going to Tools/MeshInfo. It will display the Triangle and Vertex count for the object that is selected in the hierarchy.
using UnityEngine;
using System.Collections;
using UnityEditor;
public class MeshInfo : EditorWindow {
private int vertexCount;
private int submeshCount;
private int triangleCount;
[MenuItem ("Tools/Mesh Info")]
static void Init ()
{
// Get existing open window or if none, make a new one:
MeshInfo window = (MeshInfo) EditorWindow.GetWindow (typeof (MeshInfo));
window.titleContent.text = "Mesh Info";
}
void OnSelectionChange()
{
Repaint();
}
void OnGUI()
{
if(Selection.activeGameObject && Selection.activeGameObject.GetComponent<MeshFilter>())
{
vertexCount = Selection.activeGameObject.GetComponent<MeshFilter>().sharedMesh.vertexCount;
triangleCount = Selection.activeGameObject.GetComponent<MeshFilter>().sharedMesh.triangles.Length/3;
submeshCount = Selection.activeGameObject.GetComponent<MeshFilter>().sharedMesh.subMeshCount;
EditorGUILayout.LabelField(Selection.activeGameObject.name);
EditorGUILayout.LabelField("Vertices: ", vertexCount.ToString());
EditorGUILayout.LabelField("Triangles: ", triangleCount.ToString());
EditorGUILayout.LabelField("SubMeshes: ", submeshCount.ToString());
}
}
}
Hey.
well that's really nice ! Thank you for your time, I'll try that out :)
Adrien
It's working out pretty well. $$anonymous$$ay I ask you for one more thing? I'd need a few more line to get the total polycount. (or multiple object selected?)
Thank you very much
Adrien
If you want it to work for multiple selected objects you could try changing the OnGui() method to this:
void OnGUI()
{
vertexCount = 0;
triangleCount = 0;
submeshCount = 0;
foreach(GameObject g in Selection.gameObjects)
{
if(g.GetComponent<$$anonymous$$eshFilter>())
{
vertexCount += g.GetComponent<$$anonymous$$eshFilter>().shared$$anonymous$$esh.vertexCount;
triangleCount += g.GetComponent<$$anonymous$$eshFilter>().shared$$anonymous$$esh.triangles.Length/3;
submeshCount += g.GetComponent<$$anonymous$$eshFilter>().shared$$anonymous$$esh.sub$$anonymous$$eshCount;
}
}
EditorGUILayout.LabelField("Vertices: ", vertexCount.ToString());
EditorGUILayout.LabelField("Triangles: ", triangleCount.ToString());
EditorGUILayout.LabelField("Sub$$anonymous$$eshes: ", submeshCount.ToString());
}
Don't forget to accept my answer if you feel it solves your original question :-)
Answer by kinginthenorth · May 02, 2016 at 03:18 PM
Hey,
Thank you again for your time, I'm sorry to annoy with that, but that Void edit does not work, can't count for multiple objects.
Thanks
Adrien
Hmm, it works for me. For clarity, this is the full script:
using UnityEngine;
using System.Collections;
using UnityEditor;
public class $$anonymous$$eshInfo : EditorWindow {
private int vertexCount;
private int submeshCount;
private int triangleCount;
[$$anonymous$$enuItem ("Tools/$$anonymous$$esh Info")]
static void Init ()
{
// Get existing open window or if none, make a new one:
$$anonymous$$eshInfo window = ($$anonymous$$eshInfo) EditorWindow.GetWindow (typeof ($$anonymous$$eshInfo));
window.titleContent.text = "$$anonymous$$esh Info";
}
void OnSelectionChange()
{
Repaint();
}
void OnGUI()
{
vertexCount = 0;
triangleCount = 0;
submeshCount = 0;
foreach(GameObject g in Selection.gameObjects)
{
if(g.GetComponent<$$anonymous$$eshFilter>())
{
vertexCount += g.GetComponent<$$anonymous$$eshFilter>().shared$$anonymous$$esh.vertexCount;
triangleCount += g.GetComponent<$$anonymous$$eshFilter>().shared$$anonymous$$esh.triangles.Length/3;
submeshCount += g.GetComponent<$$anonymous$$eshFilter>().shared$$anonymous$$esh.sub$$anonymous$$eshCount;
}
}
EditorGUILayout.LabelField("Vertices: ", vertexCount.ToString());
EditorGUILayout.LabelField("Triangles: ", triangleCount.ToString());
EditorGUILayout.LabelField("Sub$$anonymous$$eshes: ", submeshCount.ToString());
}
}
This gives me the total Vertices and Triangle count if I select 1 or more gameobjects in the hierarchy with meshfilter components. $$anonymous$$aybe I misunderstood your request?
OK, here's yet another iteration to extend the count to selected *GameObject*s and all their children, only *MeshFilter*s taken into account:
using UnityEngine;
using System.Collections;
using UnityEditor;
public class MeshInfo : EditorWindow
{
private int vertexCount;
private int submeshCount;
private int triangleCount;
[MenuItem("Tools/Mesh Info")]
static void Init()
{
// Get existing open window or if none, make a new one:
MeshInfo window = (MeshInfo)EditorWindow.GetWindow(typeof(MeshInfo));
window.titleContent.text = "Mesh Info";
}
void OnSelectionChange()
{
Repaint();
}
void OnGUI()
{
vertexCount = 0;
triangleCount = 0;
submeshCount = 0;
foreach(GameObject g in Selection.gameObjects)
{
foreach(MeshFilter mf in g.GetComponentsInChildren<MeshFilter>())
{
vertexCount += mf.sharedMesh.vertexCount;
triangleCount += mf.sharedMesh.triangles.Length / 3;
submeshCount += mf.sharedMesh.subMeshCount;
}
}
EditorGUILayout.LabelField("Vertices: ", vertexCount.ToString());
EditorGUILayout.LabelField("Triangles: ", triangleCount.ToString());
EditorGUILayout.LabelField("SubMeshes: ", submeshCount.ToString());
}
}
Okay it's perfectly working thank you a lot for your help I'm just very lame at coding aha.
Your answer
Follow this Question
Related Questions
poly and vertex counting at runtime 6 Answers
Perfect Cubes vs. 1 Rectangular Prism (Performance Question) 1 Answer
How to increase the polygon count? 0 Answers
searching for a "model a game car" tutorial? 2 Answers
How many tris? 1 Answer