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.
1 Answer
\$\begingroup\$
\$\endgroup\$
2
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");
}
-
\$\begingroup\$ This answer sounds good, but lacks so much. You should really add some code or explain more. \$\endgroup\$– aidangigCommented 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 calltriangles.Length / 3
. These indices refer to the positions stored inm.mesh.vertices
, which therefore has the same Length. \$\endgroup\$– KYL3RCommented Aug 21, 2022 at 17:11