本文主要是介绍【UnityEditor】查看模型Mesh面数/顶点数,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
去掉GUI字体的三角面数,统计到的数据与Unity面板显示一致。
Unity默认天空盒有大约1.7k三角面,5k顶点,如果使用了天空盒,需要额外注意。
关键步骤:
- Selection.selectionChanged:编辑器下当前选中的Gameobject发生变化的回调
- Selection.gameObjects:编辑器下当前选中的Gameobject
- 获取所有网格数据:
- Gameobject.GetComponentsInChildren():获取自身节点及所有嵌套下属节点的MeshFilter组件
- Gameobject.GetComponentsInChildren():获取自身节点及所有嵌套下属节点的SkinnedMeshRenderer组件
- 统计网格数据:
- MeshFilter.sharedMesh.triangles.Length(MeshFilter三角面数)
- MeshFilter.sharedMesh.vertexCount(MeshFilter顶点数)
- SkinnedMeshRenderer.sharedMesh.triangles.Length(SkinnedMeshRenderer三角面数)
- SkinnedMeshRenderer.sharedMesh.vertexCount(SkinnedMeshRenderer顶点数)
示例统计代码:
#if UNITY_EDITORusing System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;public class CalculateMeshTris : MonoBehaviour
{[SerializeField] private bool active = true;private int verts;private int tris;private GUIStyle textStyle = new GUIStyle();private void Start(){textStyle.normal.background = null;textStyle.normal.textColor = new Color(1.0f,0.5f,0.0f);textStyle.fontSize = 30;if(active){Selection.selectionChanged += CalculateVertsAndTris;CalculateVertsAndTris();}}private void CalculateVertsAndTris(){GameObject[] objs = Selection.gameObjects;int verts1 = 0, tris1 = 0, verts2 = 0, tris2 = 0;foreach(GameObject obj in objs){MeshFilter[] filters = obj.GetComponentsInChildren<MeshFilter>();foreach(MeshFilter filter in filters){tris1 += filter.sharedMesh.triangles.Length;verts1 += filter.sharedMesh.vertexCount;}SkinnedMeshRenderer[] renders = obj.GetComponentsInChildren<SkinnedMeshRenderer>();foreach(SkinnedMeshRenderer render in renders){tris2 += render.sharedMesh.triangles.Length;verts2 += render.sharedMesh.vertexCount;}}tris1 /= 3;tris2 /= 3;verts = verts1 + verts2;tris = tris1 + tris2;Debug.Log(string.Format("verts: {0}+{1}={2}; tris: {3}+{4}={5}",verts1,verts2,verts,tris1,tris2,tris));}private void OnGUI(){if(active)GUILayout.Label(string.Format("verts: {0}; tris: {1}",verts,tris),textStyle);}
}#endif
这篇关于【UnityEditor】查看模型Mesh面数/顶点数的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!