| 1234567891011121314151617181920212223242526272829303132333435363738 |
- using UnityEngine;
- using UnityEngine.Sprites;
- using UnityEngine.UI;
- namespace Fateforge
- {
- // Both halves sample the same sprite and share the same cut vertices.
- public sealed class TornTicket : MaskableGraphic
- {
- public Sprite sprite;
- public bool upper;
- public override Texture mainTexture => sprite ? sprite.texture : Texture2D.whiteTexture;
- static readonly float[] Cut = { .51f, .46f, .54f, .49f, .57f, .48f, .53f, .44f, .51f };
- protected override void OnPopulateMesh(VertexHelper mesh)
- {
- mesh.Clear();
- if (!sprite) return;
- var r = rectTransform.rect;
- var uv = DataUtility.GetOuterUV(sprite);
- for (int i=0; i<Cut.Length; i++)
- {
- float x = i/(float)(Cut.Length-1);
- Add(mesh,r,uv,x,upper ? Cut[i] : 0);
- Add(mesh,r,uv,x,upper ? 1 : Cut[i]);
- if (i==0) continue;
- int n=(i-1)*2;
- mesh.AddTriangle(n,n+1,n+3); mesh.AddTriangle(n,n+3,n+2);
- }
- }
- void Add(VertexHelper mesh, Rect r, Vector4 uv, float x, float y)
- {
- mesh.AddVert(new Vector2(Mathf.Lerp(r.xMin,r.xMax,x),Mathf.Lerp(r.yMin,r.yMax,y)),color,
- new Vector2(Mathf.Lerp(uv.x,uv.z,x),Mathf.Lerp(uv.y,uv.w,y)));
- }
- }
- }
|