yyl
2025-06-09 b9751b2f076ee050fe5b685e91ae4fc4469b1015
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
//--------------------------------------------------------
//    [Author]:           第二世界
//    [  Date ]:           Thursday, August 10, 2017
//--------------------------------------------------------
 
using UnityEngine;
using UnityEngine.UI;
 
 
/// <summary>
/// 这个组件接收一些顶点,绘制出水平和垂直的线。
/// </summary>
public class PolylineImage:Graphic {
 
    [SerializeField]
    float m_Width;
    public float width {
        get {
            return this.m_Width;
        }
        set {
            this.m_Width = value;
            base.SetVerticesDirty();
        }
    }
 
    [SerializeField]
    Vector2[] m_Points;
    public Vector2[] points {
        get {
            return this.m_Points;
        }
        set {
            this.m_Points = value;
            base.SetVerticesDirty();
        }
    }
 
    protected override void OnPopulateMesh(VertexHelper vh) {
        vh.Clear();
        if(this.points == null || this.points.Length < 2) {
            return;
        }
 
        var hw = this.width * 0.5f;
 
        for(var i = 0;i < this.points.Length - 1;i++) {
            var point = this.points[i];
            var nextPoint = this.points[i + 1];
 
            var xReversal = point.x > nextPoint.x;
            var yReversal = point.y > nextPoint.y;
 
            var positions = new Vector3[4];
            positions[0] = new Vector2(xReversal ? point.x + hw : point.x - hw,yReversal ? point.y + hw : point.y - hw);
            positions[1] = new Vector2(xReversal ? nextPoint.x - hw : nextPoint.x + hw,yReversal ? point.y + hw : point.y - hw);
            positions[2] = new Vector2(xReversal ? nextPoint.x - hw : nextPoint.x + hw,yReversal ? nextPoint.y - hw : nextPoint.y + hw);
            positions[3] = new Vector2(xReversal ? point.x + hw : point.x - hw,yReversal ? nextPoint.y - hw : nextPoint.y + hw);
 
            UIUtility.AddQuad(vh,positions,color,new Vector2[4] { Vector2.one,Vector2.one,Vector2.one,Vector2.one });
        }
 
    }
 
}