Created
January 16, 2016 22:17
-
-
Save davidejones/d0720f4442b88ac2e56f to your computer and use it in GitHub Desktop.
unity3d create cube from script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using UnityEngine; | |
| using System.Collections; | |
| using System.Collections.Generic; | |
| [ExecuteInEditMode] | |
| public class monkey : MonoBehaviour { | |
| private Mesh mesh; | |
| private List<Vector3> verts = new List<Vector3>(){ | |
| new Vector3(1f, 1f, -1f), | |
| new Vector3(1f, -1f, -1f), | |
| new Vector3(-1f, -1f, -1f), | |
| new Vector3(-1f, 1f, -1f), | |
| new Vector3(1f, 0.999999f, 1f), | |
| new Vector3(0.999999f, -1f, 1f), | |
| new Vector3(-1f, -1f, 1f), | |
| new Vector3(-1f, 1f, 1f) | |
| }; | |
| private List<Vector2> uvs = new List<Vector2> (){ | |
| }; | |
| private List<int> triangles = new List<int> (){ | |
| 4,0,3, | |
| 4,3,7, | |
| 2,6,7, | |
| 2,7,3, | |
| 1,5,2, | |
| 5,6,2, | |
| 0,4,1, | |
| 4,5,1, | |
| 4,7,5, | |
| 7,6,5, | |
| 0,1,2, | |
| 0,2,3, | |
| }; | |
| // Use this for initialization | |
| void Start () { | |
| //mesh = new Mesh(); | |
| mesh = GetComponent<MeshFilter>().mesh; | |
| BuildMesh(); | |
| } | |
| // Update is called once per frame | |
| void Update () { | |
| } | |
| void BuildMesh () | |
| { | |
| mesh.Clear (); | |
| mesh.vertices = verts.ToArray(); | |
| mesh.triangles = triangles.ToArray(); | |
| //mesh.uv = uvs.ToArray(); | |
| mesh.Optimize (); | |
| mesh.RecalculateNormals (); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment