UIManager.cs 40 KB

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