using UnityEngine; using UnityEngine.UI; namespace TowerDevil { // Keep the illustrated center at design scale and extend just the scenery outside the safe frame. [RequireComponent(typeof(CanvasRenderer))] public sealed class KeepBackdrop : MaskableGraphic { Texture2D painting; readonly float[] x = new float[4], y = new float[4]; static readonly float[] U = { .065f, 0, 1, .935f }, V = { .98f, 1, 0, .025f }; public override Texture mainTexture => painting ? painting : base.mainTexture; internal void SetPainting(Texture2D texture) { painting = texture; raycastTarget = false; SetMaterialDirty(); } internal void Fit(RectTransform page, RectTransform screen) { Vector2 tl = page.InverseTransformPoint(screen.TransformPoint(new Vector2(screen.rect.xMin, screen.rect.yMax))); Vector2 br = page.InverseTransformPoint(screen.TransformPoint(new Vector2(screen.rect.xMax, screen.rect.yMin))); rectTransform.anchoredPosition = tl; rectTransform.sizeDelta = new Vector2(br.x - tl.x, tl.y - br.y); x[0] = 0; x[1] = -tl.x; x[2] = 750 - tl.x; x[3] = br.x - tl.x; y[0] = 0; y[1] = -tl.y; y[2] = -1334 - tl.y; y[3] = br.y - tl.y; SetVerticesDirty(); } protected override void OnPopulateMesh(VertexHelper mesh) { mesh.Clear(); for (int row = 0; row < 4; row++) for (int col = 0; col < 4; col++) { int i = row * 4 + col; mesh.AddVert(new Vector2(x[col], y[row]), color, new Vector2(U[col], V[row])); if (row == 0 || col == 0) continue; mesh.AddTriangle(i - 5, i - 4, i); mesh.AddTriangle(i - 5, i, i - 1); } } } }