UIManager.cs 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Reflection;
  5. using Fort23.Core;
  6. using Fort23.UTool;
  7. using UnityEngine;
  8. using UnityEngine.Events;
  9. using UnityEngine.EventSystems;
  10. using UnityEngine.UI;
  11. using Utility;
  12. using EventSystem = UnityEngine.EventSystems.EventSystem;
  13. namespace Fort23.Mono
  14. {
  15. public enum UILayer
  16. {
  17. Bottom, //最底层UI(主界面)
  18. Middle, //中间UI(各种Panel)
  19. Top, //最顶层UI节点
  20. Loading, // 加载界面
  21. }
  22. /// <summary>
  23. /// 销毁类型
  24. /// </summary>
  25. public enum UIDestroyType
  26. {
  27. DelayDestroy,
  28. ImmediatelyDestroy,
  29. NotDestroy,
  30. }
  31. /// <summary>
  32. /// 如果有同时弹出多个UI界面冲突的地方,推荐用async/await的方法一次操作,这样就不用单独去为了适配打开多个UI的需求而改动太多的代码(主要是这样改动会牺牲大部分情况下的性能)
  33. /// </summary>
  34. public class UIManager : Entity
  35. {
  36. public static UIManager Instance { get; set; }
  37. // public RectTransform UIRootTransform;
  38. public Vector2 sizeDelta;
  39. public EventSystem current;
  40. /// <summary>
  41. /// 父节点(InitClip)
  42. /// </summary>
  43. private Transform _parent;
  44. /// <summary>
  45. /// 所有展示BG的ui
  46. /// </summary>
  47. public List<UIPanel> AllShowBGUIs = new List<UIPanel>();
  48. /// <summary>
  49. /// 顶部ui列表 可以通过导航栏跳转的界面
  50. /// </summary>
  51. public List<UIPanel> TopUIPanels = new List<UIPanel>();
  52. /// <summary>
  53. /// 所有top层ui
  54. /// </summary>
  55. public List<UIPanel> NoFocusTopUIPanels = new List<UIPanel>();
  56. /// <summary>
  57. /// 预先定好的几个层,数量和UILayer对应
  58. /// </summary>
  59. // public Canvas[] canvases = new Canvas[3];
  60. public Camera UICamera;
  61. public Canvas Canvas;
  62. public Transform[] UILayers = new Transform[4];
  63. /// <summary>
  64. /// 当前打开的ui
  65. /// </summary>
  66. public UIPanel currOpenPanel;
  67. /// <summary>
  68. /// UI布局大小
  69. /// </summary>
  70. private Vector2 UIScale;
  71. private BetterList<UIPanel> _lastShowPanel = new BetterList<UIPanel>();
  72. private Graphic currDeapthGraphic;
  73. /// <summary>
  74. /// 需要销毁的UI
  75. /// </summary>
  76. public Dictionary<string, UIPanel> destroyUI = new Dictionary<string, UIPanel>();
  77. public CustomCameraStack CurrCustomCameraStack
  78. {
  79. get { return _currCustomCameraStack; }
  80. set
  81. {
  82. _currCustomCameraStack = value;
  83. RefreshFull();
  84. }
  85. }
  86. public void CleanLastShowPanel()
  87. {
  88. _lastShowPanel.Clear();
  89. }
  90. private CustomCameraStack _currCustomCameraStack;
  91. /// <summary>
  92. /// 屏蔽字库
  93. /// </summary>
  94. public string MaskWordData;
  95. /// <summary>
  96. /// 展示文字提示长度
  97. /// </summary>
  98. public int ShowTextCount;
  99. /// <summary>
  100. /// 展示文字提示最大长度
  101. /// </summary>
  102. public int ShowTextMaxCount = 1;
  103. /// <summary>
  104. /// 上一个界面
  105. /// </summary>
  106. public UIPanel LastUIPanel;
  107. [CustomMethod(CustomMethodType.Awake)]
  108. public async void Awake(Transform parent)
  109. {
  110. Instance = this;
  111. _parent = parent;
  112. }
  113. public async CTask InitUI()
  114. {
  115. if (Canvas == null)
  116. {
  117. AssetHandle assetBundle =
  118. await AssetBundleLoadManager.Instance.LoadAssetAsyncTask<GameObject>("Canvas.prefab");
  119. GameObject prefab = assetBundle.AssetObject<GameObject>();
  120. // UIRootTransform = prefab.GetComponent<RectTransform>();
  121. sizeDelta = prefab.GetComponent<RectTransform>().sizeDelta;
  122. // if (1.0f * Screen.width / Screen.height < 1334f / 750f)
  123. // {
  124. // sizeDelta = new Vector2(1334, 750);
  125. // }
  126. // return;
  127. Canvas = prefab.GetComponent<Canvas>();
  128. prefab.transform.SetParent(_parent);
  129. UILayers[0] = prefab.transform.GetChild(0);
  130. UILayers[1] = prefab.transform.GetChild(1);
  131. UILayers[2] = prefab.transform.GetChild(2);
  132. UILayers[3] = prefab.transform.GetChild(3);
  133. UICamera = prefab.transform.GetComponentInChildren<Camera>();
  134. }
  135. // if (MaskWordData == null)
  136. // {
  137. // AssetHandle assetBundle =
  138. // await AssetBundleLoadManager.Instance.LoadAssetAsyncTask<TextAsset>("guofu.txt");
  139. // TextAsset text = assetBundle.AssetObject<TextAsset>();
  140. // MaskWordData = text.text;
  141. // assetBundle.Release();
  142. // }
  143. }
  144. public RectTransform GetLayer(UILayer layer)
  145. {
  146. try
  147. {
  148. int index = (int)layer;
  149. return (RectTransform)UILayers[index];
  150. }
  151. catch (Exception e)
  152. {
  153. Console.WriteLine(e);
  154. throw;
  155. }
  156. }
  157. public void SetEventSystemEnable(bool value)
  158. {
  159. // current.isClose = !value;
  160. }
  161. [CustomMethod(CustomMethodType.Update)]
  162. public async void Update()
  163. {
  164. if (currDeapthGraphic != null)
  165. {
  166. UGUIIamgeTool.minDepth = currDeapthGraphic.canvasRenderer.absoluteDepth;
  167. UGUIIamgeTool.renderOrder = currDeapthGraphic.canvas.renderOrder;
  168. }
  169. else
  170. {
  171. UGUIIamgeTool.minDepth = -1;
  172. UGUIIamgeTool.renderOrder = 0;
  173. }
  174. }
  175. public Vector2 WorldToUIWorld(Vector3 worldPos)
  176. {
  177. // Vector3 worldPos = harmReturnInfo.target.combatHeroEntity.combatHeroGameObject.hpTransform.position;
  178. Vector3 p = CurrCustomCameraStack.camera.WorldToScreenPoint(worldPos);
  179. Vector3 p2 = UICamera.ScreenToWorldPoint(p);
  180. return p2;
  181. }
  182. public Vector3 UIWorldToWorld(Vector3 worldPos)
  183. {
  184. // Vector3 worldPos = harmReturnInfo.target.combatHeroEntity.combatHeroGameObject.hpTransform.position;
  185. Vector3 p = UICamera.WorldToScreenPoint(worldPos);
  186. p.z = 20;
  187. Vector3 p2 = CurrCustomCameraStack.camera.ScreenToWorldPoint(p);
  188. return p2;
  189. }
  190. /// <summary>
  191. /// 隐藏当前全部的UIPanel(只是不显示,不是销毁) 排除不在隐藏之列的UI
  192. /// </summary>
  193. public void HindCurrAllShowPanel()
  194. {
  195. if (_lastShowPanel.Count > 0)
  196. {
  197. return;
  198. }
  199. _lastShowPanel.Clear();
  200. UIPanel[] allShowPanel = GetComponentAll<UIPanel>();
  201. for (int i = 0; i < allShowPanel.Length; i++)
  202. {
  203. if (allShowPanel[i].GObjectPoolInterface.activeSelf)
  204. {
  205. allShowPanel[i].GObjectPoolInterface.SetActive(false);
  206. _lastShowPanel.Add(allShowPanel[i]);
  207. }
  208. }
  209. }
  210. public async CTask ShowLastHindAllShowPanel()
  211. {
  212. for (int i = 0; i < _lastShowPanel.Count; i++)
  213. {
  214. if (_lastShowPanel[i].GObjectPoolInterface != null)
  215. {
  216. _lastShowPanel[i].GObjectPoolInterface.SetActive(true);
  217. }
  218. }
  219. if (_lastShowPanel.Contains(TopUIPanels[^1]))
  220. {
  221. _lastShowPanel.Clear();
  222. if (TopUIPanels[^1].isShow)
  223. {
  224. await TopUIPanels[^1].GetFocus();
  225. }
  226. }
  227. _lastShowPanel.Clear();
  228. }
  229. /// <summary>
  230. /// 用窗口打开某个界面(不可重复的预设,适用于一切带有关闭按钮的窗口UI)
  231. /// </summary>
  232. /// <param name="callback"> ui加载完成时的回调</param>
  233. /// <param name="layer"> ui需要显示在那一层,现在分为3层</param>
  234. /// <param name="isFocus"> 是否时需要获取焦点</param>
  235. /// <param name="uiData">打开UI时的透传数据</param>
  236. /// <param name="isShowBG">是否需要显示统一的背板</param>
  237. /// isFullUI 是否是全面屏UI
  238. /// <typeparam name="T"></typeparam>
  239. /// <returns></returns>
  240. public async CTask<T> LoadAndOpenPanel<T>(Action<T> callback, UILayer layer = UILayer.Middle,
  241. bool isFocus = true, object[] uiData = null, bool isShowBG = false, bool isFullUI = false,
  242. bool isActiveAnima = true)
  243. where T : UIPanel, new()
  244. {
  245. try
  246. {
  247. Type type;
  248. current = EventSystem.current;
  249. type = typeof(T);
  250. T uiPanel;
  251. // 获取UI绑定属性
  252. UIBindingAttribute uiBindingAttribute =
  253. type.GetCustomAttribute(typeof(UIBindingAttribute)) as UIBindingAttribute;
  254. if (uiBindingAttribute == null)
  255. {
  256. LogTool.Error($"类型{type.Name}没有添加UIBindingAttribute!");
  257. // ShowTips($"类型{buildType.Name}没有添加UIBindingAttribute!");
  258. return null;
  259. }
  260. // TDManager.Instance.PartCount(uiBindingAttribute.prefab);
  261. if (isFocus)
  262. {
  263. if (currOpenPanel != null)
  264. {
  265. if (currOpenPanel.isShow)
  266. {
  267. await currOpenPanel.LoseFocus();
  268. currOpenPanel = null;
  269. }
  270. else
  271. {
  272. currOpenPanel = null;
  273. }
  274. }
  275. }
  276. using (await CoroutineLockComponent.Instance.Wait(uiBindingAttribute.prefab))
  277. {
  278. if (current != null)
  279. {
  280. // current.isClose = true;
  281. }
  282. // 不可以重复打开Panel,但是可以把上一个拿来使用。
  283. if (!Components.ContainsKey(type))
  284. {
  285. GameObject gameObjectPool = await FetchUI(uiBindingAttribute, GetLayer(layer));
  286. if (gameObjectPool == null)
  287. return null;
  288. uiPanel = AddComponent<T, GameObject>(gameObjectPool, false);
  289. uiPanel.uiName = uiBindingAttribute.prefab;
  290. uiPanel.isFocus = isFocus;
  291. await uiPanel.SetUIGameObject(gameObjectPool);
  292. gameObjectPool.SetActive(false);
  293. }
  294. else
  295. {
  296. uiPanel = GetComponent<T>();
  297. if (uiPanel.DestroyTimerEntity != null)
  298. {
  299. TimerComponent.Instance.Remove(uiPanel.DestroyTimerEntity.ID);
  300. uiPanel.DestroyTimerEntity.Destroy();
  301. }
  302. destroyUI.Remove(uiBindingAttribute.prefab);
  303. }
  304. uiPanel.isFullUI = isFullUI;
  305. // 等待数据准备好才初始化UI
  306. if (await uiPanel.AsyncInit(uiData))
  307. {
  308. if (uiPanel.IsBreadcrumbBarPanel) //打开界面之前判断是否已经在堆栈当中 如果是就移除此界面往后的堆栈 保持他时在堆栈的最上方
  309. {
  310. if (TopUIPanels.Contains(uiPanel))
  311. {
  312. int index = TopUIPanels.IndexOf(uiPanel);
  313. int count = TopUIPanels.Count - index;
  314. for (int i = index + 1; i < TopUIPanels.Count; i++)
  315. {
  316. if (TopUIPanels[i].isShow)
  317. {
  318. HideUIUIPanel(TopUIPanels[i]);
  319. }
  320. }
  321. TopUIPanels.RemoveRange(index, count);
  322. }
  323. }
  324. if (layer == UILayer.Top && !NoFocusTopUIPanels.Contains(uiPanel))
  325. NoFocusTopUIPanels.Add(uiPanel);
  326. uiPanel.isActiveAnima = isActiveAnima;
  327. await uiPanel.Open();
  328. if (current != null)
  329. {
  330. // current.isClose = false;
  331. }
  332. await TimerComponent.Instance.WaitAsync(1); //等待一帧 让渲染完成后在执行完成逻辑
  333. callback?.Invoke(uiPanel);
  334. RefreshFull();
  335. if (isShowBG)
  336. {
  337. uiPanel.IsShowCustomBGPanel = true;
  338. if (!AllShowBGUIs.Contains(uiPanel))
  339. AllShowBGUIs.Add(uiPanel);
  340. // await GetBackgroundPanel(uiPanel);
  341. }
  342. return uiPanel;
  343. }
  344. else
  345. {
  346. if (current != null)
  347. {
  348. // current.isClose = false;
  349. }
  350. // 界面数据没准备好,因此本次操作将被销毁
  351. LogTool.Log($"数据准备失败,请检查{type.Name}的Init方法中的逻辑");
  352. // ShowTips($"数据准备失败,请检查{buildType.Name}的Init方法中的逻辑");
  353. uiPanel.Close();
  354. return null;
  355. }
  356. }
  357. }
  358. catch (Exception e)
  359. {
  360. if (current != null)
  361. {
  362. // current.isClose = false;
  363. }
  364. LogTool.Error(typeof(T).ToString() + " " + e.Message);
  365. LogTool.Exception(e);
  366. return null;
  367. }
  368. }
  369. public void RefreshFull()
  370. {
  371. Graphic min = null;
  372. UIPanel[] allPanel = GetComponentAll<UIPanel>();
  373. if (allPanel == null)
  374. {
  375. return;
  376. }
  377. for (int i = 0; i < allPanel.Length; i++)
  378. {
  379. UIPanel panel = allPanel[i];
  380. if (panel != null && panel.minDepth != null && !panel.IsClose && panel.isShow &&
  381. panel.GObjectPoolInterface.activeSelf && panel.isFullUI)
  382. {
  383. if (min == null)
  384. {
  385. min = panel.minDepth;
  386. continue;
  387. }
  388. if (min != null && panel.minDepth.canvasRenderer.absoluteDepth > min.canvasRenderer.absoluteDepth)
  389. {
  390. min = panel.minDepth;
  391. }
  392. }
  393. }
  394. if (min != null)
  395. {
  396. UGUIIamgeTool.minDepth = min.canvasRenderer.absoluteDepth;
  397. UGUIIamgeTool.renderOrder = min.canvas.renderOrder;
  398. }
  399. else
  400. {
  401. UGUIIamgeTool.minDepth = -1;
  402. UGUIIamgeTool.renderOrder = 0;
  403. }
  404. currDeapthGraphic = min;
  405. if (_currCustomCameraStack == null)
  406. {
  407. // URPTool.Instance.IsNotRenderMainCamera = false;
  408. return;
  409. }
  410. bool isFull = false;
  411. for (int i = 0; i < allPanel.Length; i++)
  412. {
  413. UIPanel panel = allPanel[i];
  414. if (panel != null && !panel.IsClose && panel.isShow && panel.GObjectPoolInterface.activeSelf &&
  415. panel.isFullUI)
  416. {
  417. isFull = true;
  418. break;
  419. }
  420. }
  421. // URPTool.Instance.IsNotRenderMainCamera = isFull;
  422. }
  423. /// <summary>
  424. /// 关闭最上层堆栈的uiPanel
  425. /// </summary>
  426. public void HideTopPanel()
  427. {
  428. if (TopUIPanels.Count > 0)
  429. {
  430. if (TopUIPanels[^1] != null)
  431. {
  432. TopUIPanels[^1].Hide();
  433. }
  434. }
  435. }
  436. /// <summary>
  437. /// 加入topUI堆栈
  438. /// </summary>
  439. public void AddTopStack(UIPanel uiPanel)
  440. {
  441. if (!TopUIPanels.Contains(uiPanel))
  442. {
  443. TopUIPanels.Add(uiPanel);
  444. uiPanel.IsBreadcrumbBarPanel = true;
  445. }
  446. }
  447. /// <summary>
  448. /// 弹出堆栈
  449. /// </summary>
  450. public void RemoveTopStack(UIPanel uiPanel)
  451. {
  452. if (TopUIPanels.Contains(uiPanel))
  453. {
  454. TopUIPanels.Remove(uiPanel);
  455. }
  456. }
  457. /// <summary>
  458. /// 关闭最顶部的ui
  459. /// </summary>
  460. public UIPanel CloseTopUI()
  461. {
  462. if (TopUIPanels.Count > 0)
  463. {
  464. UIPanel uiPanel = TopUIPanels[^1];
  465. uiPanel.IsBreadcrumbBarPanel = false; //返回关闭的时候将它设置为false 表示可以被删除
  466. HideUIUIPanel(uiPanel);
  467. return uiPanel;
  468. }
  469. return null;
  470. }
  471. /// <summary>
  472. /// 隐藏所有堆栈的ui
  473. /// </summary>
  474. public void HideAllStackUI()
  475. {
  476. for (var i = 0; i < TopUIPanels.Count; i++)
  477. {
  478. HideUIUIPanel(TopUIPanels[i]);
  479. }
  480. }
  481. /// <summary>
  482. /// 打开堆栈最上方
  483. /// </summary>
  484. public async void OpenTopUI()
  485. {
  486. if (TopUIPanels.Count > 0)
  487. {
  488. UIPanel uiPanel = TopUIPanels[^1];
  489. await uiPanel.Show();
  490. }
  491. }
  492. public Material UISpineMaterial;
  493. /// <summary>
  494. /// 创建组件 可重复使用的(这个方法,只是提供gameObject和脚本的绑定,没有经过池子,因此gameObject依然还挂在原来的预设上)
  495. /// </summary>
  496. /// <param name="root">父节点</param>
  497. /// <typeparam name="T"></typeparam>
  498. /// <returns></returns>
  499. public async CTask<T> CreateGComponentForObject<T>(GameObject gameObject, Action<T> callback,
  500. RectTransform root = null,
  501. object[] uiData = null, bool isInstance = false, string poolName = null, bool isActiveAnima = true)
  502. where T : UIComponent, new()
  503. {
  504. try
  505. {
  506. Type type = typeof(T);
  507. // // 获取UI绑定属性
  508. UIBindingAttribute uiBindingAttribute =
  509. type.GetCustomAttribute(typeof(UIBindingAttribute)) as UIBindingAttribute;
  510. if (uiBindingAttribute == null)
  511. {
  512. LogTool.Error($"类型{type.Name}没有添加UIBindingAttribute!");
  513. return null;
  514. }
  515. GameObject newObject = gameObject;
  516. T gameObjectPool = null;
  517. if (isInstance)
  518. {
  519. string newPoolName = string.IsNullOrEmpty(poolName) ? gameObject.name + ".prefab" : poolName;
  520. gameObjectPool = GObjectPool.Instance.FetchAsyncForGameObject<T>(gameObject, newPoolName);
  521. newObject = gameObjectPool.own;
  522. // gameObjectPool.own.transform.SetParent(root);
  523. // newObject = GameObject.Instantiate(gameObject);
  524. }
  525. else
  526. {
  527. gameObjectPool = Activator.CreateInstance<T>();
  528. // UIEventMono uiEventMono = newObject.GetComponent<UIEventMono>();
  529. // if (uiEventMono != null)
  530. // {
  531. // uiEventMono.Destory();
  532. // }
  533. }
  534. if (gameObjectPool == null)
  535. {
  536. return null;
  537. }
  538. gameObjectPool.SetGameObject(newObject);
  539. return await SetGComponentInfo(gameObjectPool, callback, root, uiData, isActiveAnima);
  540. }
  541. catch (Exception e)
  542. {
  543. LogTool.Exception(e);
  544. return null;
  545. }
  546. }
  547. /// <summary>
  548. /// 创建组件 可重复使用的
  549. /// </summary>
  550. /// <param name="root">父节点</param>
  551. /// <typeparam name="T"></typeparam>
  552. /// <returns></returns>
  553. public async CTask<T> CreateGComponent<T>(Action<T> callback, RectTransform root = null, object[] uiData = null,
  554. string poolName = null, Clock clock = null, bool isActive = true, bool isActiveAnima = true)
  555. where T : UIComponent, new()
  556. {
  557. try
  558. {
  559. Type type = typeof(T);
  560. // 获取UI绑定属性
  561. UIBindingAttribute uiBindingAttribute =
  562. type.GetCustomAttribute(typeof(UIBindingAttribute)) as UIBindingAttribute;
  563. if (uiBindingAttribute == null)
  564. {
  565. LogTool.Error($"类型{type.Name}没有添加UIBindingAttribute!");
  566. return null;
  567. }
  568. T gameObjectPool = await GObjectPool.Instance.FetchAsync<T>(uiBindingAttribute.prefab + ".prefab",
  569. poolName: poolName, clock: clock);
  570. if (gameObjectPool == null)
  571. {
  572. return default;
  573. }
  574. T cTask = await SetGComponentInfo(gameObjectPool, callback, root, uiData, isActiveAnima);
  575. gameObjectPool.own.SetActive(isActive);
  576. return cTask;
  577. }
  578. catch (Exception e)
  579. {
  580. LogTool.Exception(e);
  581. return null;
  582. }
  583. }
  584. public void DormancyAllGComponent<T>(string poolName)
  585. {
  586. GObjectPool.Instance.DormancyPool<T>(poolName);
  587. }
  588. public void DormancyAllGComponent<T>()
  589. {
  590. Type type = typeof(T);
  591. // 获取UI绑定属性
  592. UIBindingAttribute uiBindingAttribute =
  593. type.GetCustomAttribute(typeof(UIBindingAttribute)) as UIBindingAttribute;
  594. if (uiBindingAttribute == null)
  595. {
  596. LogTool.Error($"类型{type.Name}没有添加UIBindingAttribute!");
  597. return;
  598. }
  599. string poolName = uiBindingAttribute.prefab + ".prefab";
  600. GObjectPool.Instance.DormancyPool<T>(poolName);
  601. }
  602. public void DormancyGComponent(IGObjectPoolInterface poolInterface)
  603. {
  604. GObjectPool.Instance.Recycle(poolInterface);
  605. }
  606. private async CTask<T> SetGComponentInfo<T>(T gameObjectPool, Action<T> callback, RectTransform root = null,
  607. object[] uiData = null, bool isActiveAnima = true) where T : UIComponent, new()
  608. {
  609. try
  610. {
  611. if (gameObjectPool == null)
  612. return null;
  613. RectTransform rectTransform = gameObjectPool.own.GetComponent<RectTransform>();
  614. rectTransform.SetParent(root);
  615. rectTransform.SetAsLastSibling();
  616. rectTransform.anchoredPosition = Vector3.zero;
  617. rectTransform.localScale = Vector3.one;
  618. rectTransform.localEulerAngles = Vector3.zero;
  619. gameObjectPool.isActiveAnima = isActiveAnima;
  620. await gameObjectPool.SetUIGameObject(gameObjectPool.own);
  621. if (await gameObjectPool.AsyncInit(uiData))
  622. {
  623. gameObjectPool.DelEvent(); //为了以防万一,先移除内部事件
  624. gameObjectPool.AddEvent();
  625. gameObjectPool.Show();
  626. // await TimerComponent.Instance.WaitAsync(1);
  627. callback?.Invoke(gameObjectPool);
  628. return gameObjectPool;
  629. }
  630. else
  631. {
  632. LogTool.Log($"数据准备失败,请检查{gameObjectPool.own.name}的Init方法中的逻辑");
  633. gameObjectPool.Close();
  634. return null;
  635. }
  636. }
  637. catch (Exception e)
  638. {
  639. LogTool.Exception(e);
  640. return null;
  641. }
  642. }
  643. public void HideUIUIPanel<T>(UIDestroyType uiDestroyType = UIDestroyType.DelayDestroy,
  644. bool isBreadcrumbBarPanel = false) where T : UIPanel
  645. {
  646. UIPanel uiPanel = GetComponent<T>();
  647. if (uiPanel != null)
  648. {
  649. HideUIUIPanel(uiPanel, uiDestroyType, isBreadcrumbBarPanel);
  650. }
  651. }
  652. /// <summary>
  653. /// 关闭UI
  654. /// </summary>
  655. /// <param name="uiPanel"></param>
  656. public void HideUIUIPanel(UIPanel uiPanel, UIDestroyType uiDestroyType = UIDestroyType.DelayDestroy,
  657. bool isBreadcrumbBarPanel = false)
  658. {
  659. if (uiPanel == null)
  660. return;
  661. if (uiPanel.IsClose)
  662. {
  663. return;
  664. }
  665. if (uiPanel.IsShowCustomBGPanel) //如果是开了背景的界面关闭的时候将背景一起关闭
  666. {
  667. // HideUIUIPanel(GetComponent<BackgroundPanel>(), UIDestroyType.NotDestroy);
  668. }
  669. if (!NoFocusTopUIPanels.Contains(uiPanel)) NoFocusTopUIPanels.Remove(uiPanel);
  670. uiPanel.Close();
  671. RemoveTopStack(uiPanel); //如果要删除界面判断是否在跳转堆栈 如果是则移除堆栈
  672. switch (uiDestroyType)
  673. {
  674. case UIDestroyType.DelayDestroy:
  675. if (!destroyUI.ContainsKey(uiPanel.uiName))
  676. {
  677. uiPanel.AddDelayDestroy();
  678. destroyUI.Add(uiPanel.uiName, uiPanel);
  679. }
  680. break;
  681. case UIDestroyType.ImmediatelyDestroy:
  682. DestroyUIPanel(uiPanel);
  683. break;
  684. case UIDestroyType.NotDestroy:
  685. break;
  686. }
  687. RefreshFull();
  688. }
  689. public void DestroyUIPanel(UIPanel uiPanel)
  690. {
  691. if (uiPanel.Parent == null)
  692. {
  693. return;
  694. }
  695. destroyUI.Remove(uiPanel.uiName);
  696. uiPanel.Parent.RemoveComponent(uiPanel);
  697. }
  698. private async CTask<GameObject> FetchUI(UIBindingAttribute uiBindingAttribute, Transform tf)
  699. {
  700. GameObjectPool assetBundle =
  701. await GObjectPool.Instance.FetchAsync<GameObjectPool>(uiBindingAttribute.prefab + ".prefab");
  702. GameObject gameObject = assetBundle.own;
  703. if (gameObject == null)
  704. return null;
  705. gameObject.transform.SetParent(tf);
  706. gameObject.transform.SetAsLastSibling();
  707. gameObject.transform.localScale = Vector3.one;
  708. return gameObject;
  709. }
  710. /// <summary>
  711. /// <summary>
  712. /// 屏幕转ui坐标
  713. /// </summary>
  714. /// <param name="rPosition"></param>
  715. /// <returns></returns>
  716. public Vector3 ScreenPointToLocalPointInRectangle(Vector2 rPosition)
  717. {
  718. RectTransform rRect = Instance.Canvas.GetComponent<RectTransform>();
  719. Vector3 lPosition = Instance.UICamera.ScreenToWorldPoint(rPosition);
  720. Vector4 l4Position =
  721. new Vector4(lPosition.x, lPosition.y, lPosition.z, 1); // unity默认转化v4,w是0,不会计算x和y的rect的偏移,所以这里要自己搞一下。
  722. lPosition = rRect.worldToLocalMatrix * l4Position;
  723. lPosition.z = 0;
  724. return lPosition;
  725. }
  726. /// <summary>
  727. /// ui组件添加自定义事件
  728. /// </summary>
  729. /// <param name="obj"></param>
  730. /// <param name="eventTriggerType"></param>
  731. /// <param name="callback"></param>
  732. public void AddCustomEventListener(UIBehaviour control, EventTriggerType type,
  733. UnityAction<BaseEventData> callback)
  734. {
  735. EventTrigger trigger = control.GetComponent<EventTrigger>();
  736. if (trigger == null)
  737. {
  738. trigger = control.gameObject.AddComponent<EventTrigger>();
  739. }
  740. EventTrigger.Entry entry = new EventTrigger.Entry();
  741. entry.eventID = type;
  742. entry.callback.AddListener(callback);
  743. trigger.triggers.Add(entry);
  744. }
  745. public override void Dispose()
  746. {
  747. base.Dispose();
  748. UGUIIamgeTool.renderOrder = 0;
  749. }
  750. /// <summary>
  751. /// 移除自定义事件
  752. /// </summary>
  753. /// <param name="control"></param>
  754. /// <param name="type"></param>
  755. /// <param name="callback"></param>
  756. public void RemoveCustomEventListener(UIBehaviour control, EventTriggerType type,
  757. UnityAction<BaseEventData> callback)
  758. {
  759. EventTrigger trigger = control.GetComponent<EventTrigger>();
  760. if (trigger == null)
  761. {
  762. trigger = control.gameObject.AddComponent<EventTrigger>();
  763. }
  764. for (int i = 0; i < trigger.triggers.Count; i++)
  765. {
  766. if (trigger.triggers[i].eventID == type)
  767. {
  768. trigger.triggers[i].callback.RemoveListener(callback);
  769. // Debug.Log("移除事件");
  770. }
  771. }
  772. }
  773. public GameObject GetOverUI(GameObject canvas)
  774. {
  775. PointerEventData pointerEventData = new PointerEventData(UnityEngine.EventSystems.EventSystem.current);
  776. pointerEventData.position = Input.mousePosition;
  777. GraphicRaycaster gr = canvas.GetComponent<GraphicRaycaster>();
  778. List<RaycastResult> results = new List<RaycastResult>();
  779. gr.Raycast(pointerEventData, results);
  780. if (results.Count != 0)
  781. {
  782. return results[0].gameObject;
  783. }
  784. return null;
  785. }
  786. public Vector2 SetPopTipPosition(Vector3 pos, Vector2 sizeDelta)
  787. {
  788. Vector2 xy = sizeDelta / 2;
  789. Vector2 vector2 = Canvas.GetComponent<RectTransform>().sizeDelta;
  790. float x = vector2.x / 2;
  791. float y = vector2.y / 2;
  792. if (pos.x + xy.x >= x)
  793. {
  794. pos.x = x - xy.x;
  795. }
  796. else if (pos.x - xy.x <= -x)
  797. {
  798. pos.x = -x + xy.x;
  799. }
  800. if (pos.y + xy.y >= y)
  801. {
  802. pos.y = y - xy.y;
  803. }
  804. else if (pos.y - xy.y <= -y)
  805. {
  806. pos.y = -y + xy.y;
  807. }
  808. return pos;
  809. }
  810. /// <summary>
  811. /// 设置数值文本的显示
  812. /// </summary>
  813. /// <param name="number"></param>
  814. /// <returns></returns>
  815. public string SetNumberTextShow(long number)
  816. {
  817. if (number > 1000000000000)
  818. {
  819. float n = number / 1000000000000f;
  820. string str = "";
  821. if (n > 100)
  822. {
  823. int n1 = (int)(n * 10);
  824. n = n1 / 10f;
  825. str = n.ToString(CultureInfo.InvariantCulture);
  826. }
  827. else
  828. {
  829. int n1 = (int)(n * 100);
  830. n = n1 / 100f;
  831. str = n.ToString(CultureInfo.InvariantCulture);
  832. }
  833. return str + "t";
  834. }
  835. if (number > 1000000000)
  836. {
  837. float n = number / 1000000000f;
  838. string str = "";
  839. if (n > 100)
  840. {
  841. int n1 = (int)(n * 10);
  842. n = n1 / 10f;
  843. str = n.ToString(CultureInfo.InvariantCulture);
  844. }
  845. else
  846. {
  847. int n1 = (int)(n * 100);
  848. n = n1 / 100f;
  849. str = n.ToString(CultureInfo.InvariantCulture);
  850. }
  851. return str + "b";
  852. }
  853. if (number > 1000000)
  854. {
  855. float n = number / 1000000f;
  856. string str = "";
  857. if (n > 100)
  858. {
  859. int n1 = (int)(n * 10);
  860. n = n1 / 10f;
  861. str = n.ToString(CultureInfo.InvariantCulture);
  862. }
  863. else
  864. {
  865. int n1 = (int)(n * 100);
  866. n = n1 / 100f;
  867. str = n.ToString(CultureInfo.InvariantCulture);
  868. }
  869. return str + "m";
  870. }
  871. if (number > 10000)
  872. {
  873. float n = number / 1000f;
  874. string str = "";
  875. if (n > 100)
  876. {
  877. int n1 = (int)(n * 10);
  878. n = n1 / 10f;
  879. str = n.ToString(CultureInfo.InvariantCulture);
  880. }
  881. else
  882. {
  883. int n1 = (int)(n * 100);
  884. n = n1 / 100f;
  885. str = n.ToString(CultureInfo.InvariantCulture);
  886. }
  887. return str + "k";
  888. }
  889. return number.ToString();
  890. }
  891. /// <summary>
  892. /// 每日是否提醒标记
  893. /// </summary>
  894. public enum ToDayDontShowTag
  895. {
  896. MjToDiamond = 0,
  897. DiamondUsed, //钻石使用判断
  898. RelicIntensify,
  899. /// <summary>
  900. /// 经验转换
  901. /// </summary>
  902. EXConvert
  903. }
  904. //判断鼠标是否点在ui上
  905. public bool IsPointerOverGameObject(Vector2 mousePosition)
  906. {
  907. PointerEventData eventData = new PointerEventData(EventSystem.current);
  908. eventData.position = mousePosition;
  909. List<RaycastResult> raycastResults = new List<RaycastResult>();
  910. //发射射线判断是否点到ui
  911. EventSystem.current.RaycastAll(eventData, raycastResults);
  912. if (raycastResults.Count > 0)
  913. {
  914. return true;
  915. }
  916. else
  917. {
  918. return false;
  919. }
  920. }
  921. }
  922. }