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; } } }