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 { /// /// 基本的UI窗口设置 /// public class Dlg : MonoBehaviour { /// /// 标准分辨率 /// public static Vector2 normalSize = new Vector2(1920, 1080); private RectTransform dlgTrans; private RectTransform uiTrans; private RectTransform fullUITrans; private Image dlgBG; /// /// 是否可拖拽 /// 设置成可拖拽时,窗口不在跟随视角移动而变化 /// 设置成不可拖拽时,移除/失活拖拽组件 /// public bool isDragEnable = false; /// /// 是否锁定窗口 /// 锁定,代表此物体不在跟随用户视角变化 /// 不锁定,代表此物体自动跟随用户视角 /// public bool isLock = true; //public bool isBgBox = true; //public bool isAutoAddCollider = false; private ManipulationHandler dragComponent; /// /// 屏幕转屏设置适应 /// 自动代表此组件不做转屏处理 /// [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(); } return RootCanvas; } } /// /// 添加碰撞体 /// public void AddCollider(GameObject target, float hou = 1) { BoxCollider bc = target.GetComponent(); if (!bc) { bc = target.AddComponent(); Vector3 size = Vector3.one; size = target.GetComponent().sizeDelta; size.z = hou; bc.size = size; TimerMgr.Instance.CreateTimer(() => { bc.enabled = false; }, 1f); } } public RectTransform DlgTrans { get { if (dlgTrans == null) { return dlgTrans = GetComponent(); } return dlgTrans; } } public RectTransform UiTrans { get { if (uiTrans == null) { return uiTrans = transform.Find("UIRoot").GetComponent(); } return uiTrans; } } public Image DlgBG { get { if (dlgBG == null) { return dlgBG = transform.Find("Bg").GetComponent(); } return dlgBG; } } public RectTransform FullUITrans { get { if (fullUITrans == null) { return fullUITrans = transform.Find("FullUIRoot").GetComponent(); } 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().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(); //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() { } /// /// 处理当前屏幕的朝向 /// /// public void OnScreenChangeAction(DeviceOrientation orientation, Vector2 curScreen) { //UpdateLayout(); ///将UI设置成标准模式 if (GetComponentInParent().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; } } /// /// 获取指定名称类型的对象 /// /// /// /// public T GetChild(Transform target, string childName) { return UnityUtil.GetChild(target, childName); } /// /// 获取指定名称类型的对象 /// /// /// /// public T GetChild(string childName) { return GetChild(transform, childName); } /// /// 深度优先搜索查找子物体 /// /// /// /// public T GetDepthChild(string childName) { return UnityUtil.GetDepthChild(transform, childName); } /// /// 广度优先查找子物体 /// /// /// /// public T GetBreadthChild(string childName) { return UnityUtil.GetBreadthChild(transform, childName); } public T GetParent() { return GetComponentInParent(); } public T GetChild() { return GetComponentInChildren(); } public T GetT(GameObject target) { return target.GetComponent(); } public T GetT() { return GetT(gameObject); } public T Get() { return GetComponent(); } /// /// /// /// public void Close(bool isDes = false) { if (isDes) { Destroy(gameObject); } else { gameObject.SetActive(false); } } /// /// 隐藏 /// public void Show() { if (!gameObject.activeSelf) { gameObject.SetActive(true); } } /// /// 激活拖拽窗口组件 /// 窗口可以拖拽移动旋转缩放,同时不再跟随视角移动变化 /// public void ActiveDragDlg() { ///锁定视角 IsLock = true; if (!dragComponent) { if (GetComponent()) { GetComponent().enabled = true; } else { UnityLog.Instance.LogError(gameObject + " is Null Drag"); } if (dragComponent = GetComponent()) { dragComponent.enabled = true; } else { TimerMgr.Instance.CreateTimer(() => { dragComponent = Get(); dragComponent.enabled = true; }, 0.1f); } } else { dragComponent.enabled = true; } } /// /// 失活拖拽窗口组件 /// 窗口不可被移动缩放或者旋转 /// public void DisActiveDragDlg() { if (dragComponent) { dragComponent.enabled = false; } } /// /// 锁定窗口, /// public void LockDlg() { } /// /// 解锁窗口,此窗口将跟随用户移动而移动 /// public void UnLockDlg() { } } }