Get tris, verts in code

Hi all, does anyone know how to get these value in sourcecode?
I want to show how much tris, verts that were drawed in screen

1178397--45354--$getpolygon.jpg

1 Like

get all the game objects in a scene, and add up their mesh.vertices and mesh. triangles…

That would give him the scene polycount. He wants the number of rendered polygons. Also, not all gameobjects have meshes. He has to get all the MeshFilter components in that scenario.

Create a script which adds the tri count to a static variable that holds the current polycount in OnBecameVisible() method and subtracts the tri count on OnBecameInvisible() method.

You’re right, I just want to get tri count that was drawn on screen.
Your suggestion also good but cannot do for my case, because I also use particle.
Is there any other suggestion?

This is a quite general script i found somewhere on the net once, it should count all the verts and tris of the activated objects in the scene and also display fps.
in C#:

using UnityEngine;
using System.Collections;

public class Statistics : MonoBehaviour {
	public bool Show_Stats;
	public bool Show_FPS;
	public bool Show_Tris;
	public bool Show_Verts;
	public static int verts;
	public static int tris;
	
	public  float updateInterval = 0.5F;
 
	private float accum   = 0; // FPS accumulated over the interval
	private int   frames  = 0; // Frames drawn over the interval
	private float timeleft; // Left time for current interval
	public float fps;

	// Use this for initialization
	void Start () {
		timeleft = updateInterval;
	}
	
	// Update is called once per frame
	void Update () {
		/*Show_Stats = GameObject.Find("Player").GetComponent<Pause_Menu>().Show_Stats;
		Show_FPS = GameObject.Find("Player").GetComponent<Pause_Menu>().Show_FPS;
		Show_Tris = GameObject.Find("Player").GetComponent<Pause_Menu>().Show_Tris;
		Show_Verts = GameObject.Find("Player").GetComponent<Pause_Menu>().Show_Verts;*/
		timeleft -= Time.deltaTime;
    	accum += Time.timeScale/Time.deltaTime;
    	++frames;
		
		if( timeleft <= 0.0 )
		{
        	// display two fractional digits (f2 format)
			fps = accum/frames;
			string format = System.String.Format("{0:F2} FPS",fps);
			//	DebugConsole.Log(format,level);
        		timeleft = updateInterval;
       		 accum = 0.0F;
       		 frames = 0;
			GetObjectStats();
    	}	
	}
	
	void OnGUI() {
		if(Show_Stats)
		ShowStatistics();
	}
	
	void ShowStatistics(){
		GUILayout.BeginArea( new Rect(0, 0, 100, 100));
		if (Show_FPS){
			string fpsdisplay = fps.ToString ("#,##0 fps");
			GUILayout.Label(fpsdisplay);
		}
		if (Show_Tris){
			string trisdisplay = tris.ToString ("#,##0 tris");
			GUILayout.Label(trisdisplay);
		}
		if (Show_Verts){
			string vertsdisplay = verts.ToString ("#,##0 verts");
			GUILayout.Label(vertsdisplay);
		}
		GUILayout.EndArea();
	}

	void GetObjectStats() {
	    verts = 0;
	    tris = 0;
	    GameObject[] ob = FindObjectsOfType(typeof(GameObject)) as GameObject[];
	    foreach (GameObject obj in ob) {
	        GetObjectStats(obj);
	    }
	}
 
	void GetObjectStats(GameObject obj) {
	   	Component[] filters;
	    filters = obj.GetComponentsInChildren<MeshFilter>();
	    foreach( MeshFilter f  in filters )
	    {
	        tris += f.sharedMesh.triangles.Length/3;
	      	verts += f.sharedMesh.vertexCount;
	    }
	}
}

I usually attach this script (Statistics.cs) to my player object,
hope it will lend you a hand :slight_smile:

There’s a piece of code commented out that comes from my pause menu, to prevent errors when copy/pasting and to give a hint to how i normally have this set-up

2 Likes

Thanks but your code just calculate active object not same as statistic of Unity3D. I want to get value same as Unity3D’s statistics

1 Like

Yeah, this script does not seem to take into account things like occlussion and particles. which is too bad in my own case too :frowning:
If you happen to find a solution, please let us know!

Looks like no?

Any answers? ‘Thanks but your code just calculate active object not same as statistic of Unity3D. I want to get value same as Unity3D’s statistics’ No one seems to know :slight_smile:

UnityEditor.UnityStats.triangles
UnityEditor.UnityStats.vertices
3 Likes

You are my hero of the day!

1 Like