Unity Mesh教程 之 使用Mesh画一个三角形。本节介绍如何使用Mesh画一个三角形出来的简单案例,具体如下
工具/原料
Unity
Mesh
一、知识要点
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、MeshFilter:class in UnityEngine Inherits from:ComponentA class to access theMeshof themesh filter.Use this with a procedural mesh interface. See Also:Meshclass.
3、MeshRenderer:Inherits from:RendererRenders meshes inserted by theMeshFilterorTextMesh.
4、方法提示:1)要求必须添加“MeshFilter”和“MeshRender”组件2)把三角形值赋给“MeshFilter.mesh”
二、Mesh教程 之 使用Mesh画一个三角形
1、打开Unity,新建一个空工程,具体如下


4、“MeshTriangle哌囿亡噱Test”脚本的具体代码如下:usingSystem.Collections.Generic;usingUnityEngine;[Require潮贾篡绐Component(typeof(MeshFilter))][RequireComponent(typeof(MeshRenderer))]publicclassMeshTriangleTest:MonoBehaviour{privateList<Vector3>points=newList<Vector3>();//Usethisforinitialization voidStart(){ points.Add(newVector3(0,0,0)); points.Add(newVector3(0,1,0)); points.Add(newVector3(1,0,0));MeshDrawTriangle(); }privatevoidMeshDrawTriangle(){//新建一个Mesh MeshtriangleMesh=newMesh();//把列表的顶点坐标赋给Mesh的vertexs triangleMesh.vertices=points.ToArray();//设置三角形顶点数量 int[]trianglePoints=newint[3]; trianglePoints[0]=0; trianglePoints[1]=1; trianglePoints[2]=2;//把三角形的数量给Mesh的三角形 triangleMesh.triangles=trianglePoints;//设置三角形的相关参数 triangleMesh.RecalculateBounds(); triangleMesh.RecalculateNormals(); triangleMesh.RecalculateTangents();//把三角形的Mesh赋给MeshFilter组件 GetComponent<MeshFilter>().mesh=triangleMesh; }}
5、脚本编译正确,回到Unity界面,在场景中新建一个“GameObject”,把脚本“MeshTriangleTest”赋给“GameObject”,你会看到自动加上组件“MeshFilter”和“MeshRender”,具体如下图

7、到此,《Unity Mesh教程 之 使用Mesh画一个三角形》讲解结束,谢谢