1
\$\begingroup\$

I have been working on a game and every time a player prefab is instantiated, it creates 1M+ triangles. That is a nightmare considering the model itself is not even close to 1M. The only thing on the player prefab is the soldier model and some objects for holding the weapons. Nothing more then the model. How can I see which objects in the scene are utilizing the most triangle count? I need to get to the bottom of this. Triangle count model_torso tris model_feet tris model_hands tris

model_head tris

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

Get every child game object in the scene, Do GetComponents for each, go over all the MeshFilters and count Mesh.triangles.

foreach( GameObject g in UnityEngine.SceneManagement.SceneManager.GetActiveScene().GetRootGameObjects())
{
    int triangleCount = 0;
    foreach(MeshFilter m in getComponentsInChildren(typeof(MeshFilter)))
    {
        triangleCount += m.mesh.triangles;
    }
    Debug.Log(g.name + " has " + triangleCount.ToString() + " triangles");
}
\$\endgroup\$
2
  • \$\begingroup\$ This answer sounds good, but lacks so much. You should really add some code or explain more. \$\endgroup\$
    – aidangig
    Commented Jul 10, 2019 at 22:46
  • \$\begingroup\$ mesh.triangles is an int[] that contains a list of triangle indices. So 3 indices make one triangle. To get the real triangle count, you need to call triangles.Length / 3. These indices refer to the positions stored in m.mesh.vertices, which therefore has the same Length. \$\endgroup\$
    – KYL3R
    Commented Aug 21, 2022 at 17:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .