Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Draw Polygon on Hud

Discussion in 'Editor & General Support' started by Dustin27, Jun 18, 2019.

  1. Dustin27

    Dustin27

    Joined:
    Nov 12, 2018
    Posts:
    15
    Hi,

    is there any possibility to draw a polygon defined by some points on the hud?

    Kind regards
     
  2. Dustin27

    Dustin27

    Joined:
    Nov 12, 2018
    Posts:
    15
    I found a way using VertexHelper:

    Code (CSharp):
    1. Mesh mesh;
    2. List<Vec2D> points;
    3. using (VertexHelper helper = new VertexHelper())
    4. {
    5.     // Add the points as vertices
    6.     foreach (var point in points)
    7.     {
    8.         helper.AddVert(point);
    9.     }
    10.  
    11.     // Convert to Vector2 array in order to be able to use Triangulator
    12.     Vector2[] vertices2d = new Vector2[points.Count];
    13.     for (int i = 0; i < points.Count; i++)
    14.     {
    15.         vertices2d[i].x = (float)-points[i].y;
    16.         vertices2d[i].y = (float)points[i].x;
    17.     }
    18.  
    19.     // Generate triangles by triangulator and add them
    20.     Triangulator triangulator = new Triangulator(vertices2d);
    21.     int[] triangles = triangulator.Triangulate();
    22.  
    23.     for (int i = 0; i < triangles.Length; i += 3)
    24.     {
    25.         helper.AddTriangle(triangles[i], triangles[i + 1], triangles[i + 2]);
    26.     }
    27.  
    28.     helper.FillMesh(mesh);
    29. }
    With this help function:
    Code (CSharp):
    1.     public static void AddVert(this VertexHelper helper, MBVec2D vector)
    2.     {
    3.         helper.AddVert(
    4.             new Vector3(
    5.                 (float)vector.x,
    6.                 (float)vector.y),
    7.             defaultColor,
    8.             new Vector2(
    9.                 0.01f * (float)vector.x,
    10.                 0.01f * (float)vector.y));
    11.     }
    My only problem now is that the mesh does not work with a CanvasRenderer but with a MeshRenderer so i cannot show it in the game hud althought it is shown in Scene. :/