
The Unity script below demonstrates how to create a Mesh (in this case a Cube) purely through code.
Simply, create a new Scene, add this script to the Main Camera, and run.
I’ve tested this script using the ‘PC, Mac & Linux Standalone’ Platform option, in ‘Build Settings’ (it should however, work fine on other platforms).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
//Create Mesh in code using UnityEngine; using System.Collections; public class UnityMeshCreator : MonoBehaviour { GameObject _cube; void Start () { //1) Create an empty GameObject with the required Components _cube = new GameObject("Cube"); _cube.AddComponent<MeshRenderer>(); MeshFilter meshFilter = _cube.AddComponent<MeshFilter>(); Mesh mesh = meshFilter.mesh; //Create a 'Cube' mesh... //2) Define the cube's dimensions float length = 1f; float width = 1f; float height = 1f; //3) Define the co-ordinates of each Corner of the cube Vector3[] c = new Vector3[8]; c[0] = new Vector3(-length * .5f, -width * .5f, height * .5f); c[1] = new Vector3(length * .5f, -width * .5f, height * .5f); c[2] = new Vector3(length * .5f, -width * .5f, -height * .5f); c[3] = new Vector3(-length * .5f, -width * .5f, -height * .5f); c[4] = new Vector3(-length * .5f, width * .5f, height * .5f); c[5] = new Vector3(length * .5f, width * .5f, height * .5f); c[6] = new Vector3(length * .5f, width * .5f, -height * .5f); c[7] = new Vector3(-length * .5f, width * .5f, -height * .5f); //4) Define the vertices that the cube is composed of: //I have used 16 vertices (4 vertices per side). //This is because I want the vertices of each side to have separate normals. //(so the object renders light/shade correctly) Vector3[] vertices = new Vector3[] { c[0], c[1], c[2], c[3], // Bottom c[7], c[4], c[0], c[3], // Left c[4], c[5], c[1], c[0], // Front c[6], c[7], c[3], c[2], // Back c[5], c[6], c[2], c[1], // Right c[7], c[6], c[5], c[4] // Top }; //5) Define each vertex's Normal Vector3 up = Vector3.up; Vector3 down = Vector3.down; Vector3 forward = Vector3.forward; Vector3 back = Vector3.back; Vector3 left = Vector3.left; Vector3 right = Vector3.right; Vector3[] normals = new Vector3[] { down, down, down, down, // Bottom left, left, left, left, // Left forward, forward, forward, forward, // Front back, back, back, back, // Back right, right, right, right, // Right up, up, up, up // Top }; //6) Define each vertex's UV co-ordinates Vector2 uv00 = new Vector2(0f, 0f); Vector2 uv10 = new Vector2(1f, 0f); Vector2 uv01 = new Vector2(0f, 1f); Vector2 uv11 = new Vector2(1f, 1f); Vector2[] uvs = new Vector2[] { uv11, uv01, uv00, uv10, // Bottom uv11, uv01, uv00, uv10, // Left uv11, uv01, uv00, uv10, // Front uv11, uv01, uv00, uv10, // Back uv11, uv01, uv00, uv10, // Right uv11, uv01, uv00, uv10 // Top }; //7) Define the Polygons (triangles) that make up the our Mesh (cube) //IMPORTANT: Unity uses a 'Clockwise Winding Order' for determining front-facing polygons. //This means that a polygon's vertices must be defined in //a clockwise order (relative to the camera) in order to be rendered/visible. int[] triangles = new int[] { 3, 1, 0, 3, 2, 1, // Bottom 7, 5, 4, 7, 6, 5, // Left 11, 9, 8, 11, 10, 9, // Front 15, 13, 12, 15, 14, 13, // Back 19, 17, 16, 19, 18, 17, // Right 23, 21, 20, 23, 22, 21, // Top }; //8) Build the Mesh mesh.Clear(); mesh.vertices = vertices; mesh.triangles = triangles; mesh.normals = normals; mesh.uv = uvs; mesh.Optimize(); //mesh.RecalculateNormals(); _cube.transform.Translate(0f, 1f, -8f); //9) Give it a Material Material cubeMaterial = new Material(Shader.Find("Standard")); cubeMaterial.SetColor("_Color", new Color(0f, 0.7f, 0f)); //green main color _cube.GetComponent<Renderer>().material = cubeMaterial; } // Update is called once per frame void Update () { //Rotate the cube _cube.transform.Rotate(5f * Time.deltaTime, 15f * Time.deltaTime, 3f * Time.deltaTime); } } |