Created
April 12, 2023 23:39
-
-
Save kiosion/be51cb14b22e76e3920ea143b7aaf929 to your computer and use it in GitHub Desktop.
Simple script for displaying mesh info for selection(s) containing MeshFilters / SkinnedMeshRenderers in Unity Editor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEditor; | |
public class MeshInfo : EditorWindow { | |
private int vertexCount; | |
private int submeshCount; | |
private int totalTriangleCount; | |
private int triangleCount; | |
private int uvChannels; | |
private int blendShapesCount; | |
[MenuItem ("Tools/Mesh Info")] | |
static void Init () { | |
// Get existing open window or if none, make a new one: | |
MeshInfo window = (MeshInfo) EditorWindow.GetWindow (typeof (MeshInfo)); | |
window.titleContent.text = "Mesh Info"; | |
} | |
void OnEnable() { | |
Selection.selectionChanged += OnSelectionChange; | |
} | |
void OnDisable() { | |
Selection.selectionChanged -= OnSelectionChange; | |
} | |
void OnSelectionChange() { | |
Repaint(); | |
} | |
int CountTrisInScene() { | |
int totalTris = 0; | |
MeshFilter[] meshFilters = Resources.FindObjectsOfTypeAll<MeshFilter>(); | |
SkinnedMeshRenderer[] skinnedMeshRenderers = Resources.FindObjectsOfTypeAll<SkinnedMeshRenderer>(); | |
foreach (MeshFilter mf in meshFilters) { | |
if (mf.sharedMesh != null) { | |
totalTris += mf.sharedMesh.triangles.Length / 3; | |
} | |
} | |
foreach (SkinnedMeshRenderer smr in skinnedMeshRenderers) { | |
if (smr.sharedMesh != null) { | |
totalTris += smr.sharedMesh.triangles.Length / 3; | |
} | |
} | |
return totalTris; | |
} | |
void OnGUI() { | |
vertexCount = 0; | |
triangleCount = 0; | |
totalTriangleCount = CountTrisInScene(); | |
submeshCount = 0; | |
uvChannels = 0; | |
blendShapesCount = 0; | |
foreach (GameObject g in Selection.gameObjects) { | |
Mesh sharedMesh = null; | |
// We want to check both MeshFilters and SkinnedMeshRenderers | |
MeshFilter meshFilter = g.GetComponent<MeshFilter>(); | |
SkinnedMeshRenderer skinnedMeshRenderer = g.GetComponent<SkinnedMeshRenderer>(); | |
if (meshFilter != null && meshFilter.sharedMesh != null) { | |
sharedMesh = meshFilter.sharedMesh; | |
} else if (skinnedMeshRenderer != null && skinnedMeshRenderer.sharedMesh != null) { | |
sharedMesh = skinnedMeshRenderer.sharedMesh; | |
} | |
if (sharedMesh != null) { | |
vertexCount += sharedMesh.vertexCount; | |
triangleCount += sharedMesh.triangles.Length / 3; | |
submeshCount += sharedMesh.subMeshCount; | |
uvChannels = sharedMesh.uv.Length > 0 ? 1 : 0; | |
blendShapesCount += sharedMesh.blendShapeCount; | |
} | |
} | |
EditorGUILayout.LabelField("Vertices: ", vertexCount.ToString()); | |
EditorGUILayout.LabelField("Polygons: ", triangleCount.ToString() + " (" + totalTriangleCount.ToString() + " in scene)"); | |
EditorGUILayout.LabelField("SubMeshes: ", submeshCount.ToString()); | |
EditorGUILayout.LabelField("UV Channels: ", uvChannels.ToString()); | |
EditorGUILayout.LabelField("Blend Shapes: ", blendShapesCount.ToString()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment