Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Get tris, verts in code

Discussion in 'Scripting' started by Batigol, Mar 1, 2013.

  1. Batigol

    Batigol

    Joined:
    Oct 17, 2012
    Posts:
    234
    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
     

    Attached Files:

    ocimum likes this.
  2. kablammyman

    kablammyman

    Joined:
    Nov 22, 2010
    Posts:
    507
    get all the game objects in a scene, and add up their mesh.vertices and mesh. triangles...
     
  3. SarperS

    SarperS

    Joined:
    Mar 10, 2009
    Posts:
    824
    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.
     
  4. Batigol

    Batigol

    Joined:
    Oct 17, 2012
    Posts:
    234
    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?
     
  5. Annihlator

    Annihlator

    Joined:
    Oct 15, 2012
    Posts:
    378
    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#:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Statistics : MonoBehaviour {
    6.     public bool Show_Stats;
    7.     public bool Show_FPS;
    8.     public bool Show_Tris;
    9.     public bool Show_Verts;
    10.     public static int verts;
    11.     public static int tris;
    12.    
    13.     public  float updateInterval = 0.5F;
    14.  
    15.     private float accum   = 0; // FPS accumulated over the interval
    16.     private int   frames  = 0; // Frames drawn over the interval
    17.     private float timeleft; // Left time for current interval
    18.     public float fps;
    19.  
    20.     // Use this for initialization
    21.     void Start () {
    22.         timeleft = updateInterval;
    23.     }
    24.    
    25.     // Update is called once per frame
    26.     void Update () {
    27.         /*Show_Stats = GameObject.Find("Player").GetComponent<Pause_Menu>().Show_Stats;
    28.         Show_FPS = GameObject.Find("Player").GetComponent<Pause_Menu>().Show_FPS;
    29.         Show_Tris = GameObject.Find("Player").GetComponent<Pause_Menu>().Show_Tris;
    30.         Show_Verts = GameObject.Find("Player").GetComponent<Pause_Menu>().Show_Verts;*/
    31.         timeleft -= Time.deltaTime;
    32.         accum += Time.timeScale/Time.deltaTime;
    33.         ++frames;
    34.        
    35.         if( timeleft <= 0.0 )
    36.         {
    37.             // display two fractional digits (f2 format)
    38.             fps = accum/frames;
    39.             string format = System.String.Format("{0:F2} FPS",fps);
    40.             //  DebugConsole.Log(format,level);
    41.                 timeleft = updateInterval;
    42.              accum = 0.0F;
    43.              frames = 0;
    44.             GetObjectStats();
    45.         }  
    46.     }
    47.    
    48.     void OnGUI() {
    49.         if(Show_Stats)
    50.         ShowStatistics();
    51.     }
    52.    
    53.     void ShowStatistics(){
    54.         GUILayout.BeginArea( new Rect(0, 0, 100, 100));
    55.         if (Show_FPS){
    56.             string fpsdisplay = fps.ToString ("#,##0 fps");
    57.             GUILayout.Label(fpsdisplay);
    58.         }
    59.         if (Show_Tris){
    60.             string trisdisplay = tris.ToString ("#,##0 tris");
    61.             GUILayout.Label(trisdisplay);
    62.         }
    63.         if (Show_Verts){
    64.             string vertsdisplay = verts.ToString ("#,##0 verts");
    65.             GUILayout.Label(vertsdisplay);
    66.         }
    67.         GUILayout.EndArea();
    68.     }
    69.  
    70.     void GetObjectStats() {
    71.         verts = 0;
    72.         tris = 0;
    73.         GameObject[] ob = FindObjectsOfType(typeof(GameObject)) as GameObject[];
    74.         foreach (GameObject obj in ob) {
    75.             GetObjectStats(obj);
    76.         }
    77.     }
    78.  
    79.     void GetObjectStats(GameObject obj) {
    80.         Component[] filters;
    81.         filters = obj.GetComponentsInChildren<MeshFilter>();
    82.         foreach( MeshFilter f  in filters )
    83.         {
    84.             tris += f.sharedMesh.triangles.Length/3;
    85.             verts += f.sharedMesh.vertexCount;
    86.         }
    87.     }
    88. }
    89.  
    I usually attach this script (Statistics.cs) to my player object,
    hope it will lend you a hand :)

    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
     
    Last edited: Mar 1, 2013
    JosephSolar and Marcos-Elias like this.
  6. Batigol

    Batigol

    Joined:
    Oct 17, 2012
    Posts:
    234
    Thanks but your code just calculate active object not same as statistic of Unity3D. I want to get value same as Unity3D's statistics
     
    deus0 likes this.
  7. Annihlator

    Annihlator

    Joined:
    Oct 15, 2012
    Posts:
    378
    Yeah, this script does not seem to take into account things like occlussion and particles. which is too bad in my own case too :(
    If you happen to find a solution, please let us know!
     
  8. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
  9. deus0

    deus0

    Joined:
    May 12, 2015
    Posts:
    256
    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 :)
     
  10. whatahell

    whatahell

    Joined:
    Apr 26, 2021
    Posts:
    3
    Code (CSharp):
    1. UnityEditor.UnityStats.triangles
    2. UnityEditor.UnityStats.vertices
     
    alemnunez, deus0 and OleksandrMartysh like this.
  11. OleksandrMartysh

    OleksandrMartysh

    Joined:
    Dec 13, 2015
    Posts:
    25
    You are my hero of the day!
     
    deus0 likes this.