UIManager.cs 36 KB

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