KeepBackdrop.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. namespace TowerDevil
  4. {
  5. // Keep the illustrated center at design scale and extend just the scenery outside the safe frame.
  6. [RequireComponent(typeof(CanvasRenderer))]
  7. public sealed class KeepBackdrop : MaskableGraphic
  8. {
  9. Texture2D painting;
  10. readonly float[] x = new float[4], y = new float[4];
  11. static readonly float[] U = { .065f, 0, 1, .935f }, V = { .98f, 1, 0, .025f };
  12. public override Texture mainTexture => painting ? painting : base.mainTexture;
  13. internal void SetPainting(Texture2D texture)
  14. {
  15. painting = texture;
  16. raycastTarget = false;
  17. SetMaterialDirty();
  18. }
  19. internal void Fit(RectTransform page, RectTransform screen)
  20. {
  21. Vector2 tl = page.InverseTransformPoint(screen.TransformPoint(new Vector2(screen.rect.xMin, screen.rect.yMax)));
  22. Vector2 br = page.InverseTransformPoint(screen.TransformPoint(new Vector2(screen.rect.xMax, screen.rect.yMin)));
  23. rectTransform.anchoredPosition = tl;
  24. rectTransform.sizeDelta = new Vector2(br.x - tl.x, tl.y - br.y);
  25. x[0] = 0; x[1] = -tl.x; x[2] = 750 - tl.x; x[3] = br.x - tl.x;
  26. y[0] = 0; y[1] = -tl.y; y[2] = -1334 - tl.y; y[3] = br.y - tl.y;
  27. SetVerticesDirty();
  28. }
  29. protected override void OnPopulateMesh(VertexHelper mesh)
  30. {
  31. mesh.Clear();
  32. for (int row = 0; row < 4; row++)
  33. for (int col = 0; col < 4; col++)
  34. {
  35. int i = row * 4 + col;
  36. mesh.AddVert(new Vector2(x[col], y[row]), color, new Vector2(U[col], V[row]));
  37. if (row == 0 || col == 0) continue;
  38. mesh.AddTriangle(i - 5, i - 4, i);
  39. mesh.AddTriangle(i - 5, i, i - 1);
  40. }
  41. }
  42. }
  43. }