UIManager.cs 39 KB

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