NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/GameWrapper/TechTree/UILineRenderer.cs

59 lines
1.8 KiB
C#

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace GameWrapper
{
[RequireComponent(typeof(CanvasRenderer))]
public class UILineRenderer : MaskableGraphic
{
public List<Vector2> points = new List<Vector2>();
public float thickness = 10f;
protected override void Awake()
{
base.Awake();
raycastTarget = false;
}
public void SetPoints(List<Vector2> points)
{
this.points = points ?? new List<Vector2>();
SetVerticesDirty();
}
protected override void OnPopulateMesh(VertexHelper vh)
{
vh.Clear();
if (points.Count < 2)
return;
// If the graphic is transparent, do nothing.
if (canvasRenderer.GetAlpha() == 0)
return;
for (int i = 0; i < points.Count; i += 2)
{
if (i + 1 >= points.Count) break;
DrawLineSegment(points[i], points[i + 1], vh);
}
}
private void DrawLineSegment(Vector2 start, Vector2 end, VertexHelper vh)
{
Vector2 dir = (end - start).normalized;
Vector2 perpendicular = new Vector2(-dir.y, dir.x) * (thickness / 2);
UIVertex[] verts = new UIVertex[4];
verts[0] = new UIVertex { position = start - perpendicular, color = this.color, uv0 = Vector2.zero };
verts[1] = new UIVertex { position = start + perpendicular, color = this.color, uv0 = Vector2.up };
verts[2] = new UIVertex { position = end + perpendicular, color = this.color, uv0 = Vector2.one };
verts[3] = new UIVertex { position = end - perpendicular, color = this.color, uv0 = Vector2.right };
vh.AddUIVertexQuad(verts);
}
}
}