- Home /
Display current vertex/tri count drawn in game (When built)
I need to show a variety of performance metrics inside my built project (It's kinda a tech demo)
In the editor, the stats windows displays a bunch of really useful stats, including Tri & Vert counts, however, as far as I can tell there's not a way to show this window / get the stats inside the built executable?
Is there any way to get these stats (Just vert/tri count would be fine, but the draw calls etc would be useful aswell!) and display them ingame? Thanks!
Answer by nesis · Mar 07, 2014 at 01:19 PM
int triCount = 0;
int vertCount = 0;
MeshFilter[] allMeshFilters = Object.FindObjectsOfType<MeshFilter>();
foreach (MeshFilter meshFilter in allMeshFilters) {
if (meshFilter.enabled) {
triCount += meshFilter.mesh.triangles.Length;
vertCount += meshFilter.mesh.vertices.Length;
}
}
Warning: This could be a slow process if you have a ton of meshes at runtime!
I'm not sure if you need to manually typecast Object.FindObjectsOfType() to $$anonymous$$eshFilter[]. If that's the case, then you could do this ins$$anonymous$$d:
int triCount = 0;
int vertCount = 0;
Object[] all$$anonymous$$eshFilters = Object.FindObjectsOfType<$$anonymous$$eshFilter>();
foreach (Object o in all$$anonymous$$eshFilters) {
$$anonymous$$eshFilter meshFilter = ($$anonymous$$eshFilter)o; //force o to get typecast to a $$anonymous$$eshFilter
if (meshFilter!=null && meshFilter.enabled) {
triCount += meshFilter.mesh.triangles.Length;
vertCount += meshFilter.mesh.vertices.Length;
}
}
Won't this show the number of verts/tris in total though, not the amount being currently rendered?
If you want the amount currently being rendered, you'll need to be a bit tricky. You'll need at attach a script to every $$anonymous$$eshRenderer's GameObject, containing a method called OnWillRenderObject() (it gets called once for each camera that will render the object). You'll need to have static variables in this script to track the two counts, and add to them inside OnWillRenderObject(). Then in another script you can call $$anonymous$$yScriptName.triCount and such to get those values for display, then reset them to 0 for the next frame.
To figure out if any part of a mesh is being rendered by some camera, you can check the renderer.isVisible flag.
Your answer
![](https://koobas.hobune.stream/wayback/20220613135619im_/https://answers.unity.com/themes/thub/images/avi.jpg)