Skip to content

Instantly share code, notes, and snippets.

@tamask
Created January 15, 2019 12:34
Show Gist options
  • Save tamask/495e1e947e0917e8afb2aae36a191016 to your computer and use it in GitHub Desktop.
Save tamask/495e1e947e0917e8afb2aae36a191016 to your computer and use it in GitHub Desktop.
Unity: display mesh info for selected object
using UnityEditor;
using UnityEngine;
[CustomEditor(typeof(MeshFilter))]
public class MeshInfo : Editor {
protected MeshFilter meshFilter;
protected Mesh mesh;
protected Vector3[] lines;
public void OnEnable() {
meshFilter = target as MeshFilter;
if (meshFilter != null)
{
mesh = meshFilter.sharedMesh;
Recalculate();
}
else
{
mesh = null;
lines = new Vector3[0];
}
}
public void OnSceneGUI() {
if (mesh == null) {
return;
}
if (MeshInfoSettings.changed)
{
Recalculate();
MeshInfoSettings.changed = false;
}
if (MeshInfoSettings.showMeshDebugInfo)
{
Transform transform = meshFilter.transform;
Handles.zTest = UnityEngine.Rendering.CompareFunction.LessEqual;
Handles.matrix = meshFilter.transform.localToWorldMatrix;
Handles.color = Color.yellow;
Handles.DrawLines(lines);
string info = "";
info += "Indices:\t" + mesh.GetIndexCount(0) + "\n";
info += "Vertices:\t" + mesh.vertexCount + "\n";
info += "Triangles:\t" + mesh.triangles.Length + "\n";
info += "Normals:\t" + mesh.normals.Length;
GUIStyle style = new GUIStyle();
style.padding = new RectOffset(8, 0, 8, 0);
style.normal.textColor = Color.black;
style.normal.background = Texture2D.whiteTexture;
Handles.Label(transform.position, info, style);
}
}
protected void Recalculate() {
if (mesh != null)
{
lines = new Vector3[mesh.vertexCount * 2];
for (int i = 0; i < mesh.vertexCount; i++)
{
lines[i * 2] = mesh.vertices[i];
lines[i * 2 + 1] = mesh.vertices[i] + mesh.normals[i] * MeshInfoSettings.vertexNormalLength;
}
}
}
}
public class MeshInfoSettings : EditorWindow
{
static public bool changed;
static public bool showMeshDebugInfo;
static public float vertexNormalLength = 0.1f;
[MenuItem("GameObject/Mesh Info")]
static void InitWindow()
{
EditorWindow.GetWindow(typeof(MeshInfoSettings));
}
public void OnEnable()
{
titleContent = new GUIContent("Mesh Info");
}
public void OnGUI() {
EditorGUILayout.Space();
showMeshDebugInfo = EditorGUILayout.Toggle("Show Mesh Debug Info", showMeshDebugInfo);
if (vertexNormalLength != (vertexNormalLength = EditorGUILayout.FloatField("Vertex Normal Length", vertexNormalLength)))
changed = true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment