示例 – 创建一个广告牌平面

Unity 自带一个平面 (Plane) 原始对象，但是更简单的平面在二维游戏或 GUI 中可能非常有用，在任何情况下可以做一个好的开始示例。一个最小平面包含四个顶点，界定两个三角形的边角。

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

（由于网格 (Mesh) 数据属性在场景后面执行代码，在自己的数组中设置数据然后赋予属性，而不是逐一访问属性数组元素会更为有效。）

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

//	Lower left triangle.
tri[0] = 0;
tri[1] = 2;
tri[2] = 1;

//	Upper right triangle.
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;
}```

Page last updated: 2013-06-25