Version: 2022.3

## 顶点数组

``````Vector3[] vertices = new Vector3[4]
{
new Vector3(0, 0, 0),
new Vector3(width, 0, 0),
new Vector3(0, height, 0),
new Vector3(width, height, 0)
};
mesh.vertices = vertices;
``````

## 三角形

``````int[] tris = new int[6]
{
// 左下方三角形
0, 2, 1,
// 右上方三角形
2, 3, 1
};
mesh.triangles = tris;
``````

## 法线

``````Vector3[] normals = new Vector3[4]
{
-Vector3.forward,
-Vector3.forward,
-Vector3.forward,
-Vector3.forward
};
mesh.normals = normals;
``````

If you do not want to define the normals yourself, you can use Mesh.RecalculateNormals().

## 纹理坐标

``````Vector2[] uv = new Vector2[4]
{
new Vector2(0, 0),
new Vector2(1, 0),
new Vector2(0, 1),
new Vector2(1, 1)
};
mesh.uv = uv;
``````

## 最终的脚本

``````using UnityEngine;

{
public float width = 1;
public float height = 1;

public void Start()
{

Mesh mesh = new Mesh();

Vector3[] vertices = new Vector3[4]
{
new Vector3(0, 0, 0),
new Vector3(width, 0, 0),
new Vector3(0, height, 0),
new Vector3(width, height, 0)
};
mesh.vertices = vertices;

int[] tris = new int[6]
{
// 左下方三角形
0, 2, 1,
// 右上方三角形
2, 3, 1
};
mesh.triangles = tris;

Vector3[] normals = new Vector3[4]
{
-Vector3.forward,
-Vector3.forward,
-Vector3.forward,
-Vector3.forward
};
mesh.normals = normals;

Vector2[] uv = new Vector2[4]
{
new Vector2(0, 0),
new Vector2(1, 0),
new Vector2(0, 1),
new Vector2(1, 1)
};
mesh.uv = uv;

meshFilter.mesh = mesh;
}
}

``````