MatchConnections.cs 1.2 KB

12345678910111213141516171819
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. namespace Fateforge
  5. {
  6. [RequireComponent(typeof(RectTransform),typeof(CanvasRenderer))]
  7. public sealed class MatchConnections : MaskableGraphic
  8. {
  9. readonly List<Vector2> endpoints=new List<Vector2>();
  10. public void Clear(){endpoints.Clear();SetVerticesDirty();}
  11. public void Connect(Vector2 a,Vector2 b){endpoints.Add(a);endpoints.Add(b);SetVerticesDirty();}
  12. protected override void OnPopulateMesh(VertexHelper mesh)
  13. {
  14. mesh.Clear();for(int i=0;i+1<endpoints.Count;i+=2){Line(mesh,endpoints[i],endpoints[i+1],14,new Color(.2f,1,1,.09f));Line(mesh,endpoints[i],endpoints[i+1],7,new Color(.3f,1,1,.3f));Line(mesh,endpoints[i],endpoints[i+1],2.4f,new Color(.7f,1,1,.95f));}
  15. }
  16. static void Line(VertexHelper mesh,Vector2 a,Vector2 b,float width,Color tint)
  17. {var normal=new Vector2(-(b-a).y,(b-a).x).normalized*width*.5f;int n=mesh.currentVertCount;mesh.AddVert(a-normal,tint,Vector2.zero);mesh.AddVert(a+normal,tint,Vector2.zero);mesh.AddVert(b+normal,tint,Vector2.zero);mesh.AddVert(b-normal,tint,Vector2.zero);mesh.AddTriangle(n,n+1,n+2);mesh.AddTriangle(n,n+2,n+3);}
  18. }
  19. }