using UnityEngine; using UnityEngine.UI; namespace AOT.UI { /// Fill the screen without stretching the portrait illustration. [RequireComponent(typeof(Image))] public sealed class BootArtworkCover : MonoBehaviour { void OnEnable() => Fit(); void LateUpdate() => Fit(); void OnRectTransformDimensionsChange() => Fit(); void Fit() { var image = GetComponent(); if (image == null || image.sprite == null || transform.parent == null) return; var parent = transform.parent as RectTransform; if (parent == null || parent.rect.width <= 0 || parent.rect.height <= 0) return; var rect = (RectTransform)transform; float aspect = image.sprite.rect.width / image.sprite.rect.height; float width = Mathf.Max(parent.rect.width, parent.rect.height * aspect); Vector2 size = new Vector2(width, width / aspect); rect.anchorMin = rect.anchorMax = rect.pivot = new Vector2(.5f, .5f); rect.anchoredPosition = Vector2.zero; if ((rect.sizeDelta - size).sqrMagnitude > .01f) rect.sizeDelta = size; } } }