123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110 |
- using System;
- using System.Collections.Generic;
- using System.Globalization;
- using System.Reflection;
- using Core.Event.Event;
- using Fort23.Core;
- using Fort23.UTool;
- using UnityEngine;
- using UnityEngine.Events;
- using UnityEngine.EventSystems;
- using UnityEngine.UI;
- using Utility;
- using EventSystem = UnityEngine.EventSystems.EventSystem;
- namespace Fort23.Mono
- {
- public enum UILayer
- {
- Bottom, //最底层UI(主界面)
- Middle, //中间UI(各种Panel)
- Top, //最顶层UI节点
- Loading, // 加载界面
- }
- /// <summary>
- /// 销毁类型
- /// </summary>
- public enum UIDestroyType
- {
- DelayDestroy,
- ImmediatelyDestroy,
- NotDestroy,
- }
- /// <summary>
- /// 如果有同时弹出多个UI界面冲突的地方,推荐用async/await的方法一次操作,这样就不用单独去为了适配打开多个UI的需求而改动太多的代码(主要是这样改动会牺牲大部分情况下的性能)
- /// </summary>
- public class UIManager : Entity
- {
- public static UIManager Instance { get; set; }
- public readonly Material uiGray = new Material(Shader.Find("MyShader/UIImageGray"));
- // public RectTransform UIRootTransform;
- public Vector2 sizeDelta;
- public EventSystem current;
- /// <summary>
- /// 父节点(InitClip)
- /// </summary>
- private Transform _parent;
- /// <summary>
- /// 所有展示BG的ui
- /// </summary>
- public List<UIPanel> AllShowBGUIs = new List<UIPanel>();
- /// <summary>
- /// 顶部ui列表 可以通过导航栏跳转的界面
- /// </summary>
- public List<UIPanel> TopUIPanels = new List<UIPanel>();
- /// <summary>
- /// 所有top层ui
- /// </summary>
- public List<UIPanel> NoFocusTopUIPanels = new List<UIPanel>();
- /// <summary>
- /// 预先定好的几个层,数量和UILayer对应
- /// </summary>
- // public Canvas[] canvases = new Canvas[3];
- public Camera UICamera;
- public Canvas Canvas;
- public Transform[] UILayers = new Transform[4];
- /// <summary>
- /// 当前打开的ui
- /// </summary>
- public UIPanel currOpenPanel;
- /// <summary>
- /// UI布局大小
- /// </summary>
- private Vector2 UIScale;
- private BetterList<UIPanel> _lastShowPanel = new BetterList<UIPanel>();
- private Graphic currDeapthGraphic;
- /// <summary>
- /// 需要销毁的UI
- /// </summary>
- public Dictionary<string, UIPanel> destroyUI = new Dictionary<string, UIPanel>();
- public CustomCameraStack CurrCustomCameraStack
- {
- get { return _currCustomCameraStack; }
- set
- {
- _currCustomCameraStack = value;
- RefreshFull();
- }
- }
- public void CleanLastShowPanel()
- {
- _lastShowPanel.Clear();
- }
- private CustomCameraStack _currCustomCameraStack;
- /// <summary>
- /// 屏蔽字库
- /// </summary>
- public string MaskWordData;
- /// <summary>
- /// 展示文字提示长度
- /// </summary>
- public int ShowTextCount;
- /// <summary>
- /// 展示文字提示最大长度
- /// </summary>
- public int ShowTextMaxCount = 1;
- /// <summary>
- /// 上一个界面
- /// </summary>
- public UIPanel LastUIPanel;
- [CustomMethod(CustomMethodType.Awake)]
- public async void Awake(Transform parent)
- {
- Instance = this;
- _parent = parent;
- }
- public async CTask InitUI()
- {
- if (Canvas == null)
- {
- AssetHandle assetBundle =
- await AssetBundleLoadManager.Instance.LoadAssetAsyncTask<GameObject>("Canvas.prefab");
- GameObject prefab = assetBundle.AssetObject<GameObject>();
- prefab.SetActive(false);
- // UIRootTransform = prefab.GetComponent<RectTransform>();
- sizeDelta = prefab.GetComponent<RectTransform>().sizeDelta;
- // if (1.0f * Screen.width / Screen.height < 1334f / 750f)
- // {
- // sizeDelta = new Vector2(1334, 750);
- // }
- // return;
- Canvas = prefab.GetComponent<Canvas>();
- prefab.transform.SetParent(_parent);
- UILayers[0] = prefab.transform.GetChild(0);
- UILayers[1] = prefab.transform.GetChild(1);
- UILayers[2] = prefab.transform.GetChild(2);
- UILayers[3] = prefab.transform.GetChild(3);
- UICamera = prefab.transform.GetComponentInChildren<Camera>();
- }
- // if (MaskWordData == null)
- // {
- // AssetHandle assetBundle =
- // await AssetBundleLoadManager.Instance.LoadAssetAsyncTask<TextAsset>("guofu.txt");
- // TextAsset text = assetBundle.AssetObject<TextAsset>();
- // MaskWordData = text.text;
- // assetBundle.Release();
- // }
- }
- public RectTransform GetLayer(UILayer layer)
- {
- try
- {
- int index = (int)layer;
- return (RectTransform)UILayers[index];
- }
- catch (Exception e)
- {
- Console.WriteLine(e);
- throw;
- }
- }
- public void SetGray(GameObject gameObject, bool isGray, bool isChildGray = false)
- {
- if (isChildGray)
- {
- Graphic[] graphics = gameObject.transform.GetComponentsInChildren<Graphic>();
- for (int i = 0; i < graphics.Length; i++)
- {
- if (isGray)
- {
- graphics[i].material = uiGray;
- }
- else
- {
- graphics[i].material = graphics[i].defaultMaterial;
- }
- }
- }
- else
- {
- Graphic graphic = gameObject.GetComponent<Graphic>();
- if (isGray)
- {
- graphic.material = uiGray;
- }
- else
- {
- graphic.material = graphic.defaultMaterial;
- }
- }
- }
- public void SetEventSystemEnable(bool value)
- {
- // current.isClose = !value;
- }
- [CustomMethod(CustomMethodType.Update)]
- public async void Update()
- {
- if (currDeapthGraphic != null)
- {
- UGUIIamgeTool.minDepth = currDeapthGraphic.canvasRenderer.absoluteDepth;
- UGUIIamgeTool.renderOrder = currDeapthGraphic.canvas.renderOrder;
- }
- else
- {
- UGUIIamgeTool.minDepth = -1;
- UGUIIamgeTool.renderOrder = 0;
- }
- }
- public Vector2 WorldToUIWorld(Vector3 worldPos)
- {
- // Vector3 worldPos = harmReturnInfo.target.combatHeroEntity.combatHeroGameObject.hpTransform.position;
- Vector3 p = CurrCustomCameraStack.camera.WorldToScreenPoint(worldPos);
- Vector3 p2 = UICamera.ScreenToWorldPoint(p);
- return p2;
- }
- public Vector3 UIWorldToWorld(Vector3 worldPos)
- {
- // Vector3 worldPos = harmReturnInfo.target.combatHeroEntity.combatHeroGameObject.hpTransform.position;
- Vector3 p = UICamera.WorldToScreenPoint(worldPos);
- p.z = 20;
- Vector3 p2 = CurrCustomCameraStack.camera.ScreenToWorldPoint(p);
- return p2;
- }
- /// <summary>
- /// 隐藏当前全部的UIPanel(只是不显示,不是销毁) 排除不在隐藏之列的UI
- /// </summary>
- public void HindCurrAllShowPanel()
- {
- if (_lastShowPanel.Count > 0)
- {
- return;
- }
- _lastShowPanel.Clear();
- UIPanel[] allShowPanel = GetComponentAll<UIPanel>();
- for (int i = 0; i < allShowPanel.Length; i++)
- {
- if (allShowPanel[i].GObjectPoolInterface.activeSelf)
- {
- allShowPanel[i].GObjectPoolInterface.SetActive(false);
- _lastShowPanel.Add(allShowPanel[i]);
- }
- }
- }
- public async CTask ShowLastHindAllShowPanel()
- {
- for (int i = 0; i < _lastShowPanel.Count; i++)
- {
- if (_lastShowPanel[i].GObjectPoolInterface != null)
- {
- _lastShowPanel[i].GObjectPoolInterface.SetActive(true);
- }
- }
- if (_lastShowPanel.Contains(TopUIPanels[^1]))
- {
- _lastShowPanel.Clear();
- if (TopUIPanels[^1].isShow)
- {
- await TopUIPanels[^1].GetFocus();
- }
- }
- _lastShowPanel.Clear();
- }
- /// <summary>
- /// 用窗口打开某个界面(不可重复的预设,适用于一切带有关闭按钮的窗口UI)
- /// </summary>
- /// <param name="callback"> ui加载完成时的回调</param>
- /// <param name="layer"> ui需要显示在那一层,现在分为3层</param>
- /// <param name="isFocus"> 是否时需要获取焦点</param>
- /// <param name="uiData">打开UI时的透传数据</param>
- /// <param name="isShowBG">是否需要显示统一的背板</param>
- /// isFullUI 是否是全面屏UI
- /// <typeparam name="T"></typeparam>
- /// <returns></returns>
- public async CTask<T> LoadAndOpenPanel<T>(Action<T> callback, UILayer layer = UILayer.Middle,
- bool isFocus = true, object[] uiData = null, bool isShowBG = false, bool isFullUI = false,
- bool isActiveAnima = true)
- where T : UIPanel, new()
- {
- try
- {
- Type type;
- current = EventSystem.current;
- type = typeof(T);
- T uiPanel;
- // 获取UI绑定属性
- UIBindingAttribute uiBindingAttribute =
- type.GetCustomAttribute(typeof(UIBindingAttribute)) as UIBindingAttribute;
- if (uiBindingAttribute == null)
- {
- LogTool.Error($"类型{type.Name}没有添加UIBindingAttribute!");
- // ShowTips($"类型{buildType.Name}没有添加UIBindingAttribute!");
- return null;
- }
- // TDManager.Instance.PartCount(uiBindingAttribute.prefab);
- if (isFocus)
- {
- if (currOpenPanel != null)
- {
- if (currOpenPanel.isShow)
- {
- await currOpenPanel.LoseFocus();
- currOpenPanel = null;
- }
- else
- {
- currOpenPanel = null;
- }
- }
- }
- using (await CoroutineLockComponent.Instance.Wait(uiBindingAttribute.prefab))
- {
- if (current != null)
- {
- // current.isClose = true;
- }
- // 不可以重复打开Panel,但是可以把上一个拿来使用。
- if (!Components.ContainsKey(type))
- {
- GameObject gameObjectPool = await FetchUI(uiBindingAttribute, GetLayer(layer));
- if (gameObjectPool == null)
- return null;
- uiPanel = AddComponent<T, GameObject>(gameObjectPool, false);
- uiPanel.uiName = uiBindingAttribute.prefab;
- uiPanel.isFocus = isFocus;
- await uiPanel.SetUIGameObject(gameObjectPool);
- gameObjectPool.SetActive(false);
- }
- else
- {
- uiPanel = GetComponent<T>();
- if (uiPanel.DestroyTimerEntity != null)
- {
- TimerComponent.Instance.Remove(uiPanel.DestroyTimerEntity.ID);
- uiPanel.DestroyTimerEntity.Destroy();
- }
- destroyUI.Remove(uiBindingAttribute.prefab);
- }
- uiPanel.isFullUI = isFullUI;
- // 等待数据准备好才初始化UI
- if (await uiPanel.AsyncInit(uiData))
- {
- if (uiPanel.IsBreadcrumbBarPanel) //打开界面之前判断是否已经在堆栈当中 如果是就移除此界面往后的堆栈 保持他时在堆栈的最上方
- {
- if (TopUIPanels.Contains(uiPanel))
- {
- int index = TopUIPanels.IndexOf(uiPanel);
- int count = TopUIPanels.Count - index;
- for (int i = index + 1; i < TopUIPanels.Count; i++)
- {
- if (TopUIPanels[i].isShow)
- {
- HideUIUIPanel(TopUIPanels[i]);
- }
- }
- TopUIPanels.RemoveRange(index, count);
- }
- }
- if (layer == UILayer.Top && !NoFocusTopUIPanels.Contains(uiPanel))
- NoFocusTopUIPanels.Add(uiPanel);
- uiPanel.isActiveAnima = isActiveAnima;
- await uiPanel.Open();
-
- if (current != null)
- {
- // current.isClose = false;
- }
- await TimerComponent.Instance.WaitAsync(1); //等待一帧 让渲染完成后在执行完成逻辑
- callback?.Invoke(uiPanel);
- RefreshFull();
- if (isShowBG)
- {
- uiPanel.IsShowCustomBGPanel = true;
- if (!AllShowBGUIs.Contains(uiPanel))
- AllShowBGUIs.Add(uiPanel);
- // await GetBackgroundPanel(uiPanel);
- }
- return uiPanel;
- }
- else
- {
- if (current != null)
- {
- // current.isClose = false;
- }
- // 界面数据没准备好,因此本次操作将被销毁
- LogTool.Log($"数据准备失败,请检查{type.Name}的Init方法中的逻辑");
- // ShowTips($"数据准备失败,请检查{buildType.Name}的Init方法中的逻辑");
- uiPanel.Close();
- return null;
- }
- }
- }
- catch (Exception e)
- {
- if (current != null)
- {
- // current.isClose = false;
- }
- LogTool.Error(typeof(T).ToString() + " " + e.Message);
- LogTool.Exception(e);
- return null;
- }
- }
- public void RefreshFull()
- {
- Graphic min = null;
- UIPanel[] allPanel = GetComponentAll<UIPanel>();
- bool isFull = false;
- if (allPanel != null)
- {
- for (int i = 0; i < allPanel.Length; i++)
- {
- UIPanel panel = allPanel[i];
- if (panel != null && panel.minDepth != null && !panel.IsClose && panel.isShow &&
- panel.GObjectPoolInterface.activeSelf && panel.isFullUI)
- {
- if (min == null)
- {
- min = panel.minDepth;
- continue;
- }
- if (min != null && panel.minDepth.canvasRenderer.absoluteDepth >
- min.canvasRenderer.absoluteDepth)
- {
- min = panel.minDepth;
- }
- }
- }
- if (min != null)
- {
- UGUIIamgeTool.minDepth = min.canvasRenderer.absoluteDepth;
- UGUIIamgeTool.renderOrder = min.canvas.renderOrder;
- }
- else
- {
- UGUIIamgeTool.minDepth = -1;
- UGUIIamgeTool.renderOrder = 0;
- }
- currDeapthGraphic = min;
- for (int i = 0; i < allPanel.Length; i++)
- {
- UIPanel panel = allPanel[i];
- if (panel != null && !panel.IsClose && panel.isShow && panel.GObjectPoolInterface.activeSelf &&
- panel.isFullUI)
- {
- isFull = true;
- break;
- }
- }
- }
- RefreshFullEventData refreshFullEventData = RefreshFullEventData.Create();
- refreshFullEventData.isFullShow = isFull;
- EventManager.Instance.Dispatch(CustomEventType.RefreshFull, refreshFullEventData);
- if (_currCustomCameraStack == null)
- {
- // URPTool.Instance.IsNotRenderMainCamera = false;
- return;
- }
- // URPTool.Instance.IsNotRenderMainCamera = isFull;
- }
- /// <summary>
- /// 关闭最上层堆栈的uiPanel
- /// </summary>
- public void HideTopPanel()
- {
- if (TopUIPanels.Count > 0)
- {
- if (TopUIPanels[^1] != null)
- {
- TopUIPanels[^1].Hide();
- }
- }
- }
- /// <summary>
- /// 加入topUI堆栈
- /// </summary>
- public void AddTopStack(UIPanel uiPanel)
- {
- if (!TopUIPanels.Contains(uiPanel))
- {
- TopUIPanels.Add(uiPanel);
- uiPanel.IsBreadcrumbBarPanel = true;
- }
- }
- /// <summary>
- /// 弹出堆栈
- /// </summary>
- public void RemoveTopStack(UIPanel uiPanel)
- {
- if (TopUIPanels.Contains(uiPanel))
- {
- TopUIPanels.Remove(uiPanel);
- }
- }
- /// <summary>
- /// 关闭最顶部的ui
- /// </summary>
- public UIPanel CloseTopUI()
- {
- if (TopUIPanels.Count > 0)
- {
- UIPanel uiPanel = TopUIPanels[^1];
- uiPanel.IsBreadcrumbBarPanel = false; //返回关闭的时候将它设置为false 表示可以被删除
- HideUIUIPanel(uiPanel);
- return uiPanel;
- }
- return null;
- }
- /// <summary>
- /// 隐藏所有堆栈的ui
- /// </summary>
- public void HideAllStackUI()
- {
- for (var i = 0; i < TopUIPanels.Count; i++)
- {
- HideUIUIPanel(TopUIPanels[i]);
- }
- }
- /// <summary>
- /// 打开堆栈最上方
- /// </summary>
- public async void OpenTopUI()
- {
- if (TopUIPanels.Count > 0)
- {
- UIPanel uiPanel = TopUIPanels[^1];
- await uiPanel.Show();
- }
- }
- public Material UISpineMaterial;
- /// <summary>
- /// 创建组件 可重复使用的(这个方法,只是提供gameObject和脚本的绑定,没有经过池子,因此gameObject依然还挂在原来的预设上)
- /// </summary>
- /// <param name="root">父节点</param>
- /// <typeparam name="T"></typeparam>
- /// <returns></returns>
- public async CTask<T> CreateGComponentForObject<T>(GameObject gameObject, Action<T> callback,
- RectTransform root = null,
- object[] uiData = null, bool isInstance = false, string poolName = null, bool isActiveAnima = true)
- where T : UIComponent, new()
- {
- try
- {
- Type type = typeof(T);
- // // 获取UI绑定属性
- UIBindingAttribute uiBindingAttribute =
- type.GetCustomAttribute(typeof(UIBindingAttribute)) as UIBindingAttribute;
- if (uiBindingAttribute == null)
- {
- LogTool.Error($"类型{type.Name}没有添加UIBindingAttribute!");
- return null;
- }
- GameObject newObject = gameObject;
- T gameObjectPool = null;
- if (isInstance)
- {
- string newPoolName = string.IsNullOrEmpty(poolName) ? gameObject.name + ".prefab" : poolName;
- gameObjectPool = GObjectPool.Instance.FetchAsyncForGameObject<T>(gameObject, newPoolName);
- newObject = gameObjectPool.own;
- // gameObjectPool.own.transform.SetParent(root);
- // newObject = GameObject.Instantiate(gameObject);
- }
- else
- {
- gameObjectPool = Activator.CreateInstance<T>();
- // UIEventMono uiEventMono = newObject.GetComponent<UIEventMono>();
- // if (uiEventMono != null)
- // {
- // uiEventMono.Destory();
- // }
- }
- if (gameObjectPool == null)
- {
- return null;
- }
- gameObjectPool.SetGameObject(newObject);
- return await SetGComponentInfo(gameObjectPool, callback, root, uiData, isActiveAnima);
- }
- catch (Exception e)
- {
- LogTool.Exception(e);
- return null;
- }
- }
- /// <summary>
- /// 创建组件 可重复使用的
- /// </summary>
- /// <param name="root">父节点</param>
- /// <typeparam name="T"></typeparam>
- /// <returns></returns>
- public async CTask<T> CreateGComponent<T>(Action<T> callback, RectTransform root = null, object[] uiData = null,
- string poolName = null, Clock clock = null, bool isActive = true, bool isActiveAnima = true)
- where T : UIComponent, new()
- {
- try
- {
- Type type = typeof(T);
- // 获取UI绑定属性
- UIBindingAttribute uiBindingAttribute =
- type.GetCustomAttribute(typeof(UIBindingAttribute)) as UIBindingAttribute;
- if (uiBindingAttribute == null)
- {
- LogTool.Error($"类型{type.Name}没有添加UIBindingAttribute!");
- return null;
- }
- T gameObjectPool = await GObjectPool.Instance.FetchAsync<T>(uiBindingAttribute.prefab + ".prefab",
- poolName: poolName, clock: clock);
- if (gameObjectPool == null)
- {
- return default;
- }
- T cTask = await SetGComponentInfo(gameObjectPool, callback, root, uiData, isActiveAnima);
- gameObjectPool.own.SetActive(isActive);
- return cTask;
- }
- catch (Exception e)
- {
- LogTool.Exception(e);
- return null;
- }
- }
- public void DormancyAllGComponent<T>(string poolName)
- {
- GObjectPool.Instance.DormancyPool<T>(poolName);
- }
- public void DormancyAllGComponent<T>()
- {
- Type type = typeof(T);
- // 获取UI绑定属性
- UIBindingAttribute uiBindingAttribute =
- type.GetCustomAttribute(typeof(UIBindingAttribute)) as UIBindingAttribute;
- if (uiBindingAttribute == null)
- {
- LogTool.Error($"类型{type.Name}没有添加UIBindingAttribute!");
- return;
- }
- string poolName = uiBindingAttribute.prefab + ".prefab";
- GObjectPool.Instance.DormancyPool<T>(poolName);
- }
- public void DormancyGComponent(IGObjectPoolInterface poolInterface)
- {
- GObjectPool.Instance.Recycle(poolInterface);
- }
- private async CTask<T> SetGComponentInfo<T>(T gameObjectPool, Action<T> callback, RectTransform root = null,
- object[] uiData = null, bool isActiveAnima = true) where T : UIComponent, new()
- {
- try
- {
- if (gameObjectPool == null)
- return null;
- RectTransform rectTransform = gameObjectPool.own.GetComponent<RectTransform>();
- rectTransform.SetParent(root);
- rectTransform.SetAsLastSibling();
- rectTransform.anchoredPosition = Vector3.zero;
- rectTransform.localScale = Vector3.one;
- rectTransform.localEulerAngles = Vector3.zero;
- gameObjectPool.isActiveAnima = isActiveAnima;
- await gameObjectPool.SetUIGameObject(gameObjectPool.own);
- if (await gameObjectPool.AsyncInit(uiData))
- {
- gameObjectPool.DelEvent(); //为了以防万一,先移除内部事件
- gameObjectPool.AddEvent();
- gameObjectPool.Show();
- // await TimerComponent.Instance.WaitAsync(1);
- callback?.Invoke(gameObjectPool);
- return gameObjectPool;
- }
- else
- {
- LogTool.Log($"数据准备失败,请检查{gameObjectPool.own.name}的Init方法中的逻辑");
- gameObjectPool.Close();
- return null;
- }
- }
- catch (Exception e)
- {
- LogTool.Exception(e);
- return null;
- }
- }
- public void HideUIUIPanel<T>(UIDestroyType uiDestroyType = UIDestroyType.DelayDestroy,
- bool isBreadcrumbBarPanel = false) where T : UIPanel
- {
- UIPanel uiPanel = GetComponent<T>();
- if (uiPanel != null)
- {
- HideUIUIPanel(uiPanel, uiDestroyType, isBreadcrumbBarPanel);
- }
- }
- /// <summary>
- /// 关闭UI
- /// </summary>
- /// <param name="uiPanel"></param>
- public void HideUIUIPanel(UIPanel uiPanel, UIDestroyType uiDestroyType = UIDestroyType.DelayDestroy,
- bool isBreadcrumbBarPanel = false)
- {
- if (uiPanel == null)
- return;
- if (uiPanel.IsClose)
- {
- return;
- }
- if (uiPanel.IsShowCustomBGPanel) //如果是开了背景的界面关闭的时候将背景一起关闭
- {
- // HideUIUIPanel(GetComponent<BackgroundPanel>(), UIDestroyType.NotDestroy);
- }
- if (!NoFocusTopUIPanels.Contains(uiPanel)) NoFocusTopUIPanels.Remove(uiPanel);
- uiPanel.Close();
- RemoveTopStack(uiPanel); //如果要删除界面判断是否在跳转堆栈 如果是则移除堆栈
- switch (uiDestroyType)
- {
- case UIDestroyType.DelayDestroy:
- if (!destroyUI.ContainsKey(uiPanel.uiName))
- {
- uiPanel.AddDelayDestroy();
- destroyUI.Add(uiPanel.uiName, uiPanel);
- }
- break;
- case UIDestroyType.ImmediatelyDestroy:
- DestroyUIPanel(uiPanel);
- break;
- case UIDestroyType.NotDestroy:
- break;
- }
- RefreshFull();
- }
- public void DestroyUIPanel(UIPanel uiPanel)
- {
- if (uiPanel.Parent == null)
- {
- return;
- }
- destroyUI.Remove(uiPanel.uiName);
- uiPanel.Parent.RemoveComponent(uiPanel);
- }
- private async CTask<GameObject> FetchUI(UIBindingAttribute uiBindingAttribute, Transform tf)
- {
- GameObjectPool assetBundle =
- await GObjectPool.Instance.FetchAsync<GameObjectPool>(uiBindingAttribute.prefab + ".prefab");
- GameObject gameObject = assetBundle.own;
- if (gameObject == null)
- return null;
- gameObject.transform.SetParent(tf);
- gameObject.transform.SetAsLastSibling();
- gameObject.transform.localScale = Vector3.one;
- return gameObject;
- }
- /// <summary>
- /// <summary>
- /// 屏幕转ui坐标
- /// </summary>
- /// <param name="rPosition"></param>
- /// <returns></returns>
- public Vector3 ScreenPointToLocalPointInRectangle(Vector2 rPosition)
- {
- RectTransform rRect = Instance.Canvas.GetComponent<RectTransform>();
- Vector3 lPosition = Instance.UICamera.ScreenToWorldPoint(rPosition);
- Vector4 l4Position =
- new Vector4(lPosition.x, lPosition.y, lPosition.z, 1); // unity默认转化v4,w是0,不会计算x和y的rect的偏移,所以这里要自己搞一下。
- lPosition = rRect.worldToLocalMatrix * l4Position;
- lPosition.z = 0;
- return lPosition;
- }
- /// <summary>
- /// ui组件添加自定义事件
- /// </summary>
- /// <param name="obj"></param>
- /// <param name="eventTriggerType"></param>
- /// <param name="callback"></param>
- public void AddCustomEventListener(UIBehaviour control, EventTriggerType type,
- UnityAction<BaseEventData> callback)
- {
- EventTrigger trigger = control.GetComponent<EventTrigger>();
- if (trigger == null)
- {
- trigger = control.gameObject.AddComponent<EventTrigger>();
- }
- EventTrigger.Entry entry = new EventTrigger.Entry();
- entry.eventID = type;
- entry.callback.AddListener(callback);
- trigger.triggers.Add(entry);
- }
- public override void Dispose()
- {
- base.Dispose();
- UGUIIamgeTool.renderOrder = 0;
- }
- /// <summary>
- /// 移除自定义事件
- /// </summary>
- /// <param name="control"></param>
- /// <param name="type"></param>
- /// <param name="callback"></param>
- public void RemoveCustomEventListener(UIBehaviour control, EventTriggerType type,
- UnityAction<BaseEventData> callback)
- {
- EventTrigger trigger = control.GetComponent<EventTrigger>();
- if (trigger == null)
- {
- trigger = control.gameObject.AddComponent<EventTrigger>();
- }
- for (int i = 0; i < trigger.triggers.Count; i++)
- {
- if (trigger.triggers[i].eventID == type)
- {
- trigger.triggers[i].callback.RemoveListener(callback);
- // Debug.Log("移除事件");
- }
- }
- }
- public GameObject GetOverUI(GameObject canvas)
- {
- PointerEventData pointerEventData = new PointerEventData(UnityEngine.EventSystems.EventSystem.current);
- pointerEventData.position = Input.mousePosition;
- GraphicRaycaster gr = canvas.GetComponent<GraphicRaycaster>();
- List<RaycastResult> results = new List<RaycastResult>();
- gr.Raycast(pointerEventData, results);
- if (results.Count != 0)
- {
- return results[0].gameObject;
- }
- return null;
- }
- public Vector2 SetPopTipPosition(Vector3 pos, Vector2 sizeDelta)
- {
- Vector2 xy = sizeDelta / 2;
- Vector2 vector2 = Canvas.GetComponent<RectTransform>().sizeDelta;
- float x = vector2.x / 2;
- float y = vector2.y / 2;
- if (pos.x + xy.x >= x)
- {
- pos.x = x - xy.x;
- }
- else if (pos.x - xy.x <= -x)
- {
- pos.x = -x + xy.x;
- }
- if (pos.y + xy.y >= y)
- {
- pos.y = y - xy.y;
- }
- else if (pos.y - xy.y <= -y)
- {
- pos.y = -y + xy.y;
- }
- return pos;
- }
- /// <summary>
- /// 设置数值文本的显示
- /// </summary>
- /// <param name="number"></param>
- /// <returns></returns>
- public string SetNumberTextShow(long number)
- {
- if (number > 1000000000000)
- {
- float n = number / 1000000000000f;
- string str = "";
- if (n > 100)
- {
- int n1 = (int)(n * 10);
- n = n1 / 10f;
- str = n.ToString(CultureInfo.InvariantCulture);
- }
- else
- {
- int n1 = (int)(n * 100);
- n = n1 / 100f;
- str = n.ToString(CultureInfo.InvariantCulture);
- }
- return str + "t";
- }
- if (number > 1000000000)
- {
- float n = number / 1000000000f;
- string str = "";
- if (n > 100)
- {
- int n1 = (int)(n * 10);
- n = n1 / 10f;
- str = n.ToString(CultureInfo.InvariantCulture);
- }
- else
- {
- int n1 = (int)(n * 100);
- n = n1 / 100f;
- str = n.ToString(CultureInfo.InvariantCulture);
- }
- return str + "b";
- }
- if (number > 1000000)
- {
- float n = number / 1000000f;
- string str = "";
- if (n > 100)
- {
- int n1 = (int)(n * 10);
- n = n1 / 10f;
- str = n.ToString(CultureInfo.InvariantCulture);
- }
- else
- {
- int n1 = (int)(n * 100);
- n = n1 / 100f;
- str = n.ToString(CultureInfo.InvariantCulture);
- }
- return str + "m";
- }
- if (number > 10000)
- {
- float n = number / 1000f;
- string str = "";
- if (n > 100)
- {
- int n1 = (int)(n * 10);
- n = n1 / 10f;
- str = n.ToString(CultureInfo.InvariantCulture);
- }
- else
- {
- int n1 = (int)(n * 100);
- n = n1 / 100f;
- str = n.ToString(CultureInfo.InvariantCulture);
- }
- return str + "k";
- }
- return number.ToString();
- }
- /// <summary>
- /// 每日是否提醒标记
- /// </summary>
- public enum ToDayDontShowTag
- {
- MjToDiamond = 0,
- DiamondUsed, //钻石使用判断
- RelicIntensify,
- /// <summary>
- /// 经验转换
- /// </summary>
- EXConvert
- }
- //判断鼠标是否点在ui上
- public bool IsPointerOverGameObject(Vector2 mousePosition)
- {
- PointerEventData eventData = new PointerEventData(EventSystem.current);
- eventData.position = mousePosition;
- List<RaycastResult> raycastResults = new List<RaycastResult>();
- //发射射线判断是否点到ui
- EventSystem.current.RaycastAll(eventData, raycastResults);
- if (raycastResults.Count > 0)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- }
- }
|