| //-------------------------------------------------------- | 
| //    [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 }); | 
|         } | 
|   | 
|     } | 
|   | 
| } |