- Home /
How to get the number of vertices, triangles programmatically?
Hello, is it possible to get the number of vertices, polygons programmatically, e.g. as written in the statistics inside the Unity editor? If so, how?
My fault, badly answered question. Should have been: is it possible to get the number of vertices, polygons in the whole scene programmatically?
Thank you
Answer by Adam Rademacher · Oct 14, 2010 at 02:28 PM
Mesh.vertexCount
Mesh.triangles.Length
EDIT: Try this (C#):
int totalVertexes = 0; int totalTriangles = 0;
foreach(MeshFilter mf in FindObjectsOfType(typeof(Meshfilter)) { totalVertexes += mf.mesh.vertexCount; totalTriangles += mf.mesh.triangles.Length; }
Note: this is a very expensive call, so you probably don't want to run it often.
Hello, thank you for your answer. However, how to get the number of vertices and polygon in a scene at a certain moment? Or, how to get the mesh object for each object in the scene?
Edited to add functionality that should help you.
Answer by PatHightree · Oct 25, 2011 at 09:02 AM
A correction on Adam's answer :
meshFilter.sharedMesh.vertexCount
meshFilter.sharedMesh.triangles.Length / 3
The triangles list contains 3 vertex indices for each triangle, so it needs to be divided by 3. I'm using sharedMesh here because Unity complains of leaked objects when the mesh is used.
Answer by FZ_Applications · Jun 27, 2019 at 10:44 AM
Just to complete this: You can use UnityEditor.UnityStats.triangles (also vertices, batches, ...) But this not works in built games
I use it that way: (https://docs.unity3d.com/Manual/PlatformDependentCompilation.html)
#if UnityEditor
Debug.Log(UnityEditor.UnityStats.triangles);
#endif