Unity中自带的Plane的顶点个数是很多的,经过打印得出Plane的顶点个数为121个,对于要实现一些简单的放一个图片的Plane来说这有点浪费资源了,所以这些简单的只是显示一张图片的Plane可以自己创建出来,只使用4个顶点。
using System.Collections.Generic; using UnityEngine; public class CheckPlane : MonoBehaviour { void Start () { Mesh m = this.GetComponent<MeshFilter>().mesh; List<Vector3> list = new List<Vector3>(); m.GetVertices(list); //Unity自带的Plane的顶点个数为 121 Debug.LogError("顶点的个数为:" + list.Count); CreatPlane(); } void CreatPlane() { Mesh m = new Mesh(); GameObject go = new GameObject("Haha"); MeshFilter f = go.AddComponent<MeshFilter>(); f.mesh = m; Vector3[] array = new Vector3[4];//顶点坐标 Vector3[] array2 = new Vector3[4];//顶点面法线 Color[] array3 = new Color[4];//顶点颜色 Vector2[] array4 = new Vector2[4];//uv坐标 int[] array5 = new int[6];//三角型索引 array[0] = new Vector3(-1f, 1f, 0f);//顶点坐标 array[1] = new Vector3(1f, 1f, 0f); array[2] = new Vector3(-1f, -1f, 0f); array[3] = new Vector3(1f, -1f, 0f); array2[0] = new Vector3(0f, 1f, 0f);//顶点面法线 array2[1] = new Vector3(0f, 1f, 0f); array2[2] = new Vector3(0f, 1f, 0f); array2[3] = new Vector3(0f, 1f, 0f); array3[0] = new Color(1f, 1f, 1f);//顶点颜色 array3[1] = new Color(1f, 1f, 1f); array3[2] = new Color(1f, 1f, 1f); array3[3] = new Color(1f, 1f, 1f); array4[0] = new Vector2(0f, 0f);//uv坐标 array4[1] = new Vector2(1f, 0f); array4[2] = new Vector2(0f, 1f); array4[3] = new Vector2(1f, 1f); array5[0] = 0;//三角型索引 array5[1] = 1; array5[2] = 2; array5[3] = 2; array5[4] = 1; array5[5] = 3; m.vertices = array; m.normals = array2; m.colors = array3; m.uv = array4; m.triangles = array5; Shader shader = Shader.Find("Standard");//根据项目需要,使用Shader Material mat = new Material(shader); go.AddComponent<MeshRenderer>().material = mat; //打印下顶点个数 List<Vector3> list = new List<Vector3>(); m.GetVertices(list); Debug.LogError("自己创建的顶点个数:" + list.Count); } }