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

2025-09-29 03:26:51

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顶点并标记》讲解结束,谢谢

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢