using UnityEngine; using System.Collections; using System.Collections.Generic; using System; using System.Text.RegularExpressions; using UnityEngine.EventSystems; [RequireComponent(typeof(UIStackManager))] [RequireComponent(typeof(UILayerManager))] [RequireComponent(typeof(UIAnimManager))] public class UIManager : MonoBehaviour { private static GameObject s_UIManagerGo; private static UILayerManager s_UILayerManager; //UI层级管理器 private static UIAnimManager s_UIAnimManager; //UI动画管理器 private static UIStackManager s_UIStackManager; //UI栈管理器 //public static Camera s_UIcamera; //UICamera private static EventSystem s_EventSystem; static public Dictionary> s_UIs = new Dictionary>(); //打开的UI static public Dictionary> s_hideUIs = new Dictionary>(); //隐藏的UI #region 初始化 static bool isInit; public static void Init() { if(!isInit) { isInit = true; GameObject instance = GameObject.Find("UIManager"); if (instance == null) { instance = GameObjectManager.CreateGameObjectByPool("UIManager"); } UIManagerGo = instance; s_UILayerManager = instance.GetComponent(); s_UIAnimManager = instance.GetComponent(); s_UIStackManager = instance.GetComponent(); s_EventSystem = instance.GetComponentInChildren(); if (Application.isPlaying) { DontDestroyOnLoad(instance); } } } ///异步加载UIMnager //public static void InitAsync() //{ // GameObject instance = GameObject.Find("UIManager"); // if (instance == null) // { // GameObjectManager.CreateGameObjectByPoolAsync("UIManager",(obj)=> { // SetUIManager(obj); // }); // } // else // { // SetUIManager(instance); // } //} static void SetUIManager(GameObject instance) { UIManagerGo = instance; UILayerManager = instance.GetComponent(); UIAnimManager = instance.GetComponent(); DontDestroyOnLoad(instance); } public static UILayerManager UILayerManager { get { if (s_UILayerManager == null) { Init(); } return s_UILayerManager; } set { s_UILayerManager = value; } } public static UIAnimManager UIAnimManager { get { if (s_UILayerManager == null) { Init(); } return s_UIAnimManager; } set { s_UIAnimManager = value; } } public static UIStackManager UIStackManager { get { if (s_UIStackManager == null) { Init(); } return s_UIStackManager; } set { s_UIStackManager = value; } } public static EventSystem EventSystem { get { if (s_EventSystem == null) { Init(); } return s_EventSystem; } set { s_EventSystem = value; } } public static GameObject UIManagerGo { get { if (s_UIManagerGo == null) { Init(); } return s_UIManagerGo; } set { s_UIManagerGo = value; } } #endregion #region EventSystem public static void SetEventSystemEnable(bool enable) { if(EventSystem != null) { EventSystem.enabled = enable; } else { Debug.LogError("EventSystem.current is null !"); } } #endregion #region UICamera public static string[] GetCameraNames() { string[] list = new string[UILayerManager.UICameraList.Count]; for (int i = 0; i < UILayerManager.UICameraList.Count; i++) { list[i] = UILayerManager.UICameraList[i].m_key; } return list; } public static Camera GetCamera(string CameraKey = null) { var data = UILayerManager.GetUICameraDataByKey(CameraKey); return data.m_camera; } /// /// 将一个UI移动到另一个UICamera下 /// /// /// public static void ChangeUICamera(UIWindowBase ui, string cameraKey) { UILayerManager.SetLayer(ui, cameraKey); } /// /// 将一个UI重新放回它原本的UICamera下 /// /// /// public static void ResetUICamera(UIWindowBase ui) { UILayerManager.SetLayer(ui, ui.cameraKey); } #endregion #region UI的打开与关闭方法 /// /// 创建UI,如果不打开则存放在Hide列表中 /// /// /// public static T CreateUIWindow() where T : UIWindowBase { return (T)CreateUIWindow(typeof(T).Name); } public static UIWindowBase CreateUIWindow(string UIName) { GameObject UItmp = GameObjectManager.CreateGameObjectByPool(UIName, UIManagerGo); UIWindowBase UIWIndowBase = UItmp.GetComponent(); UISystemEvent.Dispatch(UIWIndowBase, UIEvent.OnInit); //派发OnInit事件 UIWIndowBase.windowStatus = UIWindowBase.WindowStatus.Create; try { UIWIndowBase.InitWindow(GetUIID(UIName)); } catch(Exception e) { Debug.LogError(UIName + "OnInit Exception: " + e.ToString());} AddHideUI(UIWIndowBase); UILayerManager.SetLayer(UIWIndowBase); //设置层级 return UIWIndowBase; } /// /// 打开UI /// /// UI名 /// 动画播放完毕回调 /// 回调传参` /// 返回打开的UI public static UIWindowBase OpenUIWindow(string UIName, UICallBack callback = null, params object[] objs) { UIWindowBase UIbase = GetHideUI(UIName); if (UIbase == null) { UIbase = CreateUIWindow(UIName); } RemoveHideUI(UIbase); AddUI(UIbase); UIStackManager.OnUIOpen(UIbase); UILayerManager.SetLayer(UIbase); //设置层级 UIbase.windowStatus = UIWindowBase.WindowStatus.OpenAnim; UISystemEvent.Dispatch(UIbase, UIEvent.OnOpen); //派发OnOpen事件 try { UIbase.OnOpen(); } catch (Exception e) { Debug.LogError(UIName + " OnOpen Exception: " + e.ToString()); } UISystemEvent.Dispatch(UIbase, UIEvent.OnOpened); //派发OnOpened事件 UIAnimManager.StartEnterAnim(UIbase, callback, objs); //播放动画 return UIbase; } public static T OpenUIWindow() where T : UIWindowBase { return (T)OpenUIWindow(typeof(T).Name); } /// /// 关闭UI /// /// 目标UI /// 是否播放关闭动画 /// 动画播放完毕回调 /// 回调传参 public static void CloseUIWindow(UIWindowBase UI,bool isPlayAnim = true ,UICallBack callback = null, params object[] objs) { RemoveUI(UI); //移除UI引用 UI.RemoveAllListener(); //s_UILayerManager.RemoveUI(UI); if (isPlayAnim) { //动画播放完毕删除UI if (callback != null) { callback += CloseUIWindowCallBack; } else { callback = CloseUIWindowCallBack; } UI.windowStatus = UIWindowBase.WindowStatus.CloseAnim; UIAnimManager.StartExitAnim(UI, callback, objs); } else { CloseUIWindowCallBack(UI, objs); } } static void CloseUIWindowCallBack(UIWindowBase UI, params object[] objs) { UI.windowStatus = UIWindowBase.WindowStatus.Close; UISystemEvent.Dispatch(UI, UIEvent.OnClose); //派发OnClose事件 try { UI.OnClose(); } catch (Exception e) { Debug.LogError(UI.UIName + " OnClose Exception: " + e.ToString()); } UIStackManager.OnUIClose(UI); AddHideUI(UI); UISystemEvent.Dispatch(UI, UIEvent.OnClosed); //派发OnOpened事件 } public static void CloseUIWindow(string UIname, bool isPlayAnim = true, UICallBack callback = null, params object[] objs) { UIWindowBase ui = GetUI(UIname); if (ui == null) { Debug.LogError("CloseUIWindow Error UI ->" + UIname + "<- not Exist!"); } else { CloseUIWindow(GetUI(UIname), isPlayAnim, callback, objs); } } public static void CloseUIWindow(bool isPlayAnim = true, UICallBack callback = null, params object[] objs) where T : UIWindowBase { CloseUIWindow(typeof(T).Name, isPlayAnim,callback, objs); } public static UIWindowBase ShowUI(string UIname) { UIWindowBase ui = GetUI(UIname); return ShowUI(ui); } public static UIWindowBase ShowUI(UIWindowBase ui) { ui.windowStatus = UIWindowBase.WindowStatus.Open; UISystemEvent.Dispatch(ui, UIEvent.OnShow); //派发OnShow事件 try { ui.Show(); ui.OnShow(); } catch (Exception e) { Debug.LogError(ui.UIName + " OnShow Exception: " + e.ToString()); } return ui; } public static UIWindowBase HideUI(string UIname) { UIWindowBase ui = GetUI(UIname); return HideUI(ui); } public static UIWindowBase HideUI(UIWindowBase ui) { ui.windowStatus = UIWindowBase.WindowStatus.Hide; UISystemEvent.Dispatch(ui, UIEvent.OnHide); //派发OnHide事件 try { ui.Hide(); ui.OnHide(); } catch (Exception e) { Debug.LogError(ui.UIName + " OnShow Exception: " + e.ToString()); } return ui; } public static void HideOtherUI(string UIName) { List keys = new List(s_UIs.Keys); for (int i = 0; i < keys.Count; i++) { List list = s_UIs[keys[i]]; for (int j = 0; j < list.Count; j++) { if (list[j].UIName != UIName) { HideUI(list[j]); } } } } public static void ShowOtherUI(string UIName) { List keys = new List(s_UIs.Keys); for (int i = 0; i < keys.Count; i++) { List list = s_UIs[keys[i]]; for (int j = 0; j < list.Count; j++) { if (list[j].UIName != UIName) { ShowUI(list[j]); } } } } /// /// 移除全部UI /// public static void CloseAllUI(bool isPlayerAnim = false) { List keys = new List(s_UIs.Keys); for (int i = 0; i < keys.Count; i++) { List list = s_UIs[keys[i]]; for(int j = 0;j( UICallBack callback, params object[] objs) where T : UIWindowBase { string UIName = typeof(T).Name; OpenUIAsync(UIName,callback,objs); } public static void OpenUIAsync(string UIName , UICallBack callback, params object[] objs) { ResourceManager.LoadAsync(UIName, (loadState,resObject) => { if(loadState.isDone) { OpenUIWindow(UIName, callback, objs); } }); } #endregion #region UI内存管理 public static void DestroyUI(UIWindowBase UI) { Debug.Log("UIManager DestroyUI " + UI.name); if (GetIsExitsHide(UI)) { RemoveHideUI(UI); } else if(GetIsExits(UI)) { RemoveUI(UI); } UISystemEvent.Dispatch(UI, UIEvent.OnDestroy); //派发OnDestroy事件 try { UI.Dispose(); } catch(Exception e) { Debug.LogError("OnDestroy :" + e.ToString()); } GameObjectManager.DestroyGameObjectByPool(UI.gameObject); } public static void DestroyAllUI() { DestroyAllActiveUI(); DestroyAllHideUI(); } #endregion #region 打开UI列表的管理 /// /// 删除所有打开的UI /// public static void DestroyAllActiveUI() { foreach (List uis in s_UIs.Values) { for (int i = 0; i < uis.Count; i++) { UISystemEvent.Dispatch(uis[i], UIEvent.OnDestroy); //派发OnDestroy事件 try { uis[i].Dispose(); } catch (Exception e) { Debug.LogError("OnDestroy :" + e.ToString()); } GameObjectManager.DestroyGameObjectByPool(uis[i].gameObject); } } s_UIs.Clear(); } public static T GetUI() where T : UIWindowBase { return (T)GetUI(typeof(T).Name); } public static UIWindowBase GetUI(string UIname) { if (!s_UIs.ContainsKey(UIname)) { //Debug.Log("!ContainsKey " + UIname); return null; } else { if (s_UIs[UIname].Count == 0) { //Debug.Log("s_UIs[UIname].Count == 0"); return null; } else { //默认返回最后创建的那一个 return s_UIs[UIname][s_UIs[UIname].Count - 1]; } } } public static UIBase GetUIBaseByEventKey(string eventKey) { string UIkey = eventKey.Split('.')[0]; string[] keyArray = UIkey.Split('_'); string uiEventKey = ""; UIBase uiTmp = null; for (int i = 0; i < keyArray.Length; i++) { if(i == 0) { uiEventKey = keyArray[0]; uiTmp = GetUIWindowByEventKey(uiEventKey); } else { uiEventKey += "_" + keyArray[i]; uiTmp = uiTmp.GetItemByKey(uiEventKey); } Debug.Log("uiEventKey " + uiEventKey); } return uiTmp; } static Regex uiKey = new Regex(@"(\S+)\d+"); static UIWindowBase GetUIWindowByEventKey(string eventKey) { string UIname = uiKey.Match(eventKey).Groups[1].Value; if (!s_UIs.ContainsKey(UIname)) { throw new Exception("UIManager: GetUIWindowByEventKey error dont find UI name: ->" + eventKey + "<- " + UIname); } List list = s_UIs[UIname]; for (int i = 0; i < list.Count; i++) { if(list[i].UIEventKey == eventKey) { return list[i]; } } throw new Exception("UIManager: GetUIWindowByEventKey error dont find UI name: ->" + eventKey + "<- " + UIname); } static bool GetIsExits(UIWindowBase UI) { if (!s_UIs.ContainsKey(UI.name)) { return false; } else { return s_UIs[UI.name].Contains(UI); } } static void AddUI(UIWindowBase UI) { if (!s_UIs.ContainsKey(UI.name)) { s_UIs.Add(UI.name, new List()); } s_UIs[UI.name].Add(UI); UI.Show(); } static void RemoveUI(UIWindowBase UI) { if (UI == null) { throw new Exception("UIManager: RemoveUI error UI is null: !"); } if (!s_UIs.ContainsKey(UI.name)) { throw new Exception("UIManager: RemoveUI error dont find UI name: ->" + UI.name + "<- " + UI); } if (!s_UIs[UI.name].Contains(UI)) { throw new Exception("UIManager: RemoveUI error dont find UI: ->" + UI.name + "<- " + UI); } else { s_UIs[UI.name].Remove(UI); } } static int GetUIID(string UIname) { if (!s_UIs.ContainsKey(UIname)) { return 0; } else { int id = s_UIs[UIname].Count; for (int i = 0; i < s_UIs[UIname].Count; i++) { if(s_UIs[UIname][i].UIID == id) { id++; i = 0; } } return id; } } public static int GetNormalUICount() { return UIStackManager.m_normalStack.Count; } #endregion #region 隐藏UI列表的管理 /// /// 删除所有隐藏的UI /// public static void DestroyAllHideUI() { foreach (List uis in s_hideUIs.Values) { for (int i = 0; i < uis.Count; i++) { UISystemEvent.Dispatch(uis[i], UIEvent.OnDestroy); //派发OnDestroy事件 try { uis[i].Dispose(); } catch (Exception e) { Debug.LogError("OnDestroy :" + e.ToString()); } GameObjectManager.DestroyGameObjectByPool(uis[i].gameObject); } } s_hideUIs.Clear(); } public static T GetHideUI() where T:UIWindowBase { string UIname = typeof(T).Name; return (T)GetHideUI(UIname); } /// /// 获取一个隐藏的UI,如果有多个同名UI,则返回最后创建的那一个 /// /// UI名 /// public static UIWindowBase GetHideUI(string UIname) { if (!s_hideUIs.ContainsKey(UIname)) { return null; } else { if (s_hideUIs[UIname].Count == 0) { return null; } else { UIWindowBase ui = s_hideUIs[UIname][s_hideUIs[UIname].Count - 1]; //默认返回最后创建的那一个 return ui; } } } static bool GetIsExitsHide(UIWindowBase UI) { if (!s_hideUIs.ContainsKey(UI.name)) { return false; } else { return s_hideUIs[UI.name].Contains(UI); } } static void AddHideUI(UIWindowBase UI) { if (!s_hideUIs.ContainsKey(UI.name)) { s_hideUIs.Add(UI.name, new List()); } s_hideUIs[UI.name].Add(UI); UI.Hide(); } static void RemoveHideUI(UIWindowBase UI) { if (UI == null) { throw new Exception("UIManager: RemoveUI error l_UI is null: !"); } if (!s_hideUIs.ContainsKey(UI.name)) { throw new Exception("UIManager: RemoveUI error dont find: " + UI.name + " " + UI); } if (!s_hideUIs[UI.name].Contains(UI)) { throw new Exception("UIManager: RemoveUI error dont find: " + UI.name + " " + UI); } else { s_hideUIs[UI.name].Remove(UI); } } #endregion } #region UI事件 代理 枚举 /// /// UI回调 /// /// public delegate void UICallBack(UIWindowBase UI, params object[] objs); public delegate void UIAnimCallBack(UIWindowBase UIbase, UICallBack callBack, params object[] objs); public enum UIType { GameUI=0, Fixed=1, Normal=2, TopBar=3, PopUp=4, } public enum UIEvent { OnOpen, OnOpened, OnClose, OnClosed, OnHide, OnShow, OnInit, OnDestroy, OnRefresh, OnStartEnterAnim, OnCompleteEnterAnim, OnStartExitAnim, OnCompleteExitAnim, } #endregion