MeshInfo

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(Usage)
m (Text replace - "</csharp>" to "</syntaxhighlight>")
 
(5 intermediate revisions by 4 users not shown)
Line 13: Line 13:
  
 
==C# - MeshInfo.cs==
 
==C# - MeshInfo.cs==
<csharp>using UnityEngine;
+
<syntaxhighlight lang="csharp">using UnityEngine;
 
using UnityEditor;
 
using UnityEditor;
 
using System.Collections;
 
using System.Collections;
Line 28: Line 28:
 
         foreach (GameObject go in Selection.GetFiltered(typeof(GameObject), SelectionMode.TopLevel))
 
         foreach (GameObject go in Selection.GetFiltered(typeof(GameObject), SelectionMode.TopLevel))
 
         {
 
         {
             Component[] meshes = go.GetComponentsInChildren(typeof(MeshFilter));
+
             Component[] skinnedMeshes = go.GetComponentsInChildren(typeof(SkinnedMeshRenderer)) ;
 +
            Component[] meshFilters = go.GetComponentsInChildren(typeof(MeshFilter));
  
             foreach (MeshFilter mesh in meshes)
+
             ArrayList totalMeshes = new ArrayList(meshFilters.Length + skinnedMeshes.Length);
 +
 
 +
            for (int meshFiltersIndex = 0; meshFiltersIndex < meshFilters.Length; meshFiltersIndex++)
 +
            {
 +
                MeshFilter meshFilter = (MeshFilter)meshFilters[meshFiltersIndex];
 +
                totalMeshes.Add(meshFilter.sharedMesh);
 +
            }
 +
 
 +
            for (int skinnedMeshIndex = 0; skinnedMeshIndex < skinnedMeshes.Length; skinnedMeshIndex++)
 +
            {
 +
                SkinnedMeshRenderer skinnedMeshRenderer = (SkinnedMeshRenderer)skinnedMeshes[skinnedMeshIndex];
 +
                totalMeshes.Add(skinnedMeshRenderer.sharedMesh);
 +
            }
 +
 
 +
            for (int i = 0; i < totalMeshes.Count; i++)
 
             {
 
             {
                 if (mesh.sharedMesh)
+
                Mesh mesh = totalMeshes[i] as Mesh;
 +
                 if (mesh == null)
 
                 {
 
                 {
                     vertices += mesh.sharedMesh.vertexCount;
+
                     Debug.LogWarning("You have a missing mesh in your scene.");
                    triangles += mesh.sharedMesh.triangles.Length / 3;
+
                     continue;
                     meshCount++;
+
 
                 }
 
                 }
 +
                vertices += mesh.vertexCount;
 +
                triangles += mesh.triangles.Length / 3;
 +
                meshCount++;
 
             }
 
             }
 
         }
 
         }
Line 54: Line 72:
 
      
 
      
 
}
 
}
</csharp>
+
</syntaxhighlight>

Latest revision as of 20:45, 10 January 2012

Author: Jonathan Czeck (aarku)

[edit] Description

This script will show statistics of the meshes in the current selection. If a parent object is selected, the script will show statistics of the parent's entire hierarchy.

[edit] Usage

Place this script in YourProject/Assets/Editor/MeshInfo.cs and a menu item will automatically appear in the Custom menu after it is compiled. Then select any number of objects and select the menu item to see triangle and vertex info.

[edit] C# - MeshInfo.cs

using UnityEngine;
using UnityEditor;
using System.Collections;
 
public class MeshInfo : ScriptableObject
{   
    [MenuItem ("Custom/Show Mesh Info %#i")]
    public static void ShowCount()
    {
        int triangles = 0;
        int vertices = 0;
        int meshCount = 0;
 
        foreach (GameObject go in Selection.GetFiltered(typeof(GameObject), SelectionMode.TopLevel))
        {
            Component[] skinnedMeshes = go.GetComponentsInChildren(typeof(SkinnedMeshRenderer)) ;
            Component[] meshFilters = go.GetComponentsInChildren(typeof(MeshFilter));
 
            ArrayList totalMeshes = new ArrayList(meshFilters.Length + skinnedMeshes.Length);
 
            for (int meshFiltersIndex = 0; meshFiltersIndex < meshFilters.Length; meshFiltersIndex++)
            {
                MeshFilter meshFilter = (MeshFilter)meshFilters[meshFiltersIndex];
                totalMeshes.Add(meshFilter.sharedMesh);
            }
 
            for (int skinnedMeshIndex = 0; skinnedMeshIndex < skinnedMeshes.Length; skinnedMeshIndex++)
            {
                SkinnedMeshRenderer skinnedMeshRenderer = (SkinnedMeshRenderer)skinnedMeshes[skinnedMeshIndex];
                totalMeshes.Add(skinnedMeshRenderer.sharedMesh);
            }
 
            for (int i = 0; i < totalMeshes.Count; i++)
            {
                Mesh mesh = totalMeshes[i] as Mesh;
                if (mesh == null)
                {
                    Debug.LogWarning("You have a missing mesh in your scene.");
                    continue;
                }
                vertices += mesh.vertexCount;
                triangles += mesh.triangles.Length / 3;
                meshCount++;
            }
        }
 
        EditorUtility.DisplayDialog("Vertex and Triangle Count", vertices
            + " vertices in selection.  " + triangles + " triangles in selection.  "
            + meshCount + " meshes in selection." + (meshCount > 0 ? ("  Average of " + vertices / meshCount
            + " vertices and " + triangles / meshCount + " triangles per mesh.") : ""), "OK", "");
    }
 
    [MenuItem ("Custom/Show Mesh Info %i", true)]
    public static bool ValidateShowCount()
    {
        return Selection.activeGameObject;
    }
 
}
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox