WorldDlg.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  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. //public void showDlg(Action action = null)
  245. //{
  246. // ShowActiveEffect(action);
  247. // this.gameObject.SetActive(true);
  248. //}
  249. //public void hideDlg(Action action = null)
  250. //{
  251. // switch (effectType)
  252. // {
  253. // case Effect.None:
  254. // transform.localScale = scaleDefault;
  255. // if (action != null)
  256. // action.Invoke();
  257. // break;
  258. // case Effect.Normal:
  259. // hideNormalEffect(action);
  260. // break;
  261. // case Effect.NormalY:
  262. // hideNormalYEffect(action);
  263. // break;
  264. // case Effect.RotateX:
  265. // hideRotateXEffect(action);
  266. // break;
  267. // }
  268. //}
  269. //void ShowActiveEffect(Action action = null)
  270. //{
  271. // switch (effectType)
  272. // {
  273. // case Effect.None:
  274. // transform.localScale = scaleDefault;
  275. // break;
  276. // case Effect.Normal:
  277. // showNormalEffect(action);
  278. // break;
  279. // case Effect.NormalY:
  280. // showNormalYEffect(action);
  281. // break;
  282. // case Effect.RotateX:
  283. // showRotateXEffect(action);
  284. // break;
  285. // }
  286. //}
  287. void showRotateXEffect(Action action = null)
  288. {
  289. Ease ease = Ease.OutBack;
  290. if (scaleDefault != Vector3.zero)
  291. {
  292. transform.DOKill();
  293. transform.localScale = scaleDefault;
  294. Vector3 pos = transform.localPosition;
  295. transform.localPosition = new Vector3(GSXRManager.Instance.head.position.x, GSXRManager.Instance.head.position.y - 0.5f, GSXRManager.Instance.head.position.z);
  296. float activeTime = 0.6f;
  297. Vector3 qa = transform.localEulerAngles;
  298. transform.localEulerAngles = new Vector3(90, 0, 0);
  299. transform.DOLocalRotate(qa, activeTime).OnComplete<Tweener>(() =>
  300. {
  301. if (action != null)
  302. action.Invoke();
  303. });
  304. transform.DOLocalMove(pos, activeTime).SetEase(Ease.OutBack);
  305. }
  306. }
  307. void showNormalYEffect(Action action = null)
  308. {
  309. Ease ease = Ease.OutBack;
  310. if (scaleDefault != Vector3.zero)
  311. {
  312. transform.DOKill();
  313. transform.localScale = new Vector3(scaleDefault.x, scaleDefault.y / 100, scaleDefault.z);
  314. float activeTime = 0.3f;
  315. transform.DOScale(scaleDefault, activeTime).SetEase(ease).OnComplete<Tweener>(() =>
  316. {
  317. if (action != null)
  318. action.Invoke();
  319. });
  320. }
  321. }
  322. void showNormalEffect(Action action = null)
  323. {
  324. Ease ease = Ease.OutBack;
  325. if (scaleDefault != Vector3.zero)
  326. {
  327. transform.DOKill();
  328. transform.localScale = scaleDefault / 100;
  329. float activeTime = 0.3f;
  330. transform.DOScale(scaleDefault, activeTime).SetEase(ease).OnComplete<Tweener>(() =>
  331. {
  332. if (action != null)
  333. action.Invoke();
  334. });
  335. }
  336. }
  337. void hideNormalYEffect(Action action)
  338. {
  339. Ease ease = Ease.InBack;
  340. transform.DOKill();
  341. float activeTime = 0.3f;
  342. transform.DOScale(new Vector3(transform.localScale.x, Vector3.zero.y, transform.localScale.z), activeTime).SetEase(ease).OnComplete<Tweener>(() =>
  343. {
  344. if (action != null)
  345. action.Invoke();
  346. this.gameObject.SetActive(false);
  347. });
  348. }
  349. void hideNormalEffect(Action action)
  350. {
  351. Ease ease = Ease.InBack;
  352. transform.DOKill();
  353. float activeTime = 0.3f;
  354. transform.DOScale(Vector3.zero, activeTime).SetEase(ease).OnComplete<Tweener>(() =>
  355. {
  356. if (action != null)
  357. action.Invoke();
  358. this.gameObject.SetActive(false);
  359. });
  360. }
  361. void hideRotateXEffect(Action action)
  362. {
  363. Ease ease = Ease.InBack;
  364. transform.DOKill();
  365. float activeTime = 0.3f;
  366. transform.DOLocalRotate(new Vector3(45, 0, 0), activeTime).OnComplete<Tweener>(() =>
  367. {
  368. if (action != null)
  369. action.Invoke();
  370. });
  371. transform.DOMove(new Vector3(GSXRManager.Instance.head.position.x, GSXRManager.Instance.head.position.y - 0.5f, GSXRManager.Instance.head.position.z), activeTime).SetEase(Ease.InBack);
  372. }
  373. /// <summary>
  374. /// 获取指定名称类型的对象
  375. /// </summary>
  376. /// <typeparam name="T"></typeparam>
  377. /// <param name="childName"></param>
  378. /// <returns></returns>
  379. public T GetChild<T>(Transform target, string childName)
  380. {
  381. return UnityUtil.GetChild<T>(target, childName);
  382. }
  383. /// <summary>
  384. /// 获取指定名称类型的对象
  385. /// </summary>
  386. /// <typeparam name="T"></typeparam>
  387. /// <param name="childName"></param>
  388. /// <returns></returns>
  389. public T GetChild<T>(string childName)
  390. {
  391. if (DlgRoot)
  392. {
  393. return GetChild<T>(DlgRoot, childName);
  394. }
  395. return GetChild<T>(transform, childName);
  396. }
  397. /// <summary>
  398. /// 深度优先搜索查找子物体
  399. /// </summary>
  400. /// <typeparam name="T"></typeparam>
  401. /// <param name="childName"></param>
  402. /// <returns></returns>
  403. public T GetDepthChild<T>(string childName)
  404. {
  405. return UnityUtil.GetDepthChild<T>(transform, childName);
  406. }
  407. /// <summary>
  408. /// 广度优先查找子物体
  409. /// </summary>
  410. /// <typeparam name="T"></typeparam>
  411. /// <param name="childName"></param>
  412. /// <returns></returns>
  413. public T GetBreadthChild<T>(string childName)
  414. {
  415. return UnityUtil.GetBreadthChild<T>(transform, childName);
  416. }
  417. public GameObject GetBreadthChild(string childName)
  418. {
  419. return UnityUtil.GetBreadthChild(transform, childName);
  420. }
  421. public T GetParent<T>()
  422. {
  423. return GetComponentInParent<T>();
  424. }
  425. public T GetChild<T>()
  426. {
  427. return GetComponentInChildren<T>();
  428. }
  429. public T GetT<T>(GameObject target)
  430. {
  431. return target.GetComponent<T>();
  432. }
  433. public T GetT<T>()
  434. {
  435. return GetT<T>(gameObject);
  436. }
  437. public T Get<T>()
  438. {
  439. return GetComponent<T>();
  440. }
  441. /// <summary>
  442. ///
  443. /// </summary>
  444. /// <param name="isDes"></param>
  445. public void Close(bool isDes = false)
  446. {
  447. if (isDes)
  448. {
  449. Destroy(gameObject);
  450. }
  451. else
  452. {
  453. gameObject.SetActive(false);
  454. }
  455. }
  456. /// <summary>
  457. /// 隐藏
  458. /// </summary>
  459. public void Show()
  460. {
  461. if (!gameObject.activeSelf)
  462. {
  463. gameObject.SetActive(true);
  464. }
  465. }
  466. /// <summary>
  467. /// 激活拖拽窗口组件
  468. /// 窗口可以拖拽移动旋转缩放,同时不再跟随视角移动变化
  469. /// </summary>
  470. public void ActiveDragDlg()
  471. {
  472. ///锁定视角
  473. IsFaceLock = true;
  474. if (Transfer)
  475. {
  476. Transfer.IsDragEnable = true;
  477. }
  478. }
  479. /// <summary>
  480. /// 失活拖拽窗口组件
  481. /// 窗口不可被移动缩放或者旋转
  482. /// </summary>
  483. public void DisActiveDragDlg()
  484. {
  485. if (Transfer)
  486. {
  487. Transfer.IsDragEnable = false;
  488. }
  489. }
  490. /// <summary>
  491. /// 锁定窗口,
  492. /// </summary>
  493. public void LockDlg()
  494. {
  495. }
  496. /// <summary>
  497. /// 解锁窗口,此窗口将跟随用户移动而移动
  498. /// </summary>
  499. public void UnLockDlg()
  500. {
  501. }
  502. public void SetScale(Vector2 size, float infoSize = 1)
  503. {
  504. size = DlgTrans.rect.size;
  505. AutoSetScale(DlgTrans, size);
  506. //print(gameObject);
  507. BG.UpdateSize(size * WorldDlg.UIScale);
  508. SetBoundSize(BG.Back, dragOffset * infoSize);
  509. if (isShowTitle)
  510. {
  511. AutoSetTitle(size);
  512. }
  513. //RefreshBounds();
  514. }
  515. public void AutoSetScale(Transform body, Vector2 size)
  516. {
  517. if (DlgRoot)
  518. {
  519. for (int i = 0; i < DlgRoot.childCount; i++)
  520. {
  521. var child = DlgRoot.GetChild(i) as RectTransform;
  522. if (child)
  523. {
  524. child.sizeDelta = size * WorldDlg.UIScale;
  525. child.localScale = Vector3.one / WorldDlg.UIScale;
  526. }
  527. }
  528. }
  529. if (FullUIRoot)
  530. {
  531. FullUIRoot.localScale = Vector3.one;
  532. FullUIRoot.sizeDelta = size;
  533. }
  534. }
  535. public void SetBoundSize(RectTransform back, Vector2 dragOffset)
  536. {
  537. if (isAddBgCollider)
  538. {
  539. if (!boxCollider)
  540. {
  541. if (!(boxCollider = BG.GetComponentInChildren<BoxCollider>()))
  542. {
  543. boxCollider = BG.gameObject.AddComponent<BoxCollider>();
  544. }
  545. //UIRayCast
  546. }
  547. Vector3 boxSize = boxCollider.size;
  548. boxSize.x = back.rect.size.x + dragOffset.x;
  549. boxSize.y = back.rect.size.y + dragOffset.y;
  550. boxCollider.size = boxSize;
  551. }
  552. }
  553. public Vector2 DlgSize
  554. {
  555. get
  556. {
  557. Vector2 size = DlgTrans.sizeDelta;
  558. size.x *= DlgTrans.localScale.x;
  559. size.y *= DlgTrans.localScale.y;
  560. if (DlgRoot)
  561. {
  562. size.x *= DlgRoot.localScale.x;
  563. size.y *= DlgRoot.localScale.y;
  564. }
  565. return size;
  566. }
  567. }
  568. /// <summary>
  569. /// 获取窗口的绝对坐标点
  570. /// </summary>
  571. public DlgRect DlgRect
  572. {
  573. get
  574. {
  575. Vector2 size = DlgTrans.sizeDelta / 2;
  576. Vector2 pos = size;
  577. dlgRect.upRight = DlgTrans.TransformPoint(pos);
  578. pos.x = -size.x;
  579. dlgRect.upLeft = DlgTrans.TransformPoint(pos);
  580. pos.y = -size.y;
  581. dlgRect.downLeft = DlgTrans.TransformPoint(pos);
  582. pos.x = size.x;
  583. dlgRect.downRight = DlgTrans.TransformPoint(pos);
  584. dlgRect.CombinePoints();
  585. return dlgRect;
  586. }
  587. }
  588. public RectTransform DlgRoot
  589. {
  590. get
  591. {
  592. if (!dlgRoot)
  593. {
  594. dlgRoot = GetBreadthChild<RectTransform>("DlgRoot");
  595. }
  596. return dlgRoot;
  597. }
  598. }
  599. public RectTransform Container
  600. {
  601. get
  602. {
  603. if (!container)
  604. {
  605. if (FullUIRoot)
  606. {
  607. container = UnityUtil.GetBreadthChild<RectTransform>(FullUIRoot, "Container");
  608. }
  609. }
  610. return container;
  611. }
  612. }
  613. /// <summary>
  614. /// 自动调整标题板的位置
  615. /// </summary>
  616. /// <param name="size"></param>
  617. public void AutoSetTitle(Vector2 size)
  618. {
  619. var tmp = XRTitle.rectTransform.rect.size;
  620. tmp.x = size.x * WorldDlg.UIScale;
  621. XRTitle.UpdateSize(tmp);
  622. XRTitle.rectTransform.sizeDelta = tmp;
  623. var pos = XRTitle.rectTransform.anchoredPosition3D;
  624. pos.y = size.y * WorldDlg.UIScale / 2 + tmp.y / 2 + titleDis;
  625. XRTitle.rectTransform.anchoredPosition3D = pos;
  626. }
  627. /// <summary>
  628. /// 判断两个窗口是否相交
  629. /// 将另一个窗口转化为本窗口的坐标系
  630. /// </summary>
  631. /// <param name="otherDlg"></param>
  632. /// <returns></returns>
  633. public bool IsIntersect(WorldDlg otherDlg)
  634. {
  635. Vector3[] other = otherDlg.DlgRect.points;
  636. for (int i = 0; i < other.Length; i++)
  637. {
  638. other[i] = DlgTrans.InverseTransformPoint(other[i]);
  639. }
  640. for (int i = 0; i < other.Length / 2; i++)
  641. {
  642. float z = other[2 * i].z * other[2 * i + 1].z;
  643. if (z < 0)
  644. {
  645. if ((Math.Abs(other[2 * i].x) < DlgTrans.sizeDelta.x / 2 && Math.Abs(other[2 * i].y) < DlgTrans.sizeDelta.y / 2) ||
  646. (Math.Abs(other[2 * i + 1].x) < DlgTrans.sizeDelta.x / 2 && Math.Abs(other[2 * i + 1].y) < DlgTrans.sizeDelta.y / 2))
  647. {
  648. return true;
  649. }
  650. }
  651. }
  652. return false;
  653. }
  654. }
  655. }