WorldDlg.cs 20 KB

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