UIManager.cs 37 KB

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