| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- using System.Collections;
- 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()
- {
- CoroutineHelper.Instance.StartCoroutine(LoadPadMaskAsync());
- }
- /// <summary>
- /// 异步加载 UIPadMask 资源
- /// </summary>
- private static IEnumerator LoadPadMaskAsync()
- {
- // 异步加载资源
- var resourceRequest = Resources.LoadAsync<GameObject>("UUI/Launcher/UIPadMask");
- // 等待资源加载完成
- yield return new WaitUntil(() => resourceRequest.isDone);
- // 在资源加载完成后实例化 UI
- if (resourceRequest.asset != null)
- {
- _ui = GameObject.Instantiate(resourceRequest.asset as GameObject);
- DontDestroyOnLoad(_ui);
- }
- else
- {
- Debug.LogError("Failed to load UIPadMask.");
- }
- }
- /// <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;
- }
- }
- }
|