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!

1 Answer

1

int triCount = 0;
int vertCount = 0;
MeshFilter allMeshFilters = Object.FindObjectsOfType();
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 MeshFilter[]. If that's the case, then you could do this instead: int triCount = 0; int vertCount = 0; Object[] allMeshFilters = Object.FindObjectsOfType<MeshFilter>(); foreach (Object o in allMeshFilters) { MeshFilter meshFilter = (MeshFilter)o; //force o to get typecast to a MeshFilter 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 MeshRenderer'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 MyScriptName.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.

–

:) but that'd be the smart way to do it.

–