Dlg.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. using BeinLab.Util;
  2. using SC.XR.Unity.Module_InputSystem;
  3. using System;
  4. using System.Reflection;
  5. using UnityEngine;
  6. using UnityEngine.UI;
  7. using XRTool.Util;
  8. namespace XRTool.UI
  9. {
  10. /// <summary>
  11. /// 基本的UI窗口设置
  12. /// </summary>
  13. public class Dlg : MonoBehaviour
  14. {
  15. /// <summary>
  16. /// 标准分辨率
  17. /// </summary>
  18. public static Vector2 normalSize = new Vector2(1920, 1080);
  19. private RectTransform dlgTrans;
  20. private RectTransform uiTrans;
  21. private RectTransform fullUITrans;
  22. private Image dlgBG;
  23. /// <summary>
  24. /// 是否可拖拽
  25. /// 设置成可拖拽时,窗口不在跟随视角移动而变化
  26. /// 设置成不可拖拽时,移除/失活拖拽组件
  27. /// </summary>
  28. public bool isDragEnable = false;
  29. /// <summary>
  30. /// 是否锁定窗口
  31. /// 锁定,代表此物体不在跟随用户视角变化
  32. /// 不锁定,代表此物体自动跟随用户视角
  33. /// </summary>
  34. public bool isLock = true;
  35. //public bool isBgBox = true;
  36. //public bool isAutoAddCollider = false;
  37. private ManipulationHandler dragComponent;
  38. /// <summary>
  39. /// 屏幕转屏设置适应
  40. /// 自动代表此组件不做转屏处理
  41. /// </summary>
  42. [HideInInspector]
  43. public ScreenOrientation screenOrientation = ScreenOrientation.AutoRotation;
  44. public bool isAutoSize = true;
  45. private static Canvas rootCanvas;
  46. private static Vector2 canvasSize = Vector2.zero;
  47. public Canvas DlgCanvas
  48. {
  49. get
  50. {
  51. if (RootCanvas == null)
  52. {
  53. RootCanvas = GetComponentInParent<Canvas>();
  54. }
  55. return RootCanvas;
  56. }
  57. }
  58. /// <summary>
  59. /// 添加碰撞体
  60. /// </summary>
  61. public void AddCollider(GameObject target, float hou = 1)
  62. {
  63. BoxCollider bc = target.GetComponent<BoxCollider>();
  64. if (!bc)
  65. {
  66. bc = target.AddComponent<BoxCollider>();
  67. Vector3 size = Vector3.one;
  68. size = target.GetComponent<RectTransform>().sizeDelta;
  69. size.z = hou;
  70. bc.size = size;
  71. TimerMgr.Instance.CreateTimer(() => { bc.enabled = false; }, 1f);
  72. }
  73. }
  74. public RectTransform DlgTrans
  75. {
  76. get
  77. {
  78. if (dlgTrans == null)
  79. {
  80. return dlgTrans = GetComponent<RectTransform>();
  81. }
  82. return dlgTrans;
  83. }
  84. }
  85. public RectTransform UiTrans
  86. {
  87. get
  88. {
  89. if (uiTrans == null)
  90. {
  91. return uiTrans = transform.Find("UIRoot").GetComponent<RectTransform>();
  92. }
  93. return uiTrans;
  94. }
  95. }
  96. public Image DlgBG
  97. {
  98. get
  99. {
  100. if (dlgBG == null)
  101. {
  102. return dlgBG = transform.Find("Bg").GetComponent<Image>();
  103. }
  104. return dlgBG;
  105. }
  106. }
  107. public RectTransform FullUITrans
  108. {
  109. get
  110. {
  111. if (fullUITrans == null)
  112. {
  113. return fullUITrans = transform.Find("FullUIRoot").GetComponent<RectTransform>();
  114. }
  115. return fullUITrans;
  116. }
  117. }
  118. public static Canvas RootCanvas { get => rootCanvas; set => rootCanvas = value; }
  119. public bool IsDragEnable
  120. {
  121. get => isDragEnable;
  122. set
  123. {
  124. isDragEnable = value;
  125. if (isDragEnable)
  126. {
  127. ActiveDragDlg();
  128. }
  129. else
  130. {
  131. DisActiveDragDlg();
  132. }
  133. }
  134. }
  135. public bool IsLock
  136. {
  137. get => isLock;
  138. set
  139. {
  140. isLock = value;
  141. if (isLock)
  142. {
  143. LockDlg();
  144. }
  145. else {
  146. UnLockDlg();
  147. }
  148. }
  149. }
  150. private void Start()
  151. {
  152. ///屏幕UI进行分辨率适配,其他情况不考虑
  153. if (GetComponentInParent<Canvas>().renderMode == RenderMode.ScreenSpaceOverlay)
  154. {
  155. if (isAutoSize)
  156. {
  157. if (GameSession.Instance)
  158. {
  159. GameSession.Instance.ScreenChangeAction += OnScreenChangeAction;
  160. }
  161. else
  162. {
  163. GameSession.InitComplte += () =>
  164. {
  165. GameSession.Instance.ScreenChangeAction += OnScreenChangeAction;
  166. };
  167. }
  168. }
  169. }
  170. else
  171. {
  172. //if (isBgBox)
  173. //{
  174. //AddCollider(DlgBG.gameObject);
  175. //}
  176. //if (isAutoAddCollider)
  177. //{
  178. //Selectable[] selectable = UiTrans.GetComponentsInChildren<Selectable>();
  179. //for (int i = 0; i < selectable.Length; i++)
  180. //{
  181. // AddCollider(selectable[i].gameObject);
  182. //}
  183. //}
  184. if (IsLock)
  185. {
  186. LockDlg();
  187. }
  188. else
  189. {
  190. UnLockDlg();
  191. }
  192. if (IsDragEnable)
  193. {
  194. ActiveDragDlg();
  195. }
  196. else
  197. {
  198. DisActiveDragDlg();
  199. }
  200. }
  201. }
  202. public void AddClick()
  203. {
  204. }
  205. /// <summary>
  206. /// 处理当前屏幕的朝向
  207. /// </summary>
  208. /// <param name="orientation"></param>
  209. public void OnScreenChangeAction(DeviceOrientation orientation, Vector2 curScreen)
  210. {
  211. //UpdateLayout();
  212. ///将UI设置成标准模式
  213. if (GetComponentInParent<Canvas>().renderMode == RenderMode.ScreenSpaceOverlay)
  214. {
  215. DlgTrans.localEulerAngles = Vector3.zero;
  216. ///默认将UI设置为横屏效果
  217. Vector2 norSize = normalSize;
  218. Vector2 gameScreen = curScreen;
  219. ///如果UI是竖屏展示,或者UI要自动转屏进行适配,同时屏幕方向是竖屏的时候,将UI设置为竖屏的参数
  220. if (screenOrientation == ScreenOrientation.Portrait ||
  221. (screenOrientation == ScreenOrientation.AutoRotation && GameSession.deviceOrientation == DeviceOrientation.Portrait))
  222. {
  223. norSize.x = normalSize.y;
  224. norSize.y = normalSize.x;
  225. }
  226. ///屏幕是横屏显示,但是UI要竖屏展示时,自动旋转,同时UI的比例逆转
  227. if ((GameSession.deviceOrientation == DeviceOrientation.LandscapeLeft ||
  228. GameSession.deviceOrientation == DeviceOrientation.LandscapeRight) && screenOrientation == ScreenOrientation.Portrait)
  229. {
  230. gameScreen.x = curScreen.y;
  231. gameScreen.y = curScreen.x;
  232. Vector3 angle = Vector3.forward * 90;
  233. if (GameSession.deviceOrientation == DeviceOrientation.LandscapeRight)
  234. {
  235. angle = Vector3.forward * -90;
  236. }
  237. DlgTrans.localEulerAngles = angle;
  238. }
  239. ///屏幕是竖屏,但是UI要横屏展示时,自动旋转,同时UI比例逆转
  240. else if ((GameSession.deviceOrientation == DeviceOrientation.Portrait) && screenOrientation == ScreenOrientation.LandscapeLeft)
  241. {
  242. gameScreen.x = curScreen.y;
  243. gameScreen.y = curScreen.x;
  244. Vector3 angle = Vector3.forward * -90;
  245. DlgTrans.localEulerAngles = angle;
  246. }
  247. ///将UI设置好对应的效果
  248. DlgTrans.sizeDelta = norSize;
  249. DlgBG.rectTransform.sizeDelta = norSize;
  250. UiTrans.sizeDelta = norSize;
  251. FullUITrans.sizeDelta = norSize;
  252. ///获取标准比例
  253. float norProportion = normalSize.x / normalSize.y;
  254. ///获取当前的屏宽比,忽略横竖屏,仅获取屏宽比
  255. float curProportion = (curScreen.x > curScreen.y) ? (curScreen.x / curScreen.y) : (curScreen.y / curScreen.x);
  256. float min = (curScreen.x > curScreen.y ? curScreen.x : curScreen.y) / normalSize.x;
  257. float max = (curScreen.x < curScreen.y ? curScreen.x : curScreen.y) / normalSize.y;
  258. if (min > max)
  259. {
  260. float tmp = max;
  261. max = min;
  262. min = tmp;
  263. }
  264. DlgTrans.sizeDelta = gameScreen;
  265. ///比例存在1%的误差,判断比例不一致,需要做宽窄屏适配
  266. if (Math.Abs(curProportion - norProportion) > 0.01f)
  267. {
  268. DlgBG.rectTransform.localScale = Vector3.one * max;
  269. DlgBG.rectTransform.sizeDelta = norSize;
  270. }
  271. ///比例一致,无需计算额外比例,仅需做简单的缩放即可
  272. else
  273. {
  274. DlgBG.rectTransform.sizeDelta = gameScreen;
  275. DlgBG.rectTransform.localScale = Vector3.one;
  276. }
  277. UiTrans.localScale = Vector3.one * min;
  278. FullUITrans.localScale = Vector3.one * max;
  279. }
  280. }
  281. /// <summary>
  282. /// 获取指定名称类型的对象
  283. /// </summary>
  284. /// <typeparam name="T"></typeparam>
  285. /// <param name="childName"></param>
  286. /// <returns></returns>
  287. public T GetChild<T>(Transform target, string childName)
  288. {
  289. return UnityUtil.GetChild<T>(target, childName);
  290. }
  291. /// <summary>
  292. /// 获取指定名称类型的对象
  293. /// </summary>
  294. /// <typeparam name="T"></typeparam>
  295. /// <param name="childName"></param>
  296. /// <returns></returns>
  297. public T GetChild<T>(string childName)
  298. {
  299. return GetChild<T>(transform, childName);
  300. }
  301. /// <summary>
  302. /// 深度优先搜索查找子物体
  303. /// </summary>
  304. /// <typeparam name="T"></typeparam>
  305. /// <param name="childName"></param>
  306. /// <returns></returns>
  307. public T GetDepthChild<T>(string childName)
  308. {
  309. return UnityUtil.GetDepthChild<T>(transform, childName);
  310. }
  311. /// <summary>
  312. /// 广度优先查找子物体
  313. /// </summary>
  314. /// <typeparam name="T"></typeparam>
  315. /// <param name="childName"></param>
  316. /// <returns></returns>
  317. public T GetBreadthChild<T>(string childName)
  318. {
  319. return UnityUtil.GetBreadthChild<T>(transform, childName);
  320. }
  321. public T GetParent<T>()
  322. {
  323. return GetComponentInParent<T>();
  324. }
  325. public T GetChild<T>()
  326. {
  327. return GetComponentInChildren<T>();
  328. }
  329. public T GetT<T>(GameObject target)
  330. {
  331. return target.GetComponent<T>();
  332. }
  333. public T GetT<T>()
  334. {
  335. return GetT<T>(gameObject);
  336. }
  337. public T Get<T>()
  338. {
  339. return GetComponent<T>();
  340. }
  341. /// <summary>
  342. ///
  343. /// </summary>
  344. /// <param name="isDes"></param>
  345. public void Close(bool isDes = false)
  346. {
  347. if (isDes)
  348. {
  349. Destroy(gameObject);
  350. }
  351. else
  352. {
  353. gameObject.SetActive(false);
  354. }
  355. }
  356. /// <summary>
  357. /// 隐藏
  358. /// </summary>
  359. public void Show()
  360. {
  361. if (!gameObject.activeSelf)
  362. {
  363. gameObject.SetActive(true);
  364. }
  365. }
  366. /// <summary>
  367. /// 激活拖拽窗口组件
  368. /// 窗口可以拖拽移动旋转缩放,同时不再跟随视角移动变化
  369. /// </summary>
  370. public void ActiveDragDlg()
  371. {
  372. ///锁定视角
  373. IsLock = true;
  374. if (!dragComponent)
  375. {
  376. if (GetComponent<BoundingBox>())
  377. {
  378. GetComponent<BoundingBox>().enabled = true;
  379. }
  380. else
  381. {
  382. UnityLog.Instance.LogError(gameObject + " is Null Drag");
  383. }
  384. if (dragComponent = GetComponent<ManipulationHandler>())
  385. {
  386. dragComponent.enabled = true;
  387. }
  388. else
  389. {
  390. TimerMgr.Instance.CreateTimer(() =>
  391. {
  392. dragComponent = Get<ManipulationHandler>();
  393. dragComponent.enabled = true;
  394. }, 0.1f);
  395. }
  396. }
  397. else
  398. {
  399. dragComponent.enabled = true;
  400. }
  401. }
  402. /// <summary>
  403. /// 失活拖拽窗口组件
  404. /// 窗口不可被移动缩放或者旋转
  405. /// </summary>
  406. public void DisActiveDragDlg()
  407. {
  408. if (dragComponent)
  409. {
  410. dragComponent.enabled = false;
  411. }
  412. }
  413. /// <summary>
  414. /// 锁定窗口,
  415. /// </summary>
  416. public void LockDlg()
  417. {
  418. }
  419. /// <summary>
  420. /// 解锁窗口,此窗口将跟随用户移动而移动
  421. /// </summary>
  422. public void UnLockDlg()
  423. {
  424. }
  425. }
  426. }