Version: 2019.1

# 示例 - 创建四边形

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

``````var 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;
``````

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

``````var tris = new int[6]
{
// lower left triangle
0, 2, 1,
// upper right triangle
2, 3, 1
};
mesh.triangles = tris;
``````

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

``````var 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 class CreateBillboard : MonoBehaviour
{
public float width = 1;
public float height = 1;

public void Start()
{
var mf = GetComponent<MeshFilter>();

var mesh = new Mesh();
mf.mesh = mesh;

var 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;

var tris = new int[6]
{
// lower left triangle
0, 2, 1,
// upper right triangle
2, 3, 1
};
mesh.triangles = tris;

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

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