TornTicket.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. using UnityEngine;
  2. using UnityEngine.Sprites;
  3. using UnityEngine.UI;
  4. namespace Fateforge
  5. {
  6. // Both halves sample the same sprite and share the same cut vertices.
  7. public sealed class TornTicket : MaskableGraphic
  8. {
  9. public Sprite sprite;
  10. public bool upper;
  11. public override Texture mainTexture => sprite ? sprite.texture : Texture2D.whiteTexture;
  12. static readonly float[] Cut = { .51f, .46f, .54f, .49f, .57f, .48f, .53f, .44f, .51f };
  13. protected override void OnPopulateMesh(VertexHelper mesh)
  14. {
  15. mesh.Clear();
  16. if (!sprite) return;
  17. var r = rectTransform.rect;
  18. var uv = DataUtility.GetOuterUV(sprite);
  19. for (int i=0; i<Cut.Length; i++)
  20. {
  21. float x = i/(float)(Cut.Length-1);
  22. Add(mesh,r,uv,x,upper ? Cut[i] : 0);
  23. Add(mesh,r,uv,x,upper ? 1 : Cut[i]);
  24. if (i==0) continue;
  25. int n=(i-1)*2;
  26. mesh.AddTriangle(n,n+1,n+3); mesh.AddTriangle(n,n+3,n+2);
  27. }
  28. }
  29. void Add(VertexHelper mesh, Rect r, Vector4 uv, float x, float y)
  30. {
  31. mesh.AddVert(new Vector2(Mathf.Lerp(r.xMin,r.xMax,x),Mathf.Lerp(r.yMin,r.yMax,y)),color,
  32. new Vector2(Mathf.Lerp(uv.x,uv.z,x),Mathf.Lerp(uv.y,uv.w,y)));
  33. }
  34. }
  35. }