using UnityEngine;
public class UIAdaptation : MonoBehaviour
{
[Header("刘海屏遮挡住的UI")]
public RectTransform[] ui_bangMask;
[Header("需要调整位置与缩放的UI")]
public RectTransform[] ui_transAndScale;
private void Start()
{
//调整刘海屏遮挡住的UI
if (Mathf.Abs((Screen.height * 1.0f / Screen.width) - 2.16f) < 0.01f)
{
foreach (var ui in ui_bangMask)
{
ui.localPosition -= new Vector3(0, Screen.height * 0.035f, 0);
}
}
//调整因屏幕比例变化出现问题的UI位置与缩放
foreach (var ui in ui_transAndScale)
{
float ratio = GameObject.Find("Canvas").GetComponent<RectTransform>().rect.width / 750f;
Vector3 rawLocalScale = ui.localScale;
ui.localScale = ratio * rawLocalScale;
ui.anchoredPosition = ratio * ui.anchoredPosition;
}
}
}