
If we attach the script to an empty GameObject, we should see an I-shaped polygon with lots of colors ( see above). Note that it is a good practice to call mesh.RecalculateBounds() and mesh.RecalculateNormals() to ensure the mesh behaves as it should. The last step is to create the mesh from the vertices, triangles, and color data and apply the mesh to a MeshFilter to draw it. Here they are initialized to random colors with Random.ColorHSV() using Linq syntax. The next step involves creating an array of colors, one for each vertex. There’s nothing too fancy here just give the object a list of points and it will give back the indices of each triangle. I copied the well-suited Triangulator.cs class specified in the Unit圓D wiki and dropped it in the project.

To find the triangles in a 2D polygon’s list of points, we can use a triangulation algorithm. A small complication is that Unity wants an array of Vector3s, so we need to convert them. Specifying vertices is easy, as we can list them in an array of Vector2s.
