UIManager.cs 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189
  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. if (uiPanel.isAddStack)
  359. {
  360. EventManager.Instance.Dispatch(CustomEventType.CancelEvent,null);
  361. }
  362. uiPanel.isFullUI = isFullUI;
  363. // 等待数据准备好才初始化UI
  364. if (await uiPanel.AsyncInit(uiData))
  365. {
  366. if (uiPanel.isAddStack)
  367. {
  368. if (uiPanel.IsBreadcrumbBarPanel) //打开界面之前判断是否已经在堆栈当中 如果是就移除此界面往后的堆栈 保持他时在堆栈的最上方
  369. {
  370. if (TopUIPanels.Contains(uiPanel))
  371. {
  372. int index = TopUIPanels.IndexOf(uiPanel);
  373. int count = TopUIPanels.Count - index;
  374. for (int i = index + 1; i < TopUIPanels.Count; i++)
  375. {
  376. if (TopUIPanels[i].isShow)
  377. {
  378. HideUIUIPanel(TopUIPanels[i]);
  379. }
  380. }
  381. TopUIPanels.RemoveRange(index, count);
  382. }
  383. }
  384. AddTopStack(uiPanel);
  385. }
  386. if (layer == UILayer.Top && !NoFocusTopUIPanels.Contains(uiPanel))
  387. NoFocusTopUIPanels.Add(uiPanel);
  388. uiPanel.isActiveAnima = isActiveAnima;
  389. await uiPanel.Open();
  390. AudioManager.Instance.PlayAudio("openui.wav");
  391. // if (currOpenPanel.add)
  392. // {
  393. // currOpenPanel = uiPanel;
  394. // }
  395. if (current != null)
  396. {
  397. // current.isClose = false;
  398. }
  399. await TimerComponent.Instance.WaitAsync(1); //等待一帧 让渲染完成后在执行完成逻辑
  400. callback?.Invoke(uiPanel);
  401. RefreshFull();
  402. if (isShowBG)
  403. {
  404. uiPanel.IsShowCustomBGPanel = true;
  405. if (!AllShowBGUIs.Contains(uiPanel))
  406. AllShowBGUIs.Add(uiPanel);
  407. // await GetBackgroundPanel(uiPanel);
  408. }
  409. return uiPanel;
  410. }
  411. else
  412. {
  413. if (current != null)
  414. {
  415. // current.isClose = false;
  416. }
  417. // 界面数据没准备好,因此本次操作将被销毁
  418. LogTool.Log($"数据准备失败,请检查{type.Name}的Init方法中的逻辑");
  419. // ShowTips($"数据准备失败,请检查{buildType.Name}的Init方法中的逻辑");
  420. uiPanel.Close();
  421. return null;
  422. }
  423. }
  424. }
  425. catch (Exception e)
  426. {
  427. if (current != null)
  428. {
  429. // current.isClose = false;
  430. }
  431. LogTool.Error(typeof(T).ToString() + " " + e.Message);
  432. LogTool.Exception(e);
  433. return null;
  434. }
  435. }
  436. public void RefreshFull()
  437. {
  438. Graphic min = null;
  439. UIPanel[] allPanel = GetComponentAll<UIPanel>();
  440. bool isFull = false;
  441. if (allPanel != null)
  442. {
  443. for (int i = 0; i < allPanel.Length; i++)
  444. {
  445. UIPanel panel = allPanel[i];
  446. if (panel != null && panel.minDepth != null && !panel.IsClose && panel.isShow &&
  447. panel.GObjectPoolInterface.activeSelf && panel.isFullUI)
  448. {
  449. if (min == null)
  450. {
  451. min = panel.minDepth;
  452. continue;
  453. }
  454. if (min != null && panel.minDepth.canvasRenderer.absoluteDepth >
  455. min.canvasRenderer.absoluteDepth)
  456. {
  457. min = panel.minDepth;
  458. }
  459. }
  460. }
  461. if (min != null)
  462. {
  463. UGUIIamgeTool.minDepth = min.canvasRenderer.absoluteDepth;
  464. UGUIIamgeTool.renderOrder = min.canvas.renderOrder;
  465. }
  466. else
  467. {
  468. UGUIIamgeTool.minDepth = -1;
  469. UGUIIamgeTool.renderOrder = 0;
  470. }
  471. currDeapthGraphic = min;
  472. for (int i = 0; i < allPanel.Length; i++)
  473. {
  474. UIPanel panel = allPanel[i];
  475. if (panel != null && !panel.IsClose && panel.isShow && panel.GObjectPoolInterface.activeSelf &&
  476. panel.isFullUI)
  477. {
  478. isFull = true;
  479. break;
  480. }
  481. }
  482. }
  483. RefreshFullEventData refreshFullEventData = RefreshFullEventData.Create();
  484. refreshFullEventData.isFullShow = isFull;
  485. EventManager.Instance.Dispatch(CustomEventType.RefreshFull, refreshFullEventData);
  486. if (_currCustomCameraStack == null)
  487. {
  488. // URPTool.Instance.IsNotRenderMainCamera = false;
  489. return;
  490. }
  491. // URPTool.Instance.IsNotRenderMainCamera = isFull;
  492. }
  493. /// <summary>
  494. /// 关闭最上层堆栈的uiPanel
  495. /// </summary>
  496. public void HideTopPanel()
  497. {
  498. if (TopUIPanels.Count > 0)
  499. {
  500. if (TopUIPanels[^1] != null)
  501. {
  502. TopUIPanels[^1].Hide();
  503. }
  504. }
  505. }
  506. /// <summary>
  507. /// 加入topUI堆栈
  508. /// </summary>
  509. public void AddTopStack(UIPanel uiPanel)
  510. {
  511. if (!TopUIPanels.Contains(uiPanel))
  512. {
  513. TopUIPanels.Add(uiPanel);
  514. uiPanel.IsBreadcrumbBarPanel = true;
  515. }
  516. }
  517. /// <summary>
  518. /// 弹出堆栈
  519. /// </summary>
  520. public void RemoveTopStack(UIPanel uiPanel)
  521. {
  522. if (TopUIPanels.Contains(uiPanel))
  523. {
  524. TopUIPanels.Remove(uiPanel);
  525. }
  526. }
  527. /// <summary>
  528. /// 关闭最顶部的ui
  529. /// </summary>
  530. public UIPanel CloseTopUI()
  531. {
  532. if (TopUIPanels.Count > 0)
  533. {
  534. UIPanel uiPanel = TopUIPanels[^1];
  535. uiPanel.IsBreadcrumbBarPanel = false; //返回关闭的时候将它设置为false 表示可以被删除
  536. HideUIUIPanel(uiPanel);
  537. return uiPanel;
  538. }
  539. return null;
  540. }
  541. /// <summary>
  542. /// 隐藏所有堆栈的ui
  543. /// </summary>
  544. public void HideAllStackUI()
  545. {
  546. for (var i = 0; i < TopUIPanels.Count; i++)
  547. {
  548. HideUIUIPanel(TopUIPanels[i]);
  549. }
  550. }
  551. /// <summary>
  552. /// 打开堆栈最上方
  553. /// </summary>
  554. public async CTask OpenTopUI()
  555. {
  556. if (TopUIPanels.Count > 0)
  557. {
  558. UIPanel uiPanel = TopUIPanels[^1];
  559. // currOpenPanel = uiPanel;
  560. await uiPanel.Show();
  561. }
  562. }
  563. public Material UISpineMaterial;
  564. /// <summary>
  565. /// 创建组件 可重复使用的(这个方法,只是提供gameObject和脚本的绑定,没有经过池子,因此gameObject依然还挂在原来的预设上)
  566. /// </summary>
  567. /// <param name="root">父节点</param>
  568. /// <typeparam name="T"></typeparam>
  569. /// <returns></returns>
  570. public async CTask<T> CreateGComponentForObject<T>(GameObject gameObject, Action<T> callback,
  571. RectTransform root = null,
  572. object[] uiData = null, bool isInstance = false, string poolName = null, bool isActiveAnima = true)
  573. where T : UIComponent, new()
  574. {
  575. try
  576. {
  577. Type type = typeof(T);
  578. // // 获取UI绑定属性
  579. UIBindingAttribute uiBindingAttribute =
  580. type.GetCustomAttribute(typeof(UIBindingAttribute)) as UIBindingAttribute;
  581. if (uiBindingAttribute == null)
  582. {
  583. LogTool.Error($"类型{type.Name}没有添加UIBindingAttribute!");
  584. return null;
  585. }
  586. GameObject newObject = gameObject;
  587. T gameObjectPool = null;
  588. if (isInstance)
  589. {
  590. string newPoolName = string.IsNullOrEmpty(poolName) ? gameObject.name + ".prefab" : poolName;
  591. gameObjectPool = GObjectPool.Instance.FetchAsyncForGameObject<T>(gameObject, newPoolName);
  592. newObject = gameObjectPool.own;
  593. // gameObjectPool.own.transform.SetParent(root);
  594. // newObject = GameObject.Instantiate(gameObject);
  595. }
  596. else
  597. {
  598. gameObjectPool = Activator.CreateInstance<T>();
  599. gameObjectPool.ActiveObj();
  600. // UIEventMono uiEventMono = newObject.GetComponent<UIEventMono>();
  601. // if (uiEventMono != null)
  602. // {
  603. // uiEventMono.Destory();
  604. // }
  605. }
  606. if (gameObjectPool == null)
  607. {
  608. return null;
  609. }
  610. gameObjectPool.SetGameObject(newObject);
  611. return await SetGComponentInfo(gameObjectPool, callback, root, uiData, isActiveAnima);
  612. }
  613. catch (Exception e)
  614. {
  615. LogTool.Exception(e);
  616. return null;
  617. }
  618. }
  619. /// <summary>
  620. /// 创建组件 可重复使用的
  621. /// </summary>
  622. /// <param name="root">父节点</param>
  623. /// <typeparam name="T"></typeparam>
  624. /// <returns></returns>
  625. public async CTask<T> CreateGComponent<T>(Action<T> callback, RectTransform root = null, object[] uiData = null,
  626. string poolName = null, Clock clock = null, bool isActive = true, bool isActiveAnima = true)
  627. where T : UIComponent, new()
  628. {
  629. try
  630. {
  631. Type type = typeof(T);
  632. // 获取UI绑定属性
  633. UIBindingAttribute uiBindingAttribute =
  634. type.GetCustomAttribute(typeof(UIBindingAttribute)) as UIBindingAttribute;
  635. if (uiBindingAttribute == null)
  636. {
  637. LogTool.Error($"类型{type.Name}没有添加UIBindingAttribute!");
  638. return null;
  639. }
  640. T gameObjectPool = await GObjectPool.Instance.FetchAsync<T>(uiBindingAttribute.prefab + ".prefab",
  641. poolName: poolName, clock: clock);
  642. if (gameObjectPool == null)
  643. {
  644. return default;
  645. }
  646. T cTask = await SetGComponentInfo(gameObjectPool, callback, root, uiData, isActiveAnima);
  647. gameObjectPool.own.SetActive(isActive);
  648. return cTask;
  649. }
  650. catch (Exception e)
  651. {
  652. LogTool.Exception(e);
  653. return null;
  654. }
  655. }
  656. /// <summary>
  657. /// 隐藏回收池子内的所有对象
  658. /// </summary>
  659. /// <param name="poolName"></param>
  660. /// <typeparam name="T"></typeparam>
  661. public void DormancyAllGComponent<T>(string poolName)
  662. {
  663. GObjectPool.Instance.DormancyPool<T>(poolName);
  664. }
  665. public void DormancyAllGComponent<T>()
  666. {
  667. Type type = typeof(T);
  668. // 获取UI绑定属性
  669. UIBindingAttribute uiBindingAttribute =
  670. type.GetCustomAttribute(typeof(UIBindingAttribute)) as UIBindingAttribute;
  671. if (uiBindingAttribute == null)
  672. {
  673. LogTool.Error($"类型{type.Name}没有添加UIBindingAttribute!");
  674. return;
  675. }
  676. string poolName = uiBindingAttribute.prefab + ".prefab";
  677. GObjectPool.Instance.DormancyPool<T>(poolName);
  678. }
  679. public void DormancyGComponent(IGObjectPoolInterface poolInterface)
  680. {
  681. if (poolInterface == null)
  682. {
  683. return;
  684. }
  685. if (string.IsNullOrEmpty(poolInterface.poolObjName))
  686. {
  687. poolInterface.DormancyObj();
  688. return;
  689. }
  690. GObjectPool.Instance.Recycle(poolInterface);
  691. }
  692. private async CTask<T> SetGComponentInfo<T>(T gameObjectPool, Action<T> callback, RectTransform root = null,
  693. object[] uiData = null, bool isActiveAnima = true) where T : UIComponent, new()
  694. {
  695. try
  696. {
  697. if (gameObjectPool == null)
  698. return null;
  699. RectTransform rectTransform = gameObjectPool.own.GetComponent<RectTransform>();
  700. if (root != null)
  701. {
  702. rectTransform.SetParent(root);
  703. rectTransform.SetAsLastSibling();
  704. rectTransform.anchoredPosition = Vector3.zero;
  705. rectTransform.localScale = Vector3.one;
  706. rectTransform.localEulerAngles = Vector3.zero;
  707. }
  708. gameObjectPool.isActiveAnima = isActiveAnima;
  709. await gameObjectPool.SetUIGameObject(gameObjectPool.own);
  710. if (await gameObjectPool.AsyncInit(uiData))
  711. {
  712. gameObjectPool.DelEvent(); //为了以防万一,先移除内部事件
  713. gameObjectPool.AddEvent();
  714. gameObjectPool.Show();
  715. // await TimerComponent.Instance.WaitAsync(1);
  716. callback?.Invoke(gameObjectPool);
  717. return gameObjectPool;
  718. }
  719. else
  720. {
  721. LogTool.Log($"数据准备失败,请检查{gameObjectPool.own.name}的Init方法中的逻辑");
  722. gameObjectPool.Close();
  723. return null;
  724. }
  725. }
  726. catch (Exception e)
  727. {
  728. LogTool.Exception(e);
  729. return null;
  730. }
  731. }
  732. public async CTask HideUIUIPanel<T>(UIDestroyType uiDestroyType = UIDestroyType.DelayDestroy,
  733. bool isBreadcrumbBarPanel = false) where T : UIPanel
  734. {
  735. UIPanel uiPanel = GetComponent<T>();
  736. if (uiPanel != null)
  737. {
  738. await HideUIUIPanel(uiPanel, uiDestroyType, isBreadcrumbBarPanel);
  739. }
  740. }
  741. /// <summary>
  742. /// 关闭UI
  743. /// </summary>
  744. /// <param name="uiPanel"></param>
  745. public async CTask HideUIUIPanel(UIPanel uiPanel, UIDestroyType uiDestroyType = UIDestroyType.DelayDestroy,
  746. bool isBreadcrumbBarPanel = false)
  747. {
  748. if (uiPanel == null)
  749. return;
  750. if (uiPanel.IsClose)
  751. {
  752. return;
  753. }
  754. if (uiPanel.IsShowCustomBGPanel) //如果是开了背景的界面关闭的时候将背景一起关闭
  755. {
  756. // HideUIUIPanel(GetComponent<BackgroundPanel>(), UIDestroyType.NotDestroy);
  757. }
  758. // CloseTopUI();
  759. // OpenTopUI();
  760. if (uiPanel.IsBreadcrumbBarPanel &&
  761. uiPanel.isAddStack &&
  762. uiDestroyType != UIDestroyType.ImmediatelyDestroy) //如果显示导航栏的面板就只做隐藏 导航返回自动处理关闭
  763. {
  764. // if (isBreadcrumbBarPanel)
  765. {
  766. uiPanel.IsBreadcrumbBarPanel = false;
  767. UIPanel uiPanel1 = CloseTopUI();
  768. if (uiPanel != null)
  769. {
  770. LastUIPanel = uiPanel1;
  771. await OpenTopUI();
  772. return;
  773. }
  774. }
  775. }
  776. if (!NoFocusTopUIPanels.Contains(uiPanel)) NoFocusTopUIPanels.Remove(uiPanel);
  777. uiPanel.Close();
  778. RemoveTopStack(uiPanel); //如果要删除界面判断是否在跳转堆栈 如果是则移除堆栈
  779. switch (uiDestroyType)
  780. {
  781. case UIDestroyType.DelayDestroy:
  782. if (!destroyUI.ContainsKey(uiPanel.uiName))
  783. {
  784. uiPanel.AddDelayDestroy();
  785. destroyUI.Add(uiPanel.uiName, uiPanel);
  786. }
  787. break;
  788. case UIDestroyType.ImmediatelyDestroy:
  789. DestroyUIPanel(uiPanel);
  790. break;
  791. case UIDestroyType.NotDestroy:
  792. break;
  793. }
  794. RefreshFull();
  795. }
  796. public void DestroyUIPanel(UIPanel uiPanel)
  797. {
  798. if (uiPanel.Parent == null)
  799. {
  800. return;
  801. }
  802. destroyUI.Remove(uiPanel.uiName);
  803. uiPanel.Parent.RemoveComponent(uiPanel);
  804. }
  805. private async CTask<GameObject> FetchUI(UIBindingAttribute uiBindingAttribute, Transform tf)
  806. {
  807. GameObjectPool assetBundle =
  808. await GObjectPool.Instance.FetchAsync<GameObjectPool>(uiBindingAttribute.prefab + ".prefab");
  809. GameObject gameObject = assetBundle.own;
  810. if (gameObject == null)
  811. return null;
  812. gameObject.transform.SetParent(tf);
  813. gameObject.transform.SetAsLastSibling();
  814. gameObject.transform.localScale = Vector3.one;
  815. return gameObject;
  816. }
  817. /// <summary>
  818. /// <summary>
  819. /// 屏幕转ui坐标
  820. /// </summary>
  821. /// <param name="rPosition"></param>
  822. /// <returns></returns>
  823. public Vector3 ScreenPointToLocalPointInRectangle(Vector2 rPosition)
  824. {
  825. RectTransform rRect = Instance.Canvas.GetComponent<RectTransform>();
  826. Vector3 lPosition = Instance.UICamera.ScreenToWorldPoint(rPosition);
  827. Vector4 l4Position =
  828. new Vector4(lPosition.x, lPosition.y, lPosition.z, 1); // unity默认转化v4,w是0,不会计算x和y的rect的偏移,所以这里要自己搞一下。
  829. lPosition = rRect.worldToLocalMatrix * l4Position;
  830. lPosition.z = 0;
  831. return lPosition;
  832. }
  833. /// <summary>
  834. /// ui组件添加自定义事件
  835. /// </summary>
  836. /// <param name="obj"></param>
  837. /// <param name="eventTriggerType"></param>
  838. /// <param name="callback"></param>
  839. public void AddCustomEventListener(UIBehaviour control, EventTriggerType type,
  840. UnityAction<BaseEventData> callback)
  841. {
  842. EventTrigger trigger = control.GetComponent<EventTrigger>();
  843. if (trigger == null)
  844. {
  845. trigger = control.gameObject.AddComponent<EventTrigger>();
  846. }
  847. EventTrigger.Entry entry = new EventTrigger.Entry();
  848. entry.eventID = type;
  849. entry.callback.AddListener(callback);
  850. trigger.triggers.Add(entry);
  851. }
  852. public override void Dispose()
  853. {
  854. base.Dispose();
  855. UGUIIamgeTool.renderOrder = 0;
  856. }
  857. /// <summary>
  858. /// 移除自定义事件
  859. /// </summary>
  860. /// <param name="control"></param>
  861. /// <param name="type"></param>
  862. /// <param name="callback"></param>
  863. public void RemoveCustomEventListener(UIBehaviour control, EventTriggerType type,
  864. UnityAction<BaseEventData> callback)
  865. {
  866. EventTrigger trigger = control.GetComponent<EventTrigger>();
  867. if (trigger == null)
  868. {
  869. trigger = control.gameObject.AddComponent<EventTrigger>();
  870. }
  871. for (int i = 0; i < trigger.triggers.Count; i++)
  872. {
  873. if (trigger.triggers[i].eventID == type)
  874. {
  875. trigger.triggers[i].callback.RemoveListener(callback);
  876. // Debug.Log("移除事件");
  877. }
  878. }
  879. }
  880. public GameObject GetOverUI(GameObject canvas)
  881. {
  882. PointerEventData pointerEventData = new PointerEventData(UnityEngine.EventSystems.EventSystem.current);
  883. pointerEventData.position = Input.mousePosition;
  884. GraphicRaycaster gr = canvas.GetComponent<GraphicRaycaster>();
  885. List<RaycastResult> results = new List<RaycastResult>();
  886. gr.Raycast(pointerEventData, results);
  887. if (results.Count != 0)
  888. {
  889. return results[0].gameObject;
  890. }
  891. return null;
  892. }
  893. public Vector2 SetPopTipPosition(Vector3 pos, Vector2 sizeDelta)
  894. {
  895. Vector2 xy = sizeDelta / 2;
  896. Vector2 vector2 = Canvas.GetComponent<RectTransform>().sizeDelta;
  897. float x = vector2.x / 2;
  898. float y = vector2.y / 2;
  899. if (pos.x + xy.x >= x)
  900. {
  901. pos.x = x - xy.x;
  902. }
  903. else if (pos.x - xy.x <= -x)
  904. {
  905. pos.x = -x + xy.x;
  906. }
  907. if (pos.y + xy.y >= y)
  908. {
  909. pos.y = y - xy.y;
  910. }
  911. else if (pos.y - xy.y <= -y)
  912. {
  913. pos.y = -y + xy.y;
  914. }
  915. return pos;
  916. }
  917. /// <summary>
  918. /// 设置数值文本的显示
  919. /// </summary>
  920. /// <param name="number"></param>
  921. /// <returns></returns>
  922. public string SetNumberTextShow(long number)
  923. {
  924. if (number > 1000000000000)
  925. {
  926. float n = number / 1000000000000f;
  927. string str = "";
  928. if (n > 100)
  929. {
  930. int n1 = (int)(n * 10);
  931. n = n1 / 10f;
  932. str = n.ToString(CultureInfo.InvariantCulture);
  933. }
  934. else
  935. {
  936. int n1 = (int)(n * 100);
  937. n = n1 / 100f;
  938. str = n.ToString(CultureInfo.InvariantCulture);
  939. }
  940. return str + "t";
  941. }
  942. if (number > 1000000000)
  943. {
  944. float n = number / 1000000000f;
  945. string str = "";
  946. if (n > 100)
  947. {
  948. int n1 = (int)(n * 10);
  949. n = n1 / 10f;
  950. str = n.ToString(CultureInfo.InvariantCulture);
  951. }
  952. else
  953. {
  954. int n1 = (int)(n * 100);
  955. n = n1 / 100f;
  956. str = n.ToString(CultureInfo.InvariantCulture);
  957. }
  958. return str + "b";
  959. }
  960. if (number > 1000000)
  961. {
  962. float n = number / 1000000f;
  963. string str = "";
  964. if (n > 100)
  965. {
  966. int n1 = (int)(n * 10);
  967. n = n1 / 10f;
  968. str = n.ToString(CultureInfo.InvariantCulture);
  969. }
  970. else
  971. {
  972. int n1 = (int)(n * 100);
  973. n = n1 / 100f;
  974. str = n.ToString(CultureInfo.InvariantCulture);
  975. }
  976. return str + "m";
  977. }
  978. if (number > 10000)
  979. {
  980. float n = number / 1000f;
  981. string str = "";
  982. if (n > 100)
  983. {
  984. int n1 = (int)(n * 10);
  985. n = n1 / 10f;
  986. str = n.ToString(CultureInfo.InvariantCulture);
  987. }
  988. else
  989. {
  990. int n1 = (int)(n * 100);
  991. n = n1 / 100f;
  992. str = n.ToString(CultureInfo.InvariantCulture);
  993. }
  994. return str + "k";
  995. }
  996. return number.ToString();
  997. }
  998. /// <summary>
  999. /// 每日是否提醒标记
  1000. /// </summary>
  1001. public enum ToDayDontShowTag
  1002. {
  1003. MjToDiamond = 0,
  1004. DiamondUsed, //钻石使用判断
  1005. RelicIntensify,
  1006. /// <summary>
  1007. /// 经验转换
  1008. /// </summary>
  1009. EXConvert
  1010. }
  1011. //判断鼠标是否点在ui上
  1012. public bool IsPointerOverGameObject(Vector2 mousePosition)
  1013. {
  1014. PointerEventData eventData = new PointerEventData(EventSystem.current);
  1015. eventData.position = mousePosition;
  1016. List<RaycastResult> raycastResults = new List<RaycastResult>();
  1017. //发射射线判断是否点到ui
  1018. EventSystem.current.RaycastAll(eventData, raycastResults);
  1019. if (raycastResults.Count > 0)
  1020. {
  1021. return true;
  1022. }
  1023. else
  1024. {
  1025. return false;
  1026. }
  1027. }
  1028. }
  1029. }