Unity 动态创建 Mesh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
using UnityEngine;
using System.Collections;

public class MeshTest : MonoBehaviour {

private MeshFilter m_MeshFilter;

void Start () {
Mesh mesh = new Mesh();

mesh.vertices = new Vector3[] {
new Vector3(0, 0, 0), // 顶点:0
new Vector3(1, 0, 0), // 顶点:1
new Vector3(1, 1, 0), // 顶点:2
new Vector3(0, 1, 0) // 顶点:3
};

// 遵循顺时针三顶点确定一面
mesh.triangles = new int[] {
0, 3, 1, // 面:0, 3, 1
1, 3, 2, // 面:1, 3, 2
};

mesh.uv = new Vector2[] {
new Vector2(0, 0), // 顶点0的纹理坐标
new Vector2(1, 0), // 顶点1的纹理坐标
new Vector2(1, 1), // 顶点2的纹理坐标
new Vector2(0, 1) // 顶点3的纹理坐标
};


m_MeshFilter = GetComponent<MeshFilter>();
m_MeshFilter.mesh = mesh;
}
}