1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- using UnityEngine;
- using UnityEngine.UI;
- namespace GFGGame
- {
- public class PadMaskView : MonoBehaviour
- {
- private static GameObject _ui;
- private Image BorderLeft;
- private Image BorderRight;
- /// <summary>
- /// 打开界面
- /// </summary>
- public static void Open()
- {
- var go = Resources.Load<GameObject>("UUI/Launcher/UIPadMask");
- _ui = GameObject.Instantiate(go);
- DontDestroyOnLoad(_ui);
- }
- /// <summary>
- /// 关闭界面
- /// </summary>
- /// <param name="toDestroy"></param>
- public static void Close()
- {
- GameObject.DestroyImmediate(_ui);
- _ui = null;
- }
- private void Start()
- {
- BorderLeft = _ui.transform.Find("BorderLeft").GetComponent<Image>();
- BorderRight = _ui.transform.Find("BorderRight").GetComponent<Image>();
- 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;
- }
- }
- }
|