30 lines
1.2 KiB
C#
30 lines
1.2 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
namespace AOT.UI
|
|
{
|
|
/// <summary>Fill the screen without stretching the portrait illustration.</summary>
|
|
[RequireComponent(typeof(Image))]
|
|
public sealed class BootArtworkCover : MonoBehaviour
|
|
{
|
|
void OnEnable() => Fit();
|
|
void LateUpdate() => Fit();
|
|
void OnRectTransformDimensionsChange() => Fit();
|
|
|
|
void Fit()
|
|
{
|
|
var image = GetComponent<Image>();
|
|
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;
|
|
}
|
|
}
|
|
}
|