It took me a bit of looking around to find a good tutorial, but I eventually found that Unity itself has a great tutorial with a full simple example here that got me going pretty quickly. I’ve always created meshes from script instead of editing them as it seems easier to keep track of them but a mesh is basically made up of vertices, triangles, and uvs.
First of all if you’re creating a new mesh you’ll have to add a Mesh Filter and a Mesh Renderer component to the GameObject you’re creating it on, if you’re editing a mesh these will probably already be there. I’ll show code for creating a simple quad, a square surface which will ignore z for simplicity’s sake.
function Start() {
var mf : MeshFilter = GetComponent(MeshFilter); //Stores the Mesh Filter component in a variable
var mesh : Mesh = new Mesh(); //Creates a new blank Mesh for us to work with
Vertices are points in space and in Unity are Vector3’s with their x, y, and z coordinates. A mesh holds an array of all its vertices, a Vector3.
var vertices : Vector3[] = new Vector3[4] //Creates a new blank vertex array that can hold up to 4 vertices
vertices[0] = new Vector3(0, 0, 0); //Creates the first vertex at the lower left corner of the square
vertices[1] = new Vector3(1, 0, 0); //Creates the second vertex at the lower right corner of the square
vertices[2] = new Vector3(1, 1, 0); //Third vertex at upper right corner
vertices[1] = new Vector3(0, 1, 0); //Fourth Vertex at upper left corner
Triangles make up the surfaces of the mesh by connecting vertices and are made up of an array of vertex index’s, but in sets of three that make up the actual triangles in an int.
var triangles : int[] = new int[6] //Creates a new blank triangle array that can hold up to six vertices, which make up two triangles
triangles[0] = 0 //Defines a vertex by it's index (vertices[0]) from before for the start point on the first triangle
triangles[1] = 1 //Defines the vertex vertices[1] as the second point on the triangle
triangles[2] = 2 //Defines the vertex vertices[2] as the final point on the triangle
triangles[3] = 0 //We're on a new set of three, so this is a new triangle, the second one, with the start point at vertex vertices[0]
triangles[4] = 2 //Defines the vertex vertices[2] as the second point
triangles[5] = 3 //Defines the vertex vertices[3] as the final point
UVs will be the last thing you’ll need, and I’m not sure if you’ll really need to edit them as much when editing already created meshes. Vertices define points in space and triangles connect those points into surfaces which will give you an object. UVs are used to put textures onto the surfaces of your object. I’d look up UV mapping as it’s less intuitive than vertices or triangles.
Basically you’ll have a single texture file that actually holds multiple textures for your object, and UV is a coordinate system for that 2D texture. You have the u coordinate which is basically x and you have the v coordinate which is basically y. The maximum u or v is 1, and the minimum u or v is 0, everything in between is a decimal between 0 and 1. We’ll use only a fourth of our texture on this quad as an example.
var uvs : Vector2[] = new Vector2[4];
uvs[0] = new Vector2(0, 0); //Sets the lower left corner of the texture (u = 0, v = 0) at vertex vertices[0]
uvs[1] = new Vector2(0, 0.5); //Sets the upper left corner of the texture at vertex vertices[1]
uvs[2] = new Vector2(0.5, 0.5); //Upper right corner to vertices[2]
uvs[3] = new Vector2(0.5, 0); //Lower right corner to vertices[3]
Now that that’s all done you can load your new arrays into the mesh and the mesh filter.
mesh.vertices = vertices; //Loads your new vertices into the mesh
mesh.triangles = triangles; //Loads your new triangles into the mesh
mesh.uv = uv; //Loads your new uv mapping into the mesh
mesh.RecalculateNormals(); //Normals are used for lighting, you can define them yourself but it's much easier to let Unity do it for you
mf.mesh = mesh; //Loads the your new custom mesh into the mesh filter, finishing everything up
}
Mesh generation and editing can be overwhelming at first, but once you get the hang of it it actually becomes fairly easy, at least with simple meshes.
Ask any questions about any of it and I’ll be happy to help.