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

A correction on Adam’s answer :

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.


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.

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: (Unity - Manual: Conditional Compilation)

#if UnityEditor