123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451 |
- using BeinLab.Util;
- using SC.XR.Unity.Module_InputSystem;
- using System;
- using System.Reflection;
- using UnityEngine;
- using UnityEngine.UI;
- using XRTool.Util;
- namespace XRTool.UI
- {
- /// <summary>
- /// 基本的UI窗口设置
- /// </summary>
- public class Dlg : MonoBehaviour
- {
- /// <summary>
- /// 标准分辨率
- /// </summary>
- public static Vector2 normalSize = new Vector2(1920, 1080);
- private RectTransform dlgTrans;
- private RectTransform uiTrans;
- private RectTransform fullUITrans;
- private Image dlgBG;
- /// <summary>
- /// 是否可拖拽
- /// 设置成可拖拽时,窗口不在跟随视角移动而变化
- /// 设置成不可拖拽时,移除/失活拖拽组件
- /// </summary>
- public bool isDragEnable = false;
- /// <summary>
- /// 是否锁定窗口
- /// 锁定,代表此物体不在跟随用户视角变化
- /// 不锁定,代表此物体自动跟随用户视角
- /// </summary>
- public bool isLock = true;
- //public bool isBgBox = true;
- //public bool isAutoAddCollider = false;
- private ManipulationHandler dragComponent;
- /// <summary>
- /// 屏幕转屏设置适应
- /// 自动代表此组件不做转屏处理
- /// </summary>
- [HideInInspector]
- public ScreenOrientation screenOrientation = ScreenOrientation.AutoRotation;
- public bool isAutoSize = true;
- private static Canvas rootCanvas;
- private static Vector2 canvasSize = Vector2.zero;
- public Canvas DlgCanvas
- {
- get
- {
- if (RootCanvas == null)
- {
- RootCanvas = GetComponentInParent<Canvas>();
- }
- return RootCanvas;
- }
- }
- /// <summary>
- /// 添加碰撞体
- /// </summary>
- public void AddCollider(GameObject target, float hou = 1)
- {
- BoxCollider bc = target.GetComponent<BoxCollider>();
- if (!bc)
- {
- bc = target.AddComponent<BoxCollider>();
- Vector3 size = Vector3.one;
- size = target.GetComponent<RectTransform>().sizeDelta;
- size.z = hou;
- bc.size = size;
- TimerMgr.Instance.CreateTimer(() => { bc.enabled = false; }, 1f);
- }
- }
- public RectTransform DlgTrans
- {
- get
- {
- if (dlgTrans == null)
- {
- return dlgTrans = GetComponent<RectTransform>();
- }
- return dlgTrans;
- }
- }
- public RectTransform UiTrans
- {
- get
- {
- if (uiTrans == null)
- {
- return uiTrans = transform.Find("UIRoot").GetComponent<RectTransform>();
- }
- return uiTrans;
- }
- }
- public Image DlgBG
- {
- get
- {
- if (dlgBG == null)
- {
- return dlgBG = transform.Find("Bg").GetComponent<Image>();
- }
- return dlgBG;
- }
- }
- public RectTransform FullUITrans
- {
- get
- {
- if (fullUITrans == null)
- {
- return fullUITrans = transform.Find("FullUIRoot").GetComponent<RectTransform>();
- }
- return fullUITrans;
- }
- }
- public static Canvas RootCanvas { get => rootCanvas; set => rootCanvas = value; }
- public bool IsDragEnable
- {
- get => isDragEnable;
- set
- {
- isDragEnable = value;
- if (isDragEnable)
- {
- ActiveDragDlg();
- }
- else
- {
- DisActiveDragDlg();
- }
- }
- }
-
- public bool IsLock
- {
- get => isLock;
- set
- {
- isLock = value;
- if (isLock)
- {
- LockDlg();
- }
- else {
- UnLockDlg();
- }
- }
- }
- private void Start()
- {
- ///屏幕UI进行分辨率适配,其他情况不考虑
- if (GetComponentInParent<Canvas>().renderMode == RenderMode.ScreenSpaceOverlay)
- {
- if (isAutoSize)
- {
- if (GameSession.Instance)
- {
- GameSession.Instance.ScreenChangeAction += OnScreenChangeAction;
- }
- else
- {
- GameSession.InitComplte += () =>
- {
- GameSession.Instance.ScreenChangeAction += OnScreenChangeAction;
- };
- }
- }
- }
- else
- {
- //if (isBgBox)
- //{
- //AddCollider(DlgBG.gameObject);
- //}
- //if (isAutoAddCollider)
- //{
- //Selectable[] selectable = UiTrans.GetComponentsInChildren<Selectable>();
- //for (int i = 0; i < selectable.Length; i++)
- //{
- // AddCollider(selectable[i].gameObject);
- //}
- //}
- if (IsLock)
- {
- LockDlg();
- }
- else
- {
- UnLockDlg();
- }
- if (IsDragEnable)
- {
- ActiveDragDlg();
- }
- else
- {
- DisActiveDragDlg();
- }
- }
- }
- public void AddClick()
- {
- }
- /// <summary>
- /// 处理当前屏幕的朝向
- /// </summary>
- /// <param name="orientation"></param>
- public void OnScreenChangeAction(DeviceOrientation orientation, Vector2 curScreen)
- {
- //UpdateLayout();
- ///将UI设置成标准模式
- if (GetComponentInParent<Canvas>().renderMode == RenderMode.ScreenSpaceOverlay)
- {
- DlgTrans.localEulerAngles = Vector3.zero;
- ///默认将UI设置为横屏效果
- Vector2 norSize = normalSize;
- Vector2 gameScreen = curScreen;
- ///如果UI是竖屏展示,或者UI要自动转屏进行适配,同时屏幕方向是竖屏的时候,将UI设置为竖屏的参数
- if (screenOrientation == ScreenOrientation.Portrait ||
- (screenOrientation == ScreenOrientation.AutoRotation && GameSession.deviceOrientation == DeviceOrientation.Portrait))
- {
- norSize.x = normalSize.y;
- norSize.y = normalSize.x;
- }
- ///屏幕是横屏显示,但是UI要竖屏展示时,自动旋转,同时UI的比例逆转
- if ((GameSession.deviceOrientation == DeviceOrientation.LandscapeLeft ||
- GameSession.deviceOrientation == DeviceOrientation.LandscapeRight) && screenOrientation == ScreenOrientation.Portrait)
- {
- gameScreen.x = curScreen.y;
- gameScreen.y = curScreen.x;
- Vector3 angle = Vector3.forward * 90;
- if (GameSession.deviceOrientation == DeviceOrientation.LandscapeRight)
- {
- angle = Vector3.forward * -90;
- }
- DlgTrans.localEulerAngles = angle;
- }
- ///屏幕是竖屏,但是UI要横屏展示时,自动旋转,同时UI比例逆转
- else if ((GameSession.deviceOrientation == DeviceOrientation.Portrait) && screenOrientation == ScreenOrientation.LandscapeLeft)
- {
- gameScreen.x = curScreen.y;
- gameScreen.y = curScreen.x;
- Vector3 angle = Vector3.forward * -90;
- DlgTrans.localEulerAngles = angle;
- }
- ///将UI设置好对应的效果
- DlgTrans.sizeDelta = norSize;
- DlgBG.rectTransform.sizeDelta = norSize;
- UiTrans.sizeDelta = norSize;
- FullUITrans.sizeDelta = norSize;
- ///获取标准比例
- float norProportion = normalSize.x / normalSize.y;
- ///获取当前的屏宽比,忽略横竖屏,仅获取屏宽比
- float curProportion = (curScreen.x > curScreen.y) ? (curScreen.x / curScreen.y) : (curScreen.y / curScreen.x);
- float min = (curScreen.x > curScreen.y ? curScreen.x : curScreen.y) / normalSize.x;
- float max = (curScreen.x < curScreen.y ? curScreen.x : curScreen.y) / normalSize.y;
- if (min > max)
- {
- float tmp = max;
- max = min;
- min = tmp;
- }
- DlgTrans.sizeDelta = gameScreen;
- ///比例存在1%的误差,判断比例不一致,需要做宽窄屏适配
- if (Math.Abs(curProportion - norProportion) > 0.01f)
- {
- DlgBG.rectTransform.localScale = Vector3.one * max;
- DlgBG.rectTransform.sizeDelta = norSize;
- }
- ///比例一致,无需计算额外比例,仅需做简单的缩放即可
- else
- {
- DlgBG.rectTransform.sizeDelta = gameScreen;
- DlgBG.rectTransform.localScale = Vector3.one;
- }
- UiTrans.localScale = Vector3.one * min;
- FullUITrans.localScale = Vector3.one * max;
- }
- }
- /// <summary>
- /// 获取指定名称类型的对象
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="childName"></param>
- /// <returns></returns>
- public T GetChild<T>(Transform target, string childName)
- {
- return UnityUtil.GetChild<T>(target, childName);
- }
- /// <summary>
- /// 获取指定名称类型的对象
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="childName"></param>
- /// <returns></returns>
- public T GetChild<T>(string childName)
- {
- return GetChild<T>(transform, childName);
- }
- /// <summary>
- /// 深度优先搜索查找子物体
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="childName"></param>
- /// <returns></returns>
- public T GetDepthChild<T>(string childName)
- {
- return UnityUtil.GetDepthChild<T>(transform, childName);
- }
- /// <summary>
- /// 广度优先查找子物体
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="childName"></param>
- /// <returns></returns>
- public T GetBreadthChild<T>(string childName)
- {
- return UnityUtil.GetBreadthChild<T>(transform, childName);
- }
- public T GetParent<T>()
- {
- return GetComponentInParent<T>();
- }
- public T GetChild<T>()
- {
- return GetComponentInChildren<T>();
- }
- public T GetT<T>(GameObject target)
- {
- return target.GetComponent<T>();
- }
- public T GetT<T>()
- {
- return GetT<T>(gameObject);
- }
- public T Get<T>()
- {
- return GetComponent<T>();
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="isDes"></param>
- public void Close(bool isDes = false)
- {
- if (isDes)
- {
- Destroy(gameObject);
- }
- else
- {
- gameObject.SetActive(false);
- }
- }
- /// <summary>
- /// 隐藏
- /// </summary>
- public void Show()
- {
- if (!gameObject.activeSelf)
- {
- gameObject.SetActive(true);
- }
- }
- /// <summary>
- /// 激活拖拽窗口组件
- /// 窗口可以拖拽移动旋转缩放,同时不再跟随视角移动变化
- /// </summary>
- public void ActiveDragDlg()
- {
- ///锁定视角
- IsLock = true;
- if (!dragComponent)
- {
- if (GetComponent<BoundingBox>())
- {
- GetComponent<BoundingBox>().enabled = true;
- }
- else
- {
- UnityLog.Instance.LogError(gameObject + " is Null Drag");
- }
- if (dragComponent = GetComponent<ManipulationHandler>())
- {
- dragComponent.enabled = true;
- }
- else
- {
- TimerMgr.Instance.CreateTimer(() =>
- {
- dragComponent = Get<ManipulationHandler>();
- dragComponent.enabled = true;
- }, 0.1f);
- }
- }
- else
- {
- dragComponent.enabled = true;
- }
- }
- /// <summary>
- /// 失活拖拽窗口组件
- /// 窗口不可被移动缩放或者旋转
- /// </summary>
- public void DisActiveDragDlg()
- {
- if (dragComponent)
- {
- dragComponent.enabled = false;
- }
- }
- /// <summary>
- /// 锁定窗口,
- /// </summary>
- public void LockDlg()
- {
- }
- /// <summary>
- /// 解锁窗口,此窗口将跟随用户移动而移动
- /// </summary>
- public void UnLockDlg()
- {
- }
- }
- }
|