using UnityEngine; using UnityEngine.UI; namespace GFGGame { public class PadMaskView : MonoBehaviour { private static GameObject _ui; private Image BorderLeft; private Image BorderRight; /// /// 打开界面 /// public static void Open() { var go = Resources.Load("UUI/Launcher/UIPadMask"); _ui = GameObject.Instantiate(go); DontDestroyOnLoad(_ui); } /// /// 关闭界面 /// /// public static void Close() { GameObject.DestroyImmediate(_ui); _ui = null; } private void Start() { BorderLeft = _ui.transform.Find("BorderLeft").GetComponent(); BorderRight = _ui.transform.Find("BorderRight").GetComponent(); InitBorder(); } private void InitBorder() { //这里做了最大宽度适配 float targetWidth; float maxAspectRatio = 1080 * 1.0f / 1920; if (Screen.width * 1.0f / Screen.height > maxAspectRatio) { targetWidth = Screen.height * maxAspectRatio; } else { targetWidth = Screen.width; } float leftX = - targetWidth / 2; float rightX = -leftX; float maskWidth = (Screen.width - targetWidth) / 2; BorderLeft.rectTransform.anchoredPosition = new Vector2(leftX, 0); BorderRight.rectTransform.anchoredPosition = new Vector2(rightX, 0); BorderLeft.rectTransform.sizeDelta = new Vector2(maskWidth, BorderLeft.rectTransform.sizeDelta.y); BorderRight.rectTransform.sizeDelta = BorderLeft.rectTransform.sizeDelta; } } }