using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CreatUVCube : MonoBehaviour
{
public MeshFilter mf;
public MeshRenderer mr;
public MeshCollider mc;
public Texture2D texture;
// Start is called before the first frame update
void Start()
{
Mesh mesh = new Mesh();
List<Vector3> vertices = new List<Vector3>();
vertices.Add(new Vector3(0, 0, 0) - Vector3.one * 0.5f);
vertices.Add(new Vector3(0, 1, 0) - Vector3.one * 0.5f);
vertices.Add(new Vector3(1, 0, 0) - Vector3.one * 0.5f);
vertices.Add(new Vector3(1, 1, 0) - Vector3.one * 0.5f);
vertices.Add(new Vector3(1, 0, 0) - Vector3.one * 0.5f);
vertices.Add(new Vector3(1, 1, 0) - Vector3.one * 0.5f);
vertices.Add(new Vector3(1, 0, 1) - Vector3.one * 0.5f);
vertices.Add(new Vector3(1, 1, 1) - Vector3.one * 0.5f);
vertices.Add(new Vector3(1, 0, 1) - Vector3.one * 0.5f);
vertices.Add(new Vector3(1, 1, 1) - Vector3.one * 0.5f);
vertices.Add(new Vector3(0, 0, 1) - Vector3.one * 0.5f);
vertices.Add(new Vector3(0, 1, 1) - Vector3.one * 0.5f);
vertices.Add(new Vector3(0, 0, 1) - Vector3.one * 0.5f);
vertices.Add(new Vector3(0, 1, 1) - Vector3.one * 0.5f);
vertices.Add(new Vector3(0, 0, 0) - Vector3.one * 0.5f);
vertices.Add(new Vector3(0, 1, 0) - Vector3.one * 0.5f);
vertices.Add(new Vector3(0, 1, 0) - Vector3.one * 0.5f);
vertices.Add(new Vector3(0, 1, 1) - Vector3.one * 0.5f);
vertices.Add(new Vector3(1, 1, 0) - Vector3.one * 0.5f);
vertices.Add(new Vector3(1, 1, 1) - Vector3.one * 0.5f);
vertices.Add(new Vector3(0, 0, 1) - Vector3.one * 0.5f);
vertices.Add(new Vector3(0, 0, 0) - Vector3.one * 0.5f);
vertices.Add(new Vector3(1, 0, 1) - Vector3.one * 0.5f);
vertices.Add(new Vector3(1, 0, 0) - Vector3.one * 0.5f);
mesh.vertices = vertices.ToArray();
List<Vector2> uv = new List<Vector2>();
for (int i = 0; i < 6; i++)
{
uv.Add(new Vector2(0, 0));
uv.Add(new Vector2(0, 1));
uv.Add(new Vector2(1, 0));
uv.Add(new Vector2(1, 1));
}
mesh.uv = uv.ToArray();
List<int> triangles = new List<int>();
for (int i = 0; i < 6; i++)
{
triangles.Add(0 + 4 * i);
triangles.Add(1 + 4 * i);
triangles.Add(2 + 4 * i);
triangles.Add(3 + 4 * i);
triangles.Add(2 + 4 * i);
triangles.Add(1 + 4 * i);
}
Material material = new Material(Shader.Find("Standard"));
material.mainTexture = texture;
mr.material = material;
mesh.triangles = triangles.ToArray();
mesh.RecalculateNormals();
mf.mesh = mesh;
}
// Update is called once per frame
void Update()
{
}
}