Unity Mesh教程 之 快速找到Mesh顶点并标记

 时间:2024-10-28 03:28:31

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,新建一个空工程,具体如下

Unity Mesh教程 之 快速找到Mesh顶点并标记

2、在场景中,新建一个“Quad”,调整好观察角度,具体如下

Unity Mesh教程 之 快速找到Mesh顶点并标记

3、在工程中新建一个脚本“MeshTest”,双击脚本或者右键“OpenC#Project”打开,具体如下图

Unity Mesh教程 之 快速找到Mesh顶点并标记

Unity Mesh教程 之 快速找到Mesh顶点并标记

4、在打开的脚本“MeshTest”上编写代码,首先获取“MeshFilter”组件,勾袖稣拗然后获取组件上的Mesh,接着把Mseh上所有的顶点转为世界坐标,最后Gizmos.Draw胨检馁秣Sphere标记,具体代码及代码说明如下图

Unity Mesh教程 之 快速找到Mesh顶点并标记

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”,具体如下图

Unity Mesh教程 之 快速找到Mesh顶点并标记

7、随后,在游戏视图中,“Quad”的四个顶点用“绿球”标记了,具体如下图

Unity Mesh教程 之 快速找到Mesh顶点并标记

8、到此《UnityMesh教程之快速找到Mesh顶点并标记》讲解结束,谢谢

艾尔登法环如何修改按键提示按键提示怎么改 仁王2 烟炎之怪(上)任务攻略 地平线5怎么获得超级抽奖 皇家战争魔法外卡怎么兑换稀有卡牌 黑暗之魂2道具收集及游戏要点:[9]罪人塔
热门搜索
棒球棒图片 邓婕图片 巴巴电影 图片笑话 周泓图片