原创内容,转载请注明原文网址:http://homeqin.cn/a/wenzhangboke/jishutiandi/youxikaifa/2018/1012/70.html
在游戏开发过程中不免会遇到自己动态生成Mesh的操作,下面我们常州游戏开发设计培训工作室幻天网络的代码中在运行之后会动态的生成一个面片,本例中只设置了顶点、顶点颜色、uv以及三角形顺序。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GenerateMesh : MonoBehaviour {
static Color color = new Color(255, 0, 0, 255);
Vector3[] vertices = new Vector3[4];
Color[] colors = { color, color, color, color };
Vector2[] uvs = { new Vector2(0, 0), new Vector2(0, 1), new Vector2(1, 1), new Vector2(1, 0) };
int[] tris = { 0, 1, 2, 0, 2, 3 };
void Start ()
{
GameObject gameObject = new GameObject("Quad");
gameObject.transform.SetParent(transform, false);
var mf = gameObject.AddComponent<MeshFilter>();
var mr = gameObject.AddComponent<MeshRenderer>();
vertices[0] = new Vector3(0, 0, 0);
vertices[1] = new Vector3(0, 1, 0);
vertices[2] = new Vector3(1, 1, 0);
vertices[3] = new Vector3(1, 0, 0);
Material mat = new Material(Shader.Find("Custom/SurfaceShader"));
Mesh mesh = new Mesh();
mesh.vertices = vertices;
mesh.colors = colors;
mesh.uv = uvs;
mesh.triangles = tris;
mf.mesh = mesh;
mr.material = mat;
}
}
值得注意的一点是顶点坐标是基于本地坐标去设置的,并不是世界坐标
上篇:上一篇:C#获取程序运行时间
下篇:下一篇:Unity读取txt文本内容的方法




