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

 时间:2026-02-15 18:12:46

1、Mesh:

A class that allows creating or modifying meshes from scripts.

Meshes contain vertices and multiple triangle arrays. See the Procedural example project for 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 does not get called if the component is collapsed in the inspector. Use OnDrawGizmosSelected to 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() 函数把局部坐标转为世界坐标

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”脚本的具体内容如下:

using UnityEngine;

public class MeshTest : MonoBehaviour {

    void OnDrawGizmos() {

                MeshFilter targetFilter = GetComponent <MeshFilter> ();

        Mesh mh = targetFilter.mesh;

        Gizmos.color = Color.green;

        for (int i = 0; i < mh.vertices.Length; i++) {

            Vector3 targetPosition = 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]罪人塔
  • 热门搜索
    友谊是什么仿写句子 山炮是什么意思 fob是什么意思 引经据典是什么意思 住宿要带什么 什么是非主流 养生堂维生素c 移动端是什么意思 头盔什么牌子质量好 日字旁的有什么字