- Home /
Selected Mesh Polygon Count
i am building a game on a low spec system nd i really need to know that is their is a way to count the polygons of a selected mesh Through script i am aware of the stats window nd verts and tries count but i want to count the polygons of my mesh can someone plz guide me on a right path if it is possible then how to do it any help would be appreciated Thanks AR
Answer by ArkaneX · Sep 04, 2013 at 01:32 PM
You can use
gameObject.GetComponent<MeshFilter>().mesh.triangles.Length/3
For more reference, please look at Mesh.triangles
Thanks so much for the reply
but when i use your suggetion its giving me this error
Assets/_Scripts/poly_count.cs(16,79): error CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement
may b i am missimg something
$$anonymous$$y script returns number of triangles, but this number has to be assigned to some variable, e.g.
var numTriangles = gameObject.GetComponent<$$anonymous$$eshFilter>().mesh.triangles.Length/3;
Debug.Log(numTriangles);
assu$$anonymous$$g that gameObject is the object (with mesh) for which you want to check number of triangles.
yup i got that and done the changes and its working but for the exact numbers of polygons which my mesh had in 3dsmax i have to divide the result by 2 nd its working awesomely thank you so much fr support
That means you probably had your mesh created with quads not triangles. On import to Unity the mesh was triangulated, and the number of created triangles is twice the number of quads. So your calculation will be valid for quad meshes only.
Thanks for looking at this .... :)
yup figured it out now and trying to do exact polygon count as it shows in 3dsmax so cud u suggest some calculations coz it is fairly differ with every other object i import so can u help with this exact poly count thing
Answer by sysmaya · Jul 20, 2016 at 02:39 PM
Select Mesh in inspector. Ver Imagen.
Answer by AR_Rizvi · Sep 06, 2013 at 04:49 AM
i used this code for my script of counting selected mesh's polys and tries
using UnityEngine;
using UnityEditor;
public class SelectionCount : EditorWindow {
// scripted by AR_Rizvi
// date 6-sep-2013
[MenuItem("Window/SelectionCount")] //FindMissingScripts //MissingTexture
public static void ShowWindow()
{
EditorWindow.GetWindow(typeof(SelectionCount));
}
// static int selection = 0, lastSelection = 0;
void OnInspectorUpdate() {
// Call Repaint on OnInspectorUpdate as it repaints the windows
// less times as if it was OnGUI/Update
Repaint();
}
int moreThenOneComp = 0, selection =0;
float mynum;
float mynum1;
float mynum_tri;
float unit_tri;
GameObject Obj;
public void OnGUI()
{
GUILayout.BeginHorizontal ("box");
GUILayout.Label("Selected GameObject");
if (GUILayout.Button("Find Texture (None)"))
{
FindInSelected();
}
GUILayout.EndHorizontal ();
//*******************************
if(Selection.objects.Length != selection)
{
selection = Selection.objects.Length;
moreThenOneComp = 0;
GameObject[] cobj = Selection.gameObjects;
foreach (GameObject c in cobj)
{
if(c.GetComponents<Component>().Length > 1)
{
moreThenOneComp++;
}
}
//----------------------
//----------------------
}
GUILayout.BeginHorizontal ("box");
GUILayout.Label(selection+": Selected Object");
GUILayout.Label(moreThenOneComp+": Has more Component");
GUILayout.EndHorizontal ();
//****************************
GUILayout.Space(5);
GUILayout.BeginHorizontal ("box");
GameObject[] count_object = Selection.gameObjects;
if (count_object.Length > 0)
{ //----------------------
mynum = 0.0f;
mynum1=0.0f;
mynum_tri = 0.0f;
unit_tri = 0.0f;
foreach (GameObject c in count_object)
{
if(c.GetComponent<MeshFilter>())
{
mynum = c.GetComponent<MeshFilter>().mesh.triangles.Length/3;
mynum1+= mynum/2;
mynum_tri+= c.GetComponent<MeshFilter>().mesh.triangles.Length/3;
unit_tri+= c.GetComponent<MeshFilter>().mesh.triangles.Length;
}
}
}
else
{
mynum = 0.0f;
mynum1=0.0f;
mynum_tri = 0.0f;
}
// GUILayout.Label( "Polygons " + mynum1);
GUILayout.Label( "3ds Max Tris " + mynum_tri);
GUILayout.Label( "Unity Tris " + unit_tri);
GUILayout.EndHorizontal ();
}
private static void FindInSelected()
{
GameObject[] go = Selection.gameObjects;
// int go_count = 0, /*components_count = 0,*/ missing_count = 0;
int go_count = 0,missing_count = 0;
foreach (GameObject g in go)
{
go_count++;
// Component[] components = g.GetComponents<Component>();
// for (int i = 0; i < components.Length; i++)
// {
// components_count++;
// if (components[i] == null)
// {
// missing_count++;
// Debug.Log(g.name + " has an empty script attached in position: " + i, g);
// }
// }
//
//---------------------------------------------------------------------------------------------
MeshRenderer[] meshRendcomponent = g.GetComponentsInChildren<MeshRenderer>();
foreach (MeshRenderer mr in meshRendcomponent) //
{
// Debug.Log("Found MeshRenderer " + mr.name);
foreach (Material m in mr.sharedMaterials)
{
// Debug.Log("Found material " + m.name);
// Texture2D tex = m.GetTexture("_MainTex") as Texture2D;
// Texture2D tex = m.GetTexture() as Texture2D;
if(m.mainTexture)
{
// Debug.Log("Texture name---- " + m.mainTexture);
}
else
{
// Debug.Log("Missing texture--: "+mr.name);
Debug.LogError("No texture found, GameObject:"+g.name + ", Mat Name is: " + m,g);
missing_count++;
}
// if (tex != null)
// {
// Debug.Log("Found texture " + tex.name);
// }
// else
// {
// Debug.Log("Missing diffuse texture");
// }
}
}
//
//---------------------------------------------------------------------------------------------
}
// Debug.Log(string.Format("Searched {0} GameObjects, {1} components, found {2} missing", go_count, components_count, missing_count));
Debug.Log(string.Format("Searched {0} GameObjects, found {1} missing", go_count,missing_count));
}
}
Just uncomment some code to work you way through thanks ArkaneX for support and thanks Frima$$anonymous$$ickD for answer thankyou guys cheers
Answer by FrimaMickD · Sep 04, 2013 at 01:37 PM
The triangles on the mesh represent the indices of the vertex used to build every triangles on your mesh (the polygons). So basically, dividing your triangles count by 3 will give you the number of triangles (poly) drawn.
Out of curiosity - how did you do it, that your answer which was created later than $$anonymous$$e, is positioned higher, without any upvotes? View is sorted by 'voted first'.
I don't know, when I was on the page your answer was not there, so maybe I got to the page first, did something else and than answered? Sry, was not my intention!
No problem - as I stated I asked out of curiosity only, because it's the first time I see such issue :)