WorldDlg.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  1. using BeinLab.Util;
  2. using DG.Tweening;
  3. using ShadowStudio.Tool;
  4. using System;
  5. using UnityEngine;
  6. using XRTool.Util;
  7. namespace XRTool.WorldUI
  8. {
  9. public struct DlgRect
  10. {
  11. public Vector3 upLeft;
  12. public Vector3 upRight;
  13. public Vector3 downLeft;
  14. public Vector3 downRight;
  15. public Vector3[] points;
  16. public void CombinePoints()
  17. {
  18. if (points == null)
  19. {
  20. points = new Vector3[4];
  21. }
  22. points[0] = upRight;
  23. points[1] = upLeft;
  24. points[2] = downLeft;
  25. points[3] = downRight;
  26. }
  27. }
  28. public enum ContainerState
  29. {
  30. /// <summary>
  31. /// 自由状态
  32. /// </summary>
  33. Free = 0,
  34. /// <summary>
  35. /// 稳定与黑板状态
  36. /// </summary>
  37. OnBoard = 1,
  38. /// <summary>
  39. /// 自由移动状态
  40. /// </summary>
  41. FreeMove = 2,
  42. /// <summary>
  43. /// 在黑板中移动状态
  44. /// </summary>
  45. BoardMove = 3
  46. }
  47. /// <summary>
  48. /// 世界UI对话框
  49. /// </summary>
  50. public class WorldDlg : MonoBehaviour
  51. {
  52. private RectTransform dlgTrans;
  53. private RectTransform root;
  54. private RectTransform dlgRoot;
  55. private RectTransform fullUIRoot;
  56. private RectTransform container;
  57. private XRImage bG;
  58. private TransferCell transfer;
  59. public Vector2 dragOffset = Vector2.zero;
  60. private BoxCollider boxCollider;
  61. //[HideInInspector]
  62. public ContainerState containerState = ContainerState.Free;
  63. public bool isAddBgCollider = true;
  64. /// <summary>
  65. /// 是否可编辑姿态(位置,角度,比例)
  66. /// 可编辑状态时,取消面部锁定,编辑完成后,设置为世界锁定
  67. /// </summary>
  68. private bool isDragEnable;
  69. /// <summary>
  70. /// 是否面部锁定
  71. /// 如果面部锁定,代表自动跟随头部进行位移
  72. /// </summary>
  73. private bool isFaceLock;
  74. [HideInInspector]
  75. public bool isShowTitle;
  76. private XRImage xRTitle;
  77. [HideInInspector]
  78. public float titleDis = 10f;
  79. private Vector3 scaleDefault;
  80. private DlgRect dlgRect = new DlgRect();
  81. /// <summary>
  82. /// UI的缩放比例
  83. /// </summary>
  84. public const float UIScale = 3;
  85. public const float UIDistance = -0.003f;
  86. private Ease activeType = Ease.Linear;
  87. public Effect effectType = Effect.None;
  88. public event Action<ContainerState> OnChangeContainerState;
  89. private bool isFristEnable = true;
  90. [HideInInspector]
  91. public bool isOnBoard;
  92. public bool isShowEffect = true;
  93. public bool isDisCollider = true;
  94. public bool IsDragEnable
  95. {
  96. get => isDragEnable;
  97. set
  98. {
  99. isDragEnable = value;
  100. if (isDragEnable)
  101. {
  102. ActiveDragDlg();
  103. }
  104. else
  105. {
  106. DisActiveDragDlg();
  107. }
  108. }
  109. }
  110. public bool IsFaceLock
  111. {
  112. get => isFaceLock;
  113. set
  114. {
  115. isFaceLock = value;
  116. if (isFaceLock)
  117. {
  118. LockDlg();
  119. }
  120. else
  121. {
  122. UnLockDlg();
  123. }
  124. }
  125. }
  126. public RectTransform DlgTrans
  127. {
  128. get
  129. {
  130. if (dlgTrans == null)
  131. {
  132. return dlgTrans = GetComponent<RectTransform>();
  133. }
  134. return dlgTrans;
  135. }
  136. }
  137. public void ResetScale()
  138. {
  139. }
  140. public RectTransform Root
  141. {
  142. get
  143. {
  144. if (root == null)
  145. {
  146. return root = GetChild<RectTransform>("UIRoot");
  147. }
  148. return root;
  149. }
  150. }
  151. public XRImage BG
  152. {
  153. get
  154. {
  155. if (bG == null)
  156. {
  157. return bG = GetChild<XRImage>("BG");
  158. }
  159. return bG;
  160. }
  161. }
  162. public XRImage XRTitle
  163. {
  164. get
  165. {
  166. if (!xRTitle)
  167. {
  168. xRTitle = GetBreadthChild<XRImage>("XRTitle");
  169. }
  170. return xRTitle;
  171. }
  172. set => xRTitle = value;
  173. }
  174. public RectTransform FullUIRoot
  175. {
  176. get
  177. {
  178. if (!fullUIRoot)
  179. {
  180. fullUIRoot = GetChild<RectTransform>("FullUIRoot");
  181. }
  182. return fullUIRoot;
  183. }
  184. set => fullUIRoot = value;
  185. }
  186. /// <summary>
  187. /// 转换
  188. /// </summary>
  189. public TransferCell Transfer
  190. {
  191. get
  192. {
  193. if (!transfer)
  194. {
  195. transfer = GetComponent<TransferCell>();
  196. }
  197. return transfer;
  198. }
  199. }
  200. public ContainerState ContainerState
  201. {
  202. get => containerState;
  203. set
  204. {
  205. if (containerState != value)
  206. {
  207. containerState = value;
  208. OnChangeContainerState?.Invoke(value);
  209. }
  210. }
  211. }
  212. private void Start()
  213. {
  214. if (isFristEnable && isShowEffect)
  215. {
  216. TimerMgr.Instance.CreateTimer(() =>
  217. {
  218. showDlg();
  219. isFristEnable = false;
  220. if (Transfer && Transfer.BoundBox && Transfer.BoundBox.BoundBoxCollider)
  221. {
  222. Transfer.BoundBox.BoundBoxCollider.enabled = !isDisCollider;
  223. }
  224. }, 0.02f);
  225. }
  226. //RefreshBounds();
  227. }
  228. private void Awake()
  229. {
  230. scaleDefault = transform.localScale;
  231. if (isShowEffect)
  232. {
  233. transform.localScale = new Vector3(0, 0, 0);
  234. // this.gameObject.SetActive(false);
  235. }
  236. }
  237. private void OnEnable()
  238. {
  239. if (!isFristEnable)
  240. {
  241. ShowActiveEffect();
  242. }
  243. }
  244. //private void Update()
  245. //{
  246. // Debug.Log(gameObject.name + " " + transform.localScale);
  247. //}
  248. public void showDlg(Action action = null)
  249. {
  250. ShowActiveEffect(action);
  251. this.gameObject.SetActive(true);
  252. }
  253. public void hideDlg(Action action = null)
  254. {
  255. switch (effectType)
  256. {
  257. case Effect.None:
  258. transform.localScale = scaleDefault;
  259. if (action != null)
  260. action.Invoke();
  261. break;
  262. case Effect.Normal:
  263. hideNormalEffect(action);
  264. break;
  265. case Effect.NormalY:
  266. hideNormalYEffect(action);
  267. break;
  268. case Effect.RotateX:
  269. hideRotateXEffect(action);
  270. break;
  271. }
  272. }
  273. void ShowActiveEffect(Action action = null)
  274. {
  275. switch (effectType)
  276. {
  277. case Effect.None:
  278. transform.localScale = scaleDefault;
  279. break;
  280. case Effect.Normal:
  281. showNormalEffect(action);
  282. break;
  283. case Effect.NormalY:
  284. showNormalYEffect(action);
  285. break;
  286. case Effect.RotateX:
  287. showRotateXEffect(action);
  288. break;
  289. }
  290. }
  291. void showRotateXEffect(Action action = null)
  292. {
  293. Ease ease = Ease.OutBack;
  294. if (scaleDefault != Vector3.zero)
  295. {
  296. transform.DOKill();
  297. transform.localScale = scaleDefault;
  298. Vector3 pos = transform.localPosition;
  299. transform.localPosition = new Vector3(OpenXRCamera.Instance.head.position.x, OpenXRCamera.Instance.head.position.y - 0.5f, OpenXRCamera.Instance.head.position.z);
  300. float activeTime = 0.6f;
  301. Vector3 qa = transform.localEulerAngles;
  302. transform.localEulerAngles = new Vector3(90, 0, 0);
  303. transform.DOLocalRotate(qa, activeTime).OnComplete<Tweener>(() =>
  304. {
  305. if (action != null)
  306. action.Invoke();
  307. });
  308. transform.DOLocalMove(pos, activeTime).SetEase(Ease.OutBack);
  309. }
  310. }
  311. void showNormalYEffect(Action action = null)
  312. {
  313. Ease ease = Ease.OutBack;
  314. if (scaleDefault != Vector3.zero)
  315. {
  316. Debug.LogError("showNormalYEffect");
  317. transform.DOKill();
  318. transform.localScale = new Vector3(scaleDefault.x, scaleDefault.y / 100, scaleDefault.z);
  319. float activeTime = 0.3f;
  320. transform.DOScale(scaleDefault, activeTime).SetEase(ease).OnComplete<Tweener>(() =>
  321. {
  322. if (action != null)
  323. action.Invoke();
  324. });
  325. }
  326. }
  327. void showNormalEffect(Action action = null)
  328. {
  329. Ease ease = Ease.OutBack;
  330. if (scaleDefault != Vector3.zero)
  331. {
  332. Debug.LogError("showNormalEffect");
  333. transform.DOKill();
  334. transform.localScale = scaleDefault / 100;
  335. float activeTime = 0.3f;
  336. transform.DOScale(scaleDefault, activeTime).SetEase(ease).OnComplete<Tweener>(() =>
  337. {
  338. if (action != null)
  339. action.Invoke();
  340. });
  341. }
  342. }
  343. void hideNormalYEffect(Action action)
  344. {
  345. Debug.LogError("hideNormalYEffect");
  346. Ease ease = Ease.InBack;
  347. transform.DOKill();
  348. float activeTime = 0.3f;
  349. transform.DOScale(new Vector3(transform.localScale.x, Vector3.zero.y, transform.localScale.z), activeTime).SetEase(ease).OnComplete<Tweener>(() =>
  350. {
  351. if (action != null)
  352. action.Invoke();
  353. this.gameObject.SetActive(false);
  354. });
  355. }
  356. void hideNormalEffect(Action action)
  357. {
  358. Debug.LogError("hideNormalEffect");
  359. Ease ease = Ease.InBack;
  360. transform.DOKill();
  361. float activeTime = 0.3f;
  362. transform.DOScale(Vector3.zero, activeTime).SetEase(ease).OnComplete<Tweener>(() =>
  363. {
  364. if (action != null)
  365. action.Invoke();
  366. this.gameObject.SetActive(false);
  367. });
  368. }
  369. void hideRotateXEffect(Action action)
  370. {
  371. Ease ease = Ease.InBack;
  372. transform.DOKill();
  373. float activeTime = 0.3f;
  374. transform.DOLocalRotate(new Vector3(45, 0, 0), activeTime).OnComplete<Tweener>(() =>
  375. {
  376. if (action != null)
  377. action.Invoke();
  378. });
  379. transform.DOMove(new Vector3(OpenXRCamera.Instance.head.position.x, OpenXRCamera.Instance.head.position.y - 0.5f, OpenXRCamera.Instance.head.position.z), activeTime).SetEase(Ease.InBack);
  380. }
  381. /// <summary>
  382. /// 获取指定名称类型的对象
  383. /// </summary>
  384. /// <typeparam name="T"></typeparam>
  385. /// <param name="childName"></param>
  386. /// <returns></returns>
  387. public T GetChild<T>(Transform target, string childName)
  388. {
  389. return UnityUtil.GetChild<T>(target, childName);
  390. }
  391. /// <summary>
  392. /// 获取指定名称类型的对象
  393. /// </summary>
  394. /// <typeparam name="T"></typeparam>
  395. /// <param name="childName"></param>
  396. /// <returns></returns>
  397. public T GetChild<T>(string childName)
  398. {
  399. if (DlgRoot)
  400. {
  401. return GetChild<T>(DlgRoot, childName);
  402. }
  403. return GetChild<T>(transform, childName);
  404. }
  405. /// <summary>
  406. /// 深度优先搜索查找子物体
  407. /// </summary>
  408. /// <typeparam name="T"></typeparam>
  409. /// <param name="childName"></param>
  410. /// <returns></returns>
  411. public T GetDepthChild<T>(string childName)
  412. {
  413. return UnityUtil.GetDepthChild<T>(transform, childName);
  414. }
  415. /// <summary>
  416. /// 广度优先查找子物体
  417. /// </summary>
  418. /// <typeparam name="T"></typeparam>
  419. /// <param name="childName"></param>
  420. /// <returns></returns>
  421. public T GetBreadthChild<T>(string childName)
  422. {
  423. return UnityUtil.GetBreadthChild<T>(transform, childName);
  424. }
  425. public GameObject GetBreadthChild(string childName)
  426. {
  427. return UnityUtil.GetBreadthChild(transform, childName);
  428. }
  429. public T GetParent<T>()
  430. {
  431. return GetComponentInParent<T>();
  432. }
  433. public T GetChild<T>()
  434. {
  435. return GetComponentInChildren<T>();
  436. }
  437. public T GetT<T>(GameObject target)
  438. {
  439. return target.GetComponent<T>();
  440. }
  441. public T GetT<T>()
  442. {
  443. return GetT<T>(gameObject);
  444. }
  445. public T Get<T>()
  446. {
  447. return GetComponent<T>();
  448. }
  449. /// <summary>
  450. ///
  451. /// </summary>
  452. /// <param name="isDes"></param>
  453. public void Close(bool isDes = false)
  454. {
  455. if (isDes)
  456. {
  457. Destroy(gameObject);
  458. }
  459. else
  460. {
  461. gameObject.SetActive(false);
  462. }
  463. }
  464. /// <summary>
  465. /// 隐藏
  466. /// </summary>
  467. public void Show()
  468. {
  469. if (!gameObject.activeSelf)
  470. {
  471. gameObject.SetActive(true);
  472. }
  473. }
  474. /// <summary>
  475. /// 激活拖拽窗口组件
  476. /// 窗口可以拖拽移动旋转缩放,同时不再跟随视角移动变化
  477. /// </summary>
  478. public void ActiveDragDlg()
  479. {
  480. ///锁定视角
  481. IsFaceLock = true;
  482. if (Transfer)
  483. {
  484. Transfer.IsDragEnable = true;
  485. }
  486. }
  487. /// <summary>
  488. /// 失活拖拽窗口组件
  489. /// 窗口不可被移动缩放或者旋转
  490. /// </summary>
  491. public void DisActiveDragDlg()
  492. {
  493. if (Transfer)
  494. {
  495. Transfer.IsDragEnable = false;
  496. }
  497. }
  498. /// <summary>
  499. /// 锁定窗口,
  500. /// </summary>
  501. public void LockDlg()
  502. {
  503. }
  504. /// <summary>
  505. /// 解锁窗口,此窗口将跟随用户移动而移动
  506. /// </summary>
  507. public void UnLockDlg()
  508. {
  509. }
  510. public void SetScale(Vector2 size, float infoSize = 1)
  511. {
  512. size = DlgTrans.rect.size;
  513. AutoSetScale(DlgTrans, size);
  514. //print(gameObject);
  515. BG.UpdateSize(size * WorldDlg.UIScale);
  516. SetBoundSize(BG.Back, dragOffset * infoSize);
  517. if (isShowTitle)
  518. {
  519. AutoSetTitle(size);
  520. }
  521. //RefreshBounds();
  522. }
  523. public void AutoSetScale(Transform body, Vector2 size)
  524. {
  525. if (DlgRoot)
  526. {
  527. for (int i = 0; i < DlgRoot.childCount; i++)
  528. {
  529. var child = DlgRoot.GetChild(i) as RectTransform;
  530. if (child)
  531. {
  532. child.sizeDelta = size * WorldDlg.UIScale;
  533. child.localScale = Vector3.one / WorldDlg.UIScale;
  534. }
  535. }
  536. }
  537. if (FullUIRoot)
  538. {
  539. FullUIRoot.localScale = Vector3.one;
  540. FullUIRoot.sizeDelta = size;
  541. }
  542. }
  543. public void SetBoundSize(RectTransform back, Vector2 dragOffset)
  544. {
  545. if (isAddBgCollider)
  546. {
  547. if (!boxCollider)
  548. {
  549. if (!(boxCollider = BG.GetComponentInChildren<BoxCollider>()))
  550. {
  551. boxCollider = BG.gameObject.AddComponent<BoxCollider>();
  552. }
  553. //UIRayCast
  554. }
  555. Vector3 boxSize = boxCollider.size;
  556. boxSize.x = back.rect.size.x;// + dragOffset.x;
  557. boxSize.y = back.rect.size.y;// + dragOffset.y;
  558. boxCollider.size = boxSize;
  559. }
  560. }
  561. public Vector2 DlgSize
  562. {
  563. get
  564. {
  565. Vector2 size = DlgTrans.sizeDelta;
  566. size.x *= DlgTrans.localScale.x;
  567. size.y *= DlgTrans.localScale.y;
  568. if (DlgRoot)
  569. {
  570. size.x *= DlgRoot.localScale.x;
  571. size.y *= DlgRoot.localScale.y;
  572. }
  573. return size;
  574. }
  575. }
  576. /// <summary>
  577. /// 获取窗口的绝对坐标点
  578. /// </summary>
  579. public DlgRect DlgRect
  580. {
  581. get
  582. {
  583. Vector2 size = DlgTrans.sizeDelta / 2;
  584. Vector2 pos = size;
  585. dlgRect.upRight = DlgTrans.TransformPoint(pos);
  586. pos.x = -size.x;
  587. dlgRect.upLeft = DlgTrans.TransformPoint(pos);
  588. pos.y = -size.y;
  589. dlgRect.downLeft = DlgTrans.TransformPoint(pos);
  590. pos.x = size.x;
  591. dlgRect.downRight = DlgTrans.TransformPoint(pos);
  592. dlgRect.CombinePoints();
  593. return dlgRect;
  594. }
  595. }
  596. public RectTransform DlgRoot
  597. {
  598. get
  599. {
  600. if (!dlgRoot)
  601. {
  602. dlgRoot = GetBreadthChild<RectTransform>("DlgRoot");
  603. }
  604. return dlgRoot;
  605. }
  606. }
  607. public RectTransform Container
  608. {
  609. get
  610. {
  611. if (!container)
  612. {
  613. if (FullUIRoot)
  614. {
  615. container = UnityUtil.GetBreadthChild<RectTransform>(FullUIRoot, "Container");
  616. }
  617. }
  618. return container;
  619. }
  620. }
  621. /// <summary>
  622. /// 自动调整标题板的位置
  623. /// </summary>
  624. /// <param name="size"></param>
  625. public void AutoSetTitle(Vector2 size)
  626. {
  627. var tmp = XRTitle.rectTransform.rect.size;
  628. tmp.x = size.x * WorldDlg.UIScale;
  629. XRTitle.UpdateSize(tmp);
  630. XRTitle.rectTransform.sizeDelta = tmp;
  631. var pos = XRTitle.rectTransform.anchoredPosition3D;
  632. pos.y = size.y * WorldDlg.UIScale / 2 + tmp.y / 2 + titleDis;
  633. XRTitle.rectTransform.anchoredPosition3D = pos;
  634. }
  635. /// <summary>
  636. /// 判断两个窗口是否相交
  637. /// 将另一个窗口转化为本窗口的坐标系
  638. /// </summary>
  639. /// <param name="otherDlg"></param>
  640. /// <returns></returns>
  641. public bool IsIntersect(WorldDlg otherDlg)
  642. {
  643. Vector3[] other = otherDlg.DlgRect.points;
  644. for (int i = 0; i < other.Length; i++)
  645. {
  646. other[i] = DlgTrans.InverseTransformPoint(other[i]);
  647. }
  648. for (int i = 0; i < other.Length / 2; i++)
  649. {
  650. float z = other[2 * i].z * other[2 * i + 1].z;
  651. if (z < 0)
  652. {
  653. if ((Math.Abs(other[2 * i].x) < DlgTrans.sizeDelta.x / 2 && Math.Abs(other[2 * i].y) < DlgTrans.sizeDelta.y / 2) ||
  654. (Math.Abs(other[2 * i + 1].x) < DlgTrans.sizeDelta.x / 2 && Math.Abs(other[2 * i + 1].y) < DlgTrans.sizeDelta.y / 2))
  655. {
  656. return true;
  657. }
  658. }
  659. }
  660. return false;
  661. }
  662. }
  663. }