MenuUI.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. using DG.Tweening;
  2. using SC.XR.Unity.Module_InputSystem;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. using UnityEngine.EventSystems;
  7. using UnityEngine.UI;
  8. using SC.XR.Unity;
  9. using XRTool.Util;
  10. public class MenuUI : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
  11. {
  12. Transform _pointParent;//曲线控制点父物体
  13. List<Transform> _anchorPointGroup = new List<Transform>();//曲线控制点集合
  14. Transform _menuParent;//菜单元素父物体
  15. List<Transform> _menuElementGroup = new List<Transform>();//菜单元素集合
  16. List<MenuIcon> _elementDataGroup = new List<MenuIcon>();//元素数据集合。菜单元素集合与元素数据集合始终保持一 一对应的关系(在集合中的位置)
  17. int _preMidIndex;//元素集合中间索引的上一个索引
  18. int _middleIndex;//元素集合中间索引
  19. int _nextMidIndex;//元素集合中间索引的下一个索引
  20. int _segmentNum;//所需贝塞尔曲线点连接的段数(点的数量减一)
  21. public Dictionary<int, Vector3> _bezierPointGroup = new Dictionary<int, Vector3>();//记录所需贝塞尔曲线定位点的集合
  22. Vector3[] _velocityGroup;//从一点到另一点的最大速度集合
  23. float _spacingTime = 0.2f;//从一点到另一点的最短时间(用于计算预设的最大速度)
  24. bool _isInLeft = false;//图标是否开始真正往左串位
  25. bool _isInRight = false;//图标是否开始真正往右串位
  26. float _maxDragValue = 100f;//拖动时,x坐标的预设增量基数
  27. Vector3 _normalScale;//正常的缩放值
  28. Vector3 _targetScale;//预设的目标放大值
  29. float _scaleFactor = 1.4f;//缩放系数
  30. bool _isOver = false;//链表中数据是否读取完
  31. bool _isClickDel = false;//是否点击了删除按钮
  32. bool _isInit = false;//是否初始化完毕
  33. /// <summary>
  34. /// 初始化
  35. /// </summary>
  36. public void Init()
  37. {
  38. _pointParent = transform.Find("AnchorPoint");
  39. _menuParent = transform.Find("Element");
  40. LoadAnchorPoint();
  41. LoadGameObject();
  42. _segmentNum = _menuElementGroup.Count + 1;
  43. _velocityGroup = new Vector3[_segmentNum];
  44. _normalScale = _menuElementGroup[0].localScale;
  45. _targetScale = _normalScale * _scaleFactor;
  46. SaveCurvePoint();
  47. GetVelocity();
  48. DisplayMenuElement();
  49. RecordValue();
  50. EventTool._rearrange += RearrangeElements;
  51. _isInit = true;
  52. }
  53. void LoadAnchorPoint()//加载定位点
  54. {
  55. foreach (Transform item in _pointParent)
  56. {
  57. _anchorPointGroup.Add(item);
  58. }
  59. }
  60. MenuIcon _tempIcon;
  61. void LoadGameObject()//加载游戏对象和游戏对象所承载的数据
  62. {
  63. foreach (Transform item in _menuParent)
  64. {
  65. _tempIcon = item.gameObject.AddComponent<MenuIcon>();
  66. _tempIcon.Init();
  67. _menuElementGroup.Add(item);
  68. _elementDataGroup.Add(_tempIcon);
  69. }
  70. LoadData();
  71. _preMidIndex = _menuElementGroup.Count / 2 - 1;
  72. _middleIndex = _menuElementGroup.Count / 2;
  73. _nextMidIndex = _menuElementGroup.Count / 2 + 1;
  74. }
  75. /// <summary>
  76. /// 初始化加载数据
  77. /// </summary>
  78. public void LoadData()
  79. {
  80. _isOver = false;
  81. //清空数据
  82. for (int i = 0; i < _elementDataGroup.Count; i++)
  83. {
  84. _elementDataGroup[i].DataConfig = null;
  85. }
  86. DoubleLinkNode<RoomConfig> _node = ConfigModel.Instance.GetFirstMenuItem();
  87. //初始化数据
  88. for (int i = 0; i < _elementDataGroup.Count; i++)
  89. {
  90. if (!_isOver)
  91. {
  92. if (ConfigModel.Instance.GetFirstMenuItem() != null)
  93. {
  94. _elementDataGroup[i].DataConfig = _node;
  95. _node = _node.NextNode;
  96. if (_node.PreNode == ConfigModel.Instance.GetLastMenuItem())
  97. {
  98. _isOver = true;
  99. }
  100. }
  101. }
  102. }
  103. }
  104. void GetVelocity()//求得从一点到另一点的最大速度(存入数组中)(速度方向从左指向右)
  105. {
  106. for (int i = 0; i < _velocityGroup.Length; i++)
  107. {
  108. float _dis = Vector3.Distance(_bezierPointGroup[i - 1], _bezierPointGroup[i]);
  109. _velocityGroup[i] = (_dis / _spacingTime) * (_bezierPointGroup[i] - _bezierPointGroup[i - 1]).normalized;
  110. }
  111. }
  112. void DisplayMenuElement()//刷新让元素处于对应的坐标点和缩放值
  113. {
  114. for (int i = 0; i < _menuElementGroup.Count; i++)
  115. {
  116. _menuElementGroup[i].localPosition = _bezierPointGroup[i];
  117. }
  118. if (!_isClickDel)//如果没有点击删除按钮,则中间位置元素为放大显示
  119. {
  120. //
  121. _menuElementGroup[_middleIndex].localScale = _targetScale;
  122. _menuElementGroup[_preMidIndex].localScale = _normalScale;
  123. _menuElementGroup[_nextMidIndex].localScale = _normalScale;
  124. //
  125. }
  126. }
  127. private void SaveCurvePoint()//存储贝塞尔曲线上的七个定位点位置
  128. {
  129. for (int i = -1; i < _segmentNum; i++)
  130. {
  131. float t = (i + 1) / (float)_segmentNum;
  132. int nodeIndex = 0;
  133. Vector3 pixel = CalculateBezierPoint(t, _anchorPointGroup[nodeIndex].localPosition,
  134. _anchorPointGroup[nodeIndex + 1].localPosition, _anchorPointGroup[nodeIndex + 2].localPosition);
  135. _bezierPointGroup.Add(i, pixel);
  136. }
  137. }
  138. void ResetCurvePoint()//重置贝塞尔曲线上的七个定位点
  139. {
  140. for (int i = -1; i < _segmentNum; i++)
  141. {
  142. float t = (i + 1) / (float)_segmentNum;
  143. int nodeIndex = 0;
  144. Vector3 pixel = CalculateBezierPoint(t, _anchorPointGroup[nodeIndex].localPosition,
  145. _anchorPointGroup[nodeIndex + 1].localPosition, _anchorPointGroup[nodeIndex + 2].localPosition);
  146. _bezierPointGroup[i] = pixel;
  147. }
  148. }
  149. void Update()
  150. {
  151. if (_isInit)
  152. {
  153. ResetCurvePoint();
  154. }
  155. }
  156. private Vector3 CalculateBezierPoint(float t, Vector3 p0, Vector3 p1, Vector3 p2)//根据t值计算相应曲线上的点
  157. {
  158. float u = 1 - t;
  159. float tt = t * t;
  160. float uu = u * u;
  161. Vector3 p = uu * p0;
  162. p += 2 * u * t * p1;
  163. p += tt * p2;
  164. return p;
  165. }
  166. public void OnBeginDrag(PointerEventData eventData)
  167. {
  168. IsDrag(true);
  169. }
  170. public void OnDrag(PointerEventData eventData)
  171. {
  172. float _delta = eventData.delta.x / _maxDragValue;//记录拖动的增量比值(用作速度比值)
  173. if (_delta < -0.1f)//向左拖动时
  174. {
  175. _isInRight = false;
  176. if (!_isInLeft)//如果没有归位到当前元素应该处于的位置上,就先让元素先移动到对应的位置上
  177. {
  178. if (_menuElementGroup[0].localPosition == _bezierPointGroup[0])
  179. {
  180. if (!_isClickDel)//如果没有点击删除按钮,则播中间位置元素为放大显示
  181. {
  182. //
  183. _menuElementGroup[_middleIndex].localScale = _targetScale;
  184. _menuElementGroup[_preMidIndex].localScale = _normalScale;
  185. //
  186. }
  187. _isInLeft = true;
  188. return;
  189. }
  190. for (int i = 0; i < _menuElementGroup.Count; i++)
  191. {
  192. if (!_isClickDel)//如果没有点击删除按钮,则播放缩放动画
  193. {
  194. //
  195. if (i == _middleIndex)
  196. {
  197. float _prop = Vector3.Distance(_menuElementGroup[i].localPosition, _bezierPointGroup[i + 1]) /
  198. Vector3.Distance(_bezierPointGroup[i], _bezierPointGroup[i + 1]);
  199. _menuElementGroup[i].localScale = Vector3.Lerp(_normalScale, _targetScale, _prop);
  200. }
  201. else if (i == _preMidIndex)
  202. {
  203. float _prop = Vector3.Distance(_menuElementGroup[i].localPosition, _bezierPointGroup[i + 1]) /
  204. Vector3.Distance(_bezierPointGroup[i], _bezierPointGroup[i + 1]);
  205. _menuElementGroup[i].localScale = Vector3.Lerp(_targetScale, _normalScale, _prop);
  206. }
  207. //
  208. }
  209. _menuElementGroup[i].DOKill();
  210. //拖拽的增量比值乘以预设的最大速度,实现设备拖拽速度与游戏对象移动速度成正比
  211. _menuElementGroup[i].DOLocalMove(_bezierPointGroup[i], -_delta * _velocityGroup[i + 1].magnitude).SetSpeedBased();
  212. }
  213. }
  214. else//开始移位
  215. {
  216. for (int i = 0; i < _menuElementGroup.Count; i++)
  217. {
  218. if (!_isClickDel)//如果没有点击删除按钮,则播放缩放动画
  219. {
  220. //
  221. if (i == _middleIndex)
  222. {
  223. float _prop = Vector3.Distance(_menuElementGroup[i].localPosition, _bezierPointGroup[i]) /
  224. Vector3.Distance(_bezierPointGroup[i], _bezierPointGroup[i - 1]);
  225. _menuElementGroup[i].localScale = Vector3.Lerp(_targetScale, _normalScale, _prop);
  226. }
  227. else if (i == _nextMidIndex)
  228. {
  229. float _prop = Vector3.Distance(_menuElementGroup[i].localPosition, _bezierPointGroup[i]) /
  230. Vector3.Distance(_bezierPointGroup[i], _bezierPointGroup[i - 1]);
  231. _menuElementGroup[i].localScale = Vector3.Lerp(_normalScale, _targetScale, _prop);
  232. }
  233. //
  234. }
  235. _menuElementGroup[i].DOKill();
  236. _menuElementGroup[i].DOLocalMove(_bezierPointGroup[i - 1], -_delta * _velocityGroup[i].magnitude).SetSpeedBased();
  237. }
  238. if (_menuElementGroup[0].localPosition == _bezierPointGroup[-1])
  239. {
  240. _menuElementGroup[0].DOKill();
  241. ShiftToLeft();
  242. DisplayMenuElement();
  243. }
  244. }
  245. }
  246. else if (_delta > 0.1f)//向右拖动时
  247. {
  248. _isInLeft = false;
  249. if (!_isInRight)//如果没有归位到当前元素应该处于的位置上,就先让元素移动到对应的位置上
  250. {
  251. if (_menuElementGroup[0].localPosition == _bezierPointGroup[0])
  252. {
  253. if (!_isClickDel)//如果没有点击删除按钮,则中间位置元素为放大显示
  254. {
  255. //
  256. _menuElementGroup[_middleIndex].localScale = _targetScale;
  257. _menuElementGroup[_nextMidIndex].localScale = _normalScale;
  258. //
  259. }
  260. _isInRight = true;
  261. return;
  262. }
  263. for (int i = 0; i < _menuElementGroup.Count; i++)
  264. {
  265. if (!_isClickDel)//如果没有点击删除按钮,则播放缩放动画
  266. {
  267. //
  268. if (i == _middleIndex)
  269. {
  270. float _prop = Vector3.Distance(_menuElementGroup[i].localPosition, _bezierPointGroup[i - 1]) /
  271. Vector3.Distance(_bezierPointGroup[i], _bezierPointGroup[i - 1]);
  272. _menuElementGroup[i].localScale = Vector3.Lerp(_normalScale, _targetScale, _prop);
  273. }
  274. else if (i == _nextMidIndex)
  275. {
  276. float _prop = Vector3.Distance(_menuElementGroup[i].localPosition, _bezierPointGroup[i - 1]) /
  277. Vector3.Distance(_bezierPointGroup[i], _bezierPointGroup[i - 1]);
  278. _menuElementGroup[i].localScale = Vector3.Lerp(_targetScale, _normalScale, _prop);
  279. }
  280. //
  281. }
  282. _menuElementGroup[i].DOKill();
  283. _menuElementGroup[i].DOLocalMove(_bezierPointGroup[i], _delta * _velocityGroup[i].magnitude).SetSpeedBased();
  284. }
  285. }
  286. else//开始移位
  287. {
  288. for (int i = 0; i < _menuElementGroup.Count; i++)
  289. {
  290. if (!_isClickDel)//如果没有点击删除按钮,则播放缩放动画
  291. {
  292. //
  293. if (i == _middleIndex)
  294. {
  295. float _prop = Vector3.Distance(_menuElementGroup[i].localPosition, _bezierPointGroup[i]) /
  296. Vector3.Distance(_bezierPointGroup[i], _bezierPointGroup[i + 1]);
  297. _menuElementGroup[i].localScale = Vector3.Lerp(_targetScale, _normalScale, _prop);
  298. }
  299. else if (i == _preMidIndex)
  300. {
  301. float _prop = Vector3.Distance(_menuElementGroup[i].localPosition, _bezierPointGroup[i]) /
  302. Vector3.Distance(_bezierPointGroup[i], _bezierPointGroup[i + 1]);
  303. _menuElementGroup[i].localScale = Vector3.Lerp(_normalScale, _targetScale, _prop);
  304. }
  305. //
  306. }
  307. _menuElementGroup[i].DOKill();
  308. _menuElementGroup[i].DOLocalMove(_bezierPointGroup[i + 1], _delta * _velocityGroup[i + 1].magnitude).SetSpeedBased();
  309. }
  310. if (_menuElementGroup[_menuElementGroup.Count - 1].localPosition == _bezierPointGroup[_menuElementGroup.Count])
  311. {
  312. _menuElementGroup[_menuElementGroup.Count - 1].DOKill();
  313. ShiftToRight();
  314. DisplayMenuElement();
  315. }
  316. }
  317. }
  318. else//无位置变换时间超过一秒,则游戏对象不会移动
  319. {
  320. for (int i = 0; i < _menuElementGroup.Count; i++)
  321. {
  322. _menuElementGroup[i].DOKill();
  323. }
  324. }
  325. }
  326. public void OnEndDrag(PointerEventData eventData)
  327. {
  328. //拖拽结束时,移动回当前对应的定位点位置
  329. for (int i = 0; i < _menuElementGroup.Count; i++)
  330. {
  331. _menuElementGroup[i].DOKill();
  332. _menuElementGroup[i].DOLocalMove(_bezierPointGroup[i], 0.5f);
  333. if (!_isClickDel)//如果没有点击删除按钮,则播放缩放动画
  334. {
  335. _menuElementGroup[_middleIndex].DOScale(_targetScale, 0.5f);
  336. }
  337. else
  338. {
  339. _menuElementGroup[_middleIndex].DOScale(_normalScale, 0.5f);
  340. }
  341. _menuElementGroup[_preMidIndex].DOScale(_normalScale, 0.5f);
  342. _menuElementGroup[_nextMidIndex].DOScale(_normalScale, 0.5f);
  343. }
  344. IsDrag(false);
  345. }
  346. private void ShiftToLeft()//集合元素(游戏对象与数据)往左串位
  347. {
  348. //游戏对象往左串位
  349. var _tran = _menuElementGroup[0];
  350. _menuElementGroup.RemoveAt(0);
  351. _menuElementGroup.Add(_tran);
  352. //数据脚本往左串位
  353. var _icon = _elementDataGroup[0];
  354. if (_elementDataGroup[0].DataConfig != null && _elementDataGroup[_elementDataGroup.Count - 1].DataConfig != null)
  355. {
  356. _icon.DataConfig = _elementDataGroup[_elementDataGroup.Count - 1].DataConfig.NextNode;
  357. }
  358. _elementDataGroup.RemoveAt(0);
  359. _elementDataGroup.Add(_icon);
  360. _icon.RefreshData();
  361. //刷新记录物体的缩放值和索引值
  362. RecordValue();
  363. }
  364. private void ShiftToRight()//集合元素(游戏对象与数据)往右串位
  365. {
  366. //游戏对象往右串位
  367. var _tran = _menuElementGroup[_menuElementGroup.Count - 1];
  368. _menuElementGroup.RemoveAt(_menuElementGroup.Count - 1);
  369. _menuElementGroup.Insert(0, _tran);
  370. //数据脚本往右串位
  371. var _icon = _elementDataGroup[_elementDataGroup.Count - 1];
  372. if (_elementDataGroup[0].DataConfig != null && _elementDataGroup[_elementDataGroup.Count - 1].DataConfig != null)
  373. {
  374. _icon.DataConfig = _elementDataGroup[0].DataConfig.PreNode;
  375. }
  376. _elementDataGroup.RemoveAt(_elementDataGroup.Count - 1);
  377. _elementDataGroup.Insert(0, _icon);
  378. _icon.RefreshData();
  379. //刷新记录物体的缩放值和索引值
  380. RecordValue();
  381. }
  382. void RearrangeElements(Transform tran)// 回调方法:当删除完元素时,后续元素所在的游戏对象和数据往前移位
  383. {
  384. int index = -1;
  385. for (int i = 0; i < _menuElementGroup.Count; i++)//求出删除元素所在的位置
  386. {
  387. if (_menuElementGroup[i] == tran)
  388. {
  389. index = i;
  390. break;
  391. }
  392. }
  393. //所删除元素的游戏对象位置变换到最后一个定位点(等待向前移位)
  394. var _tempElement = _menuElementGroup[index];
  395. _tempElement.position = _bezierPointGroup[_menuElementGroup.Count];
  396. _menuElementGroup.RemoveAt(index);
  397. _menuElementGroup.Add(_tempElement);
  398. //游戏数据向前移位,赋予给所删除的游戏对象
  399. var _tempData = _elementDataGroup[index];
  400. _elementDataGroup.RemoveAt(index);
  401. _elementDataGroup.Add(_tempData);
  402. if (_elementDataGroup[_elementDataGroup.Count - 2].DataConfig.NextNode != ConfigModel.Instance.GetFirstMenuItem())
  403. {
  404. _elementDataGroup[_elementDataGroup.Count - 1].DataConfig = _elementDataGroup[_elementDataGroup.Count - 2].DataConfig.NextNode;
  405. }
  406. else
  407. {
  408. Debug.LogError("这个节点的下一个节点元素为头节点,出错了!");
  409. }
  410. //游戏对象向前移位
  411. for (int i = index; i < _menuElementGroup.Count; i++)
  412. {
  413. _menuElementGroup[i].DOLocalMove(_bezierPointGroup[i], 0.2f);
  414. if (i == _preMidIndex)
  415. {
  416. _menuElementGroup[_preMidIndex].DOScale(_normalScale, 0.2f);
  417. _menuElementGroup[_middleIndex].DOScale(_targetScale, 0.2f);
  418. }
  419. else if (i == _middleIndex)
  420. {
  421. _menuElementGroup[_middleIndex].DOScale(_targetScale, 0.2f);
  422. _menuElementGroup[_menuElementGroup.Count - 1].localScale = _normalScale;
  423. }
  424. }
  425. //刷新记录物体的缩放值和索引值
  426. RecordValue();
  427. }
  428. /// <summary>
  429. /// 刷新记录物体的缩放值和索引值
  430. /// </summary>
  431. void RecordValue()
  432. {
  433. for (int i = 0; i < _elementDataGroup.Count; i++)
  434. {
  435. _elementDataGroup[i].RoomIndex = i;
  436. if (i == _middleIndex)
  437. {
  438. _elementDataGroup[i].initAV3 = _targetScale;
  439. }
  440. else
  441. {
  442. _elementDataGroup[i].initAV3 = _normalScale;
  443. }
  444. }
  445. }
  446. /// <summary>
  447. /// 显示房间游戏对象动画
  448. /// </summary>
  449. public void ShowItemEffect()
  450. {
  451. for (int i = 0; i < _elementDataGroup.Count; i++)
  452. {
  453. _elementDataGroup[i].ShowItem();
  454. }
  455. }
  456. /// <summary>
  457. /// 隐藏房间游戏对象动画
  458. /// </summary>
  459. public void HideItemEffect()
  460. {
  461. for (int i = 0; i < _elementDataGroup.Count; i++)
  462. {
  463. _elementDataGroup[i].HideItem();
  464. }
  465. }
  466. /// <summary>
  467. /// 刷新删除按钮的显示状态
  468. /// </summary>
  469. public void RefreshDteBtn()
  470. {
  471. for (int i = 0; i < _elementDataGroup.Count; i++)
  472. {
  473. _elementDataGroup[i].ChangeDteBtn();
  474. }
  475. }
  476. /// <summary>
  477. /// 当加入房间失败时,取消事件监听
  478. /// </summary>
  479. public void RefeshisClick()
  480. {
  481. for (int i = 0; i < _elementDataGroup.Count; i++)
  482. {
  483. _elementDataGroup[i].Refeshbool();
  484. }
  485. }
  486. /// <summary>
  487. /// 刷新数据
  488. /// </summary>
  489. public void UpdateData()
  490. {
  491. for (int i = 0; i < _elementDataGroup.Count; i++)
  492. {
  493. _elementDataGroup[i].RefreshData();
  494. }
  495. }
  496. /// <summary>
  497. /// 游戏对象是否处于拖拽状态
  498. /// </summary>
  499. /// <param name="isDrag"></param>
  500. private void IsDrag(bool isDrag)
  501. {
  502. for (int i = 0; i < _elementDataGroup.Count; i++)
  503. {
  504. _elementDataGroup[i]._isDrag = isDrag;
  505. }
  506. }
  507. void OnDestroy()
  508. {
  509. EventTool._rearrange -= RearrangeElements;
  510. }
  511. }