MeshCreator.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /// <summary>
  2. /// Credit - ryanslikesocool
  3. /// Sourced from - https://github.com/ryanslikesocool/Unity-Card-UI
  4. /// </summary>
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using UnityEngine;
  8. namespace UnityEngine.UI.Extensions
  9. {
  10. ///Credit where credit is due
  11. ///https://wiki.unity3d.com/index.php?title=Triangulator
  12. [ExecuteInEditMode]
  13. public class MeshCreator : MonoBehaviour
  14. {
  15. public void CreateMesh(List<Vector2> points)
  16. {
  17. // Create Vector2 vertices
  18. Vector2[] vertices2D = points.ToArray();
  19. // Use the triangulator to get indices for creating triangles
  20. Triangulator tr = new Triangulator(vertices2D);
  21. int[] indices = tr.Triangulate();
  22. // Create the Vector3 vertices
  23. Vector3[] vertices = new Vector3[vertices2D.Length];
  24. for (int i = 0; i < vertices.Length; i++)
  25. {
  26. vertices[i] = new Vector3(vertices2D[i].x, vertices2D[i].y, 0);
  27. }
  28. // Create the mesh
  29. Mesh msh = new Mesh();
  30. msh.vertices = vertices;
  31. msh.triangles = indices;
  32. msh.RecalculateNormals();
  33. msh.RecalculateBounds();
  34. // Set up game object with mesh;
  35. GetComponent<MeshFilter>().mesh = msh;
  36. }
  37. }
  38. }