Version: 5.6

# 示例 - 创建四边形

Unity 随附__平面__和__四边形__原始对象来代表平面（请参阅原始对象页面以了解更多详细信息）。然而，检查最小四边形网格的构造方式通常很有用，因为这可能是最简单的有用示例，只有四个顶点用于角和两个三角形。

``````var vertices: Vector3[] = new Vector3[4];

vertices[0] = new Vector3(0, 0, 0);
vertices[1] = new Vector3(width, 0, 0);
vertices[2] = new Vector3(0, height, 0);
vertices[3] = new Vector3(width, height, 0);

mesh.vertices = vertices;

``````

（由于网格数据属性在后台执行代码，因此一种高效得多的做法是在您自己的数组中设置数据，然后将其分配给属性，而不是逐个元素访问属性数组。）

``````var tri: int[] = new int[6];

//  左下方三角形。
tri[0] = 0;
tri[1] = 2;
tri[2] = 1;

//  右上方三角形。
tri[3] = 2;
tri[4] = 3;
tri[5] = 1;

mesh.triangles = tri;

``````

``````var normals: Vector3[] = new Vector3[4];

normals[0] = -Vector3.forward;
normals[1] = -Vector3.forward;
normals[2] = -Vector3.forward;
normals[3] = -Vector3.forward;

mesh.normals = normals;

``````

``````var uv: Vector2[] = new Vector2[4];

uv[0] = new Vector2(0, 0);
uv[1] = new Vector2(1, 0);
uv[2] = new Vector2(0, 1);
uv[3] = new Vector2(1, 1);

mesh.uv = uv;

``````

``````var width: float;
var height: float;

function Start() {
var mf: MeshFilter = GetComponent.<MeshFilter>();
var mesh = new Mesh();
mf.mesh = mesh;

var vertices: Vector3[] = new Vector3[4];

vertices[0] = new Vector3(0, 0, 0);
vertices[1] = new Vector3(width, 0, 0);
vertices[2] = new Vector3(0, height, 0);
vertices[3] = new Vector3(width, height, 0);

mesh.vertices = vertices;

var tri: int[] = new int[6];

tri[0] = 0;
tri[1] = 2;
tri[2] = 1;

tri[3] = 2;
tri[4] = 3;
tri[5] = 1;

mesh.triangles = tri;

var normals: Vector3[] = new Vector3[4];

normals[0] = -Vector3.forward;
normals[1] = -Vector3.forward;
normals[2] = -Vector3.forward;
normals[3] = -Vector3.forward;

mesh.normals = normals;

var uv: Vector2[] = new Vector2[4];

uv[0] = new Vector2(0, 0);
uv[1] = new Vector2(1, 0);
uv[2] = new Vector2(0, 1);
uv[3] = new Vector2(1, 1);

mesh.uv = uv;
}

``````