1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
using UnityEngine;
using UnityEngine.UI;
 
public class UILineRenderer : MaskableGraphic
{
    public Vector2[] points;
 
    public float thickness = 10f;
    public bool center = true;
 
    protected override void OnPopulateMesh(VertexHelper vh)
    {
        vh.Clear();
 
        if (points.Length < 2)
            return;
 
        for (int i = 0; i < points.Length - 1; i++)
        {
            CreateLineSegment(points[i], points[i + 1], vh);
 
            int index = i * 5;
 
            vh.AddTriangle(index, index + 1, index + 3);
            vh.AddTriangle(index + 3, index + 2, index);
 
            if (i != 0)
            {
                vh.AddTriangle(index, index - 1, index - 3);
                vh.AddTriangle(index + 1, index - 1, index - 2);
            }
        }
    }
 
    private void CreateLineSegment(Vector3 point1, Vector3 point2, VertexHelper vh)
    {
        Vector3 offset = center ? (rectTransform.sizeDelta / 2) : Vector2.zero;
 
        UIVertex vertex = UIVertex.simpleVert;
        vertex.color = color;
 
        Quaternion point1Rotation = Quaternion.Euler(00, RotatePointTowards(point1, point2) + 90);
        vertex.position = point1Rotation * new Vector3(-thickness / 20);
        vertex.position += point1 - offset;
        vh.AddVert(vertex);
        vertex.position = point1Rotation * new Vector3(thickness / 20);
        vertex.position += point1 - offset;
        vh.AddVert(vertex);
 
        Quaternion point2Rotation = Quaternion.Euler(00, RotatePointTowards(point2, point1) - 90);
        vertex.position = point2Rotation * new Vector3(-thickness / 20);
        vertex.position += point2 - offset;
        vh.AddVert(vertex);
        vertex.position = point2Rotation * new Vector3(thickness / 20);
        vertex.position += point2 - offset;
        vh.AddVert(vertex);
 
        vertex.position = point2 - offset;
        vh.AddVert(vertex);
    }
 
    private float RotatePointTowards(Vector2 vertex, Vector2 target)
    {
        return (float)(Mathf.Atan2(target.y - vertex.y, target.x - vertex.x) * (180 / Mathf.PI));
    }
 
    public override void Cull(Rect clipRect, bool validRect)
    {
        base.Cull(clipRect, validRect);
        canvasRenderer.cull = !validRect;
    }
}
cs

RectTransform, CanvasRenderer 컴포넌트가 달린 오브젝트에 붙여서 사용하면 됨.
points에 등록된 vector2 좌표와 좌표를 이어주는 직선을 그린다.