using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class TheCube : MonoBehaviour
{
public MeshFilter meshFilter; //网格过滤器
public MeshRenderer meshRenderer;//网格渲染器
public BoxCollider box;
public Texture texture; //纹理
void Start()
{
meshFilter = gameObject.AddComponent<MeshFilter>();
meshRenderer = gameObject.AddComponent<MeshRenderer>();
box = gameObject.AddComponent<BoxCollider>();
Mesh mesh = new Mesh();//网格组件
VertexHelper vh = new VertexHelper();//顶点助手
//正方形的八个顶点
vh.AddVert(new Vector3(0,0,0),Color.white,Vector2.zero);
vh.AddVert(new Vector3(0,1,0),Color.white,Vector2.up);
vh.AddVert(new Vector3(1,0,0),Color.white,Vector2.right);
vh.AddVert(new Vector3(1,1,0),Color.white,Vector2.one);
vh.AddVert(new Vector3(0, 0, 1), Color.white, Vector2.zero);
vh.AddVert(new Vector3(0, 1, 1), Color.white, Vector2.up);
vh.AddVert(new Vector3(1, 0, 1), Color.white, Vector2.right);
vh.AddVert(new Vector3(1, 1, 1), Color.white, Vector2.one);
vh.AddTriangle(0,1,2);
vh.AddTriangle(1,3,2);
vh.AddTriangle(2,3,6);
vh.AddTriangle(3,7,6);
vh.AddTriangle(6,5,4);
vh.AddTriangle(6,7,5);
vh.AddTriangle(4,1,0);
vh.AddTriangle(4,5,1);
vh.AddTriangle(2,4,0);
vh.AddTriangle(6,4,2);
vh.AddTriangle(1,5,3);
vh.AddTriangle(5,7,3);
vh.FillMesh(mesh);//填充网格
mesh.RecalculateNormals();//重新绘制法线
meshFilter.mesh = mesh;//赋值给网格过滤器
Material material = new Material(Shader.Find("Standard"));
material.mainTexture = texture;
//材质球赋值给网格渲染器
meshRenderer.material = material;
}
}