40 lines
1.1 KiB
C#
40 lines
1.1 KiB
C#
using UnityEngine;
|
|
|
|
namespace UI.Utils
|
|
{
|
|
[ExecuteAlways] // ✅ Runs in Edit Mode too
|
|
[RequireComponent(typeof(RectTransform))]
|
|
public class MaxHeightScaler : MonoBehaviour
|
|
{
|
|
public float maxHeight = 400f;
|
|
public bool maintainAspectInEditor = true;
|
|
|
|
private RectTransform rect;
|
|
|
|
void Awake()
|
|
{
|
|
rect = GetComponent<RectTransform>();
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
if (rect == null) return;
|
|
|
|
// Get current screen or parent height
|
|
float availableHeight = Screen.height;
|
|
float targetHeight = Mathf.Min(availableHeight, maxHeight);
|
|
|
|
#if UNITY_EDITOR
|
|
// In Editor Mode, use parent height so UI previews nicely in Canvas
|
|
if (!Application.isPlaying && rect.parent is RectTransform parent)
|
|
{
|
|
float parentHeight = parent.rect.height;
|
|
targetHeight = Mathf.Min(parentHeight, maxHeight);
|
|
}
|
|
#endif
|
|
|
|
rect.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, targetHeight);
|
|
}
|
|
}
|
|
}
|