UnityMesh教程之快速找到Mesh顶点并标记。本节介绍找到Mesh的顶点并且使用Gizmos的DrawSphere标记的简单案例,具体如下
工具/原料
Unity
Mesh
Gizmos
一、知识要点
1、Mesh:Aclassthatallowscreat坡纠课柩ingormodifyingmeshesfrom惺绅寨瞀scripts.Meshescontainverticesandmultipletrianglearrays.SeetheProceduralexampleprojectforexamplesofusingthemeshinterface.Thetrianglearraysaresimplyindicesintothevertexarrays;threeindicesforeachtriangle.Foreveryvertextherecanbeanormal,twotexturecoordinates,colorandtangent.Theseareoptionalthoughandcanberemovedatwill.Allvertexinformationisstoredinseparatearraysofthesamesize,soifyourmeshhas10vertices,youwouldalsohave10-sizearraysfornormalsandotherattributes.
2、OnDrawGizmos():1)功能描述ImplementO艘绒庳焰nDrawGizmosifyouwanttodraw惺绅寨瞀gizmosthatarealsopickableandalwaysdrawn.Thisallowsyoutoquicklypickimportantobjectsinyourscene.NotethatOnDrawGizmoswilluseamousepositionthatisrelativetotheSceneView.Thisfunctiondoesnotgetcalledifthecomponentiscollapsedintheinspector.UseOnDrawGizmosSelectedtodrawgizmoswhenthegameobjectisselected.2)使用举例usingUnityEngine;usingSystem.Collections;publicclassExampleClass:MonoBehaviour{voidOnDrawGizmos(){Gizmos.color=Color.yellow;Gizmos.DrawSphere(transform.position,1);}}
3、方法提示:1)使用Gizmos画球标记2)使用TransformPoint()函数把局部坐标转为世界坐标
二、Mesh教程之快速找到Mesh顶点并标记
1、打开Unity,新建一个空工程,具体如下
2、在场景中,新建一个“Quad”,调整好观察角度,具体如下
3、在工程中新建一个脚本“MeshTest”,双击脚本或者右键“OpenC#Project”打开,具体如下图
4、在打开的脚本“MeshTest”上编写代码,首先获取“MeshFilter”组件,勾袖稣拗然后获取组件上的Mesh,接着把Mseh上所有的顶点转为世界坐标,最后Gizmos.Draw胨检馁秣Sphere标记,具体代码及代码说明如下图
5、“MeshTest”脚本的短铘辔嗟具体内容如下:usingUnityEngine;publicclassMeshTest:MonoBehaviour{v泠贾高框oidOnDrawGizmos(){MeshFiltertargetFilter=GetComponent<MeshFilter>();Meshmh=targetFilter.mesh;Gizmos.color=Color.green;for(inti=0;i<mh.vertices.Length;i++){Vector3targetPosition=transform.TransformPoint(mh.vertices[i]);Gizmos.DrawSphere(targetPosition,0.1f);}}}
6、脚本编译正确,回到Unity界面,把脚本赋给场景中的“Quad”,具体如下图
7、随后,在游戏视图中,“Quad”的四个顶点用“绿球”标记了,具体如下图
8、到此《UnityMesh教程之快速找到Mesh顶点并标记》讲解结束,谢谢