You've successfully subscribed to Hacking with Unity - Learn how to create your own games
Great! Next, complete checkout for full access to Hacking with Unity - Learn how to create your own games
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.
Success! Your billing info is updated.
Billing info update failed.

Unity Mesh Introduction

Ben Liu
Ben Liu

What is Mesh

Mesh is a component in Unity, called mesh component. Simply put, Mesh refers to the mesh of the model. The 3D model is made up of polygons, and a complex polygon is actually made up of multiple triangular faces. So the surface of a 3D model is composed of multiple triangles connected to each other. In the three-dimensional space, the set of points that make up these triangles and the sides of the triangle is Mesh. As shown below:

So to build a model is to draw a series of triangles, and positioning a triangle requires only 3 vertices. It's very simple. For example, we want to draw a pentagon as follows:

It has five vertices, but in Unity it is drawn by converting it into a series of triangles. So we need to think about the combination of several triangles to form this pentagon.

We can be composed of three triangles (1, 2, 3), (1, 3, 4) and (1, 4, 5), it either can be:

or this:

There could be many ways.

Mesh in Unity

Let us starts with creating a triangle mesh in Unity:

public class MeshDemo: MonoBehaviour {
  // Use this for initialization
  void Start() {

  // Update is called once per frame
  void Update() {


  public GameObject GetTriangle() {
    GameObject go = new GameObject("Triangle");
    MeshFilter filter = go.AddComponent < MeshFilter > ();

    // create a mesh
    Mesh mesh = new Mesh();
    filter.sharedMesh = mesh;
    mesh.vertices = new Vector3[] {
      new Vector3(0, 0, 1),
        new Vector3(0, 2, 0),
        new Vector3(2, 0, 5),

    // there is only one triangle here
    mesh.triangles = new int[3] {


    // set up shader
    Material material = new Material(Shader.Find("Diffuse"));
    material.SetColor("_Color", Color.yellow); =
    MeshRenderer renderer = go.AddComponent < MeshRenderer > ();
    renderer.sharedMaterial = material;

    return go;

Here is another example of creating pentagon mesh in Unity:

public GameObject GetPentagon ()
    GameObject go = new GameObject ("Pentagon");
    MeshFilter filter = go.AddComponent<MeshFilter> ();
    Mesh mesh = new Mesh ();
    filter.sharedMesh = mesh;
    mesh.vertices = new Vector3[] {
        new Vector3 (0, 0, 0),
        new Vector3 (0, 2, 0),
        new Vector3 (2, 0, 0),
        new Vector3 (2, -2, 0),
        new Vector3 (1, -2, 0),

    mesh.triangles = new int[9] {0, 1, 2, 0, 2, 3, 0, 3, 4};

    mesh.RecalculateNormals ();
    mesh.RecalculateBounds ();

    Material material = new Material (Shader.Find ("Diffuse"));
    material.SetColor ("_Color", Color.yellow);

    MeshRenderer renderer = go.AddComponent<MeshRenderer> ();
    renderer.sharedMaterial = material;

    return go;

Ben Liu

Please feel free to contact me if you have any question. <br>