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

 时间:2024-10-12 13:50:01

Unity Mesh教程 之 快速找到Mesh顶点并标记。本节介绍找到Mesh的顶点并且使用Gizmos的DrawSphere标记的简单案例,具体如下

工具/原料

Unity

Mesh

Gizmos

一、知识要点

1、Mesh:A class that allows creat坡纠课柩ing or modifying meshes from scripts.Meshes contain vertices and multiple triangle arrays. See theProcedural example projectfor examples of using the mesh interface.The triangle arrays are simply indices into the vertex arrays; three indices for each triangle.For every vertex there can be a normal, two texture coordinates, color and tangent. These are optional though and can be removed at will. All vertex information is stored in separate arrays of the same size, so if your mesh has 10 vertices, you would also have 10-size arrays for normals and other attributes.

2、OnDrawGizmos():1)功能描述Implement OnDrawGizmos if you want to draw gizmos that are also pickable and always drawn.This allows you to quickly pick important objects in your scene.Note that OnDrawGizmos will use a mouse position that is relative to the Scene View.This function doesnotget called if the component is collapsed in the inspector. UseOnDrawGizmosSelectedto draw gizmos when the game object is selected.2)使用举例using UnityEngine;using System.Collections;public class ExampleClass : MonoBehaviour { void OnDrawGizmos() { 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”,双击脚本或者右键“Open C# Project”打开,具体如下图

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

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

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

5、“MeshTest”脚本的短铘辔嗟具体内容如下:usingUnityEngine;publicclassMeshTest:MonoBehaviour{voidOnDrawGizmos(){ 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、到此《Unity Mesh教程 之 快速找到Mesh顶点并标记》讲解结束,谢谢

  • 艾尔登法环如何修改按键提示按键提示怎么改
  • 仁王2 烟炎之怪(上)任务攻略
  • 地平线5怎么获得超级抽奖
  • 皇家战争魔法外卡怎么兑换稀有卡牌
  • 黑暗之魂2道具收集及游戏要点:[9]罪人塔
  • 热门搜索
    旅游股 日本旅游购物清单 泰宁旅游 奥地利旅游 去泰国旅游要带什么 阳泉旅游 西沙旅游 关于旅游的英语作文 境外旅游保险 上海的旅游景点