using System.Collections.Generic; using UnityEngine; using System; namespace Snxxz.UI { public class WindowCenter : Singleton { public event Action windowBeforeOpenEvent; public event Action windowAfterOpenEvent; public event Action windowBeforeCloseEvent; public event Action windowAfterCloseEvent; public event Action jumpWindowCloseEvent; List closeAllIgnoreWindows = new List() { "MessageWin", "NewBieWin", "NewItemGetWin", "AttributePromoteShowWin" ,"DungeonBeginCoolDownWin","DungeonFightWin","StatusTipWin" ,"ScrollTipWin","MarqueeWin","ExperienceOpenWin","TrumpetWin","BattlePrepareCoolDownWin","DungeonGradeWin","BattleHintWin", "TreasureDungeonMissionHintWin","FairyGrabBossHintWin","DungeonFairyFeastHintWin", }; UIRoot m_UIRoot; public UIRoot uiRoot { get { if (m_UIRoot == null) { var prefab = Resources.Load("UI/Prefabs/UIRoot"); var instance = GameObject.Instantiate(prefab, Vector3.zero, Quaternion.identity); instance.name = "UIRoot"; m_UIRoot = instance.GetComponent(); if (Application.isPlaying) { GameObject.DontDestroyOnLoad(instance); } } return m_UIRoot; } } WindowAsyncLoad m_AnyncLoad; public WindowAsyncLoad asyncLoad { get { if (m_AnyncLoad == null) { var gameObject = new GameObject("WindowAnyncLoad"); m_AnyncLoad = gameObject.AddMissingComponent(); GameObject.DontDestroyOnLoad(gameObject); } return m_AnyncLoad; } } int m_PreCreateWindowNum = 0; public bool isPreCreating { get { return m_PreCreateWindowNum > 0; } } Dictionary windows = new Dictionary(); public void OpenFromLocal() where T : Window { var windowName = typeof(T).Name; T win = null; if (TryGetWindow(out win)) { if (win.windowState == Window.WindowState.Closed) { win.functionOrder = 0; win.playAnimation = true; win.Open(); } else { DebugEx.Log(string.Format("{0} 窗口已经打开!", typeof(T))); } } else { win = GetWindowInstance(true); if (win != null) { win.functionOrder = 0; win.playAnimation = true; win.Open(); } } } public T Open(bool _forceSync = false, int _functionalOrder = 0) where T : Window { if (VersionConfig.Get().isBanShu) { if (typeof(T) == typeof(VipRechargeWin)) { SysNotifyMgr.Instance.ShowTip("FuncNoOpen_VIP"); return null; } } var windowName = typeof(T).Name; string childWindow = string.Empty; if (_forceSync || AssetSource.uiFromEditor) { return OpenSingleWindow(_forceSync, _functionalOrder); } else { if (WindowConfig.Get().FindChildWindow(windowName, _functionalOrder, out childWindow)) { WindowTrim(childWindow); if (!windows.ContainsKey(childWindow)) { GetWindowInstanceAsync(childWindow, (bool _ok, UnityEngine.Object _object) => { if (_ok) { OpenSingleWindow(_forceSync, _functionalOrder); } }); return null; } else { return OpenSingleWindow(_forceSync, _functionalOrder); } } else { return OpenSingleWindow(_forceSync, _functionalOrder); } } } public T OpenWithoutAnimation() where T : Window { T win = null; if (TryGetWindow(out win)) { if (win.windowState == Window.WindowState.Closed) { win.functionOrder = 0; win.playAnimation = false; win.Open(); } else { DebugEx.Log(string.Format("{0} 窗口已经打开!", typeof(T))); } return (T)win; } win = GetWindowInstance(false); if (win != null) { win.functionOrder = 0; win.playAnimation = false; win.Open(); } return (T)win; } public T Get() where T : Window { T win = null; if (TryGetWindow(out win)) { return win; } else { DebugEx.LogFormat("没有找到窗口:{0}", typeof(T).Name); return null; } } public Window Get(string _window) { if (windows.ContainsKey(_window)) { return windows[_window]; } else { return null; } } public T Close() where T : Window { T win = null; if (TryGetWindow(out win)) { if (win.windowState != Window.WindowState.Closed) { win.CloseImmediately(); } else { DebugEx.Log(string.Format("{0} 窗口已经关闭!", typeof(T))); } } else { asyncLoad.StopTask(typeof(T).Name); DebugEx.Log(string.Format("{0} 窗口无法获得!", typeof(T))); } return win; } public T CloseImmediately() where T : Window { T win = null; if (TryGetWindow(out win)) { if (win.windowState != Window.WindowState.Closed) { win.CloseImmediately(); } else { DebugEx.Log(string.Format("{0} 窗口已经关闭!", typeof(T))); } } else { asyncLoad.StopTask(typeof(T).Name); DebugEx.Log(string.Format("{0} 窗口无法获得!", typeof(T))); } return win; } public List GetAll() { return new List(windows.Keys); } public void CloseAll(CloseAllIgnoreType _ignoreType = CloseAllIgnoreType.System) { foreach (var window in windows.Values) { if (window == null) { continue; } var isIgnore = false; switch (_ignoreType) { case CloseAllIgnoreType.Base: isIgnore = window.windowInfo.windowType <= WindowType.Base; break; case CloseAllIgnoreType.BaseAndCustom: isIgnore = window.windowInfo.windowType <= WindowType.Base || closeAllIgnoreWindows.Contains(window.name); break; case CloseAllIgnoreType.System: isIgnore = window.windowInfo.windowType >= WindowType.System; break; case CloseAllIgnoreType.Custom: isIgnore = closeAllIgnoreWindows.Contains(window.name); break; case CloseAllIgnoreType.SystemAndCustom: isIgnore = window.windowInfo.windowType >= WindowType.System || closeAllIgnoreWindows.Contains(window.name); break; } if (!isIgnore) { if (window.windowState == Window.WindowState.Opened || window.windowState == Window.WindowState.Opening) { window.CloseImmediately(); } } } asyncLoad.StopAllTasks(); } public void CloseOthers() where T : Window { foreach (var window in windows.Values) { if (window is T) { continue; } if (window != null) { if (window.windowState == Window.WindowState.Opened || window.windowState == Window.WindowState.Opening) { window.CloseImmediately(); } } } asyncLoad.StopAllTasks(); } public void CloseOthers(List _windowNames, CloseAllIgnoreType _ignoreType) { foreach (var window in windows.Values) { if (window == null) { continue; } var isIgnore = false; switch (_ignoreType) { case CloseAllIgnoreType.Base: isIgnore = _windowNames.Contains(window.name) || window.windowInfo.windowType <= WindowType.Base; break; case CloseAllIgnoreType.BaseAndCustom: isIgnore = _windowNames.Contains(window.name) || window.windowInfo.windowType <= WindowType.Base || closeAllIgnoreWindows.Contains(window.name); break; case CloseAllIgnoreType.System: isIgnore = _windowNames.Contains(window.name) || window.windowInfo.windowType >= WindowType.System; break; case CloseAllIgnoreType.Custom: isIgnore = _windowNames.Contains(window.name) || closeAllIgnoreWindows.Contains(window.name); break; case CloseAllIgnoreType.SystemAndCustom: isIgnore = _windowNames.Contains(window.name) || window.windowInfo.windowType >= WindowType.System || closeAllIgnoreWindows.Contains(window.name); break; } if (!isIgnore) { if (window.windowState == Window.WindowState.Opened || window.windowState == Window.WindowState.Opening) { window.CloseImmediately(); } } } asyncLoad.StopAllTasks(); } public void DestoryWinsByStage(WindowStage _windowStage) { switch (_windowStage) { case WindowStage.Launch: DestroyWin(); DestroyWin(); DestroyWin(); break; case WindowStage.Login: DestroyWin(); DestroyWin(); break; case WindowStage.SelectRole: DestroyWin(); DestroyWin(); DestroyWin(); break; default: break; } UnLoadAssetBundle(_windowStage); } public void DestroyWin() where T : Window { T win = null; if (TryGetWindow(out win)) { win.CloseImmediately(); GameObject.Destroy(win.gameObject); MonoBehaviour.Destroy(win); var name = typeof(T).Name; windows[name] = null; windows.Remove(typeof(T).Name); } else { DebugEx.Log(string.Format("{0} 窗口无法获得!", typeof(T))); } } public void UnLoadAssetBundle(WindowStage _windowStage) { if (!AssetSource.uiFromEditor) { switch (_windowStage) { case WindowStage.Launch: break; case WindowStage.Login: AssetBundleUtility.Instance.UnloadAsset("ui/prioritywindow", "LoginWin"); AssetBundleUtility.Instance.UnloadAsset("ui/prioritywindow", "ServerListWin"); break; case WindowStage.SelectRole: AssetBundleUtility.Instance.UnloadAsset("ui/prioritywindow", "CreateRoleWin"); AssetBundleUtility.Instance.UnloadAsset("ui/prioritywindow", "SelectRoleWin"); break; default: break; } } } public bool CheckOpen() where T : Window { T win = null; if (TryGetWindow(out win)) { var open = win.windowState == Window.WindowState.Opened || win.windowState == Window.WindowState.Opening; return open; } else { return false; } } public bool CheckOpen(string _windowName) { if (windows.ContainsKey(_windowName) && windows[_windowName] != null) { var window = windows[_windowName]; return window.windowState == Window.WindowState.Opened || window.windowState == Window.WindowState.Opening; } else { return false; } } public void PreCreateWindows() { m_PreCreateWindowNum = 0; } private void AsyncLoadWindowCallBack(bool _ok, UnityEngine.Object _object) { m_PreCreateWindowNum--; } public bool ExitAnyFullScreenOrMaskWin() { bool exit = false; foreach (var window in windows.Values) { if (window.windowInfo.needMask || window.windowInfo.fullScreen) { if (window.windowState == Window.WindowState.Opened || window.windowState == Window.WindowState.Opening) { exit = true; break; } } } return exit; } /// /// 是否存在大于或等于这个层级的全屏或有模糊背景的界面 /// /// /// public bool ExitAnyFullScreenOrMaskWinLEqual(WindowType _windowType) { bool exit = false; foreach (var window in windows.Values) { if ((window.windowInfo.needMask || window.windowInfo.fullScreen) && window.windowInfo.windowType >= _windowType) { if (window.windowState == Window.WindowState.Opened || window.windowState == Window.WindowState.Opening) { exit = true; break; } } } return exit; } internal void NotifyBeforeOpen(T _window) where T : Window { if (windowBeforeOpenEvent != null) { windowBeforeOpenEvent(_window); } } internal void NotifyAfterOpen(T _window) where T : Window { if (windowAfterOpenEvent != null) { windowAfterOpenEvent(_window); } } internal void NotifyBeforeClose(T _window) where T : Window { if (windowBeforeCloseEvent != null) { windowBeforeCloseEvent(_window); } } internal void NotifyAfterClose(T _window) where T : Window { if (windowAfterCloseEvent != null) { windowAfterCloseEvent(_window); } } internal void JumpNotifyAfterClose(T _window) where T : Window { if (jumpWindowCloseEvent != null) { jumpWindowCloseEvent(_window); } } private T OpenSingleWindow(bool _forceSync, int _functionalOrder) where T : Window { T win = null; if (TryGetWindow(out win)) { if (win.windowState == Window.WindowState.Closed) { win.functionOrder = _functionalOrder; win.playAnimation = true; win.Open(); } else { DebugEx.Log(string.Format("{0} 窗口已经打开!", typeof(T))); } return (T)win; } if (_forceSync || AssetSource.uiFromEditor) { win = GetWindowInstance(false); if (win != null) { win.functionOrder = _functionalOrder; win.playAnimation = true; win.Open(); } return (T)win; } else { GetWindowInstanceAsync( (bool ok, UnityEngine.Object _object) => { if (TryGetWindow(out win)) { if (win.windowState == Window.WindowState.Closed) { win.functionOrder = _functionalOrder; win.playAnimation = true; win.Open(); } else { DebugEx.Log(string.Format("{0} 窗口已经打开!", typeof(T))); } } } ); return null; } } private bool TryGetWindow(out T _win) where T : Window { var windowName = typeof(T).Name; WindowTrim(windowName); if (windows.ContainsKey(windowName)) { _win = (T)windows[windowName]; return true; } else { _win = null; return false; } } private void WindowTrim(string _windowName) { if (windows.ContainsKey(_windowName)) { if (windows[_windowName] == null || windows[_windowName].gameObject == null) { windows.Remove(_windowName); } } } private T GetWindowInstance(bool _fromLocal) where T : Window { var prefabName = typeof(T).Name; if (windows.ContainsKey(prefabName)) { return windows[prefabName] as T; } else { var prefab = _fromLocal ? Resources.Load(StringUtility.Contact("UI/Prefabs/", prefabName)) : UILoader.LoadWindow(prefabName); prefab.SetActive(false); var instance = GameObject.Instantiate(prefab); if (AssetSource.uiFromEditor) { prefab.SetActive(true); } UILoader.UnLoadWindowAsset(prefabName); instance.name = prefabName; var window = instance.GetComponent(); if (window != null) { var windowName = typeof(T).Name; windows[windowName] = window; } else { DebugEx.LogFormat("无法获得 {0} 的资源!", prefabName); } return window; } } private void GetWindowInstanceAsync(Action _callBack) where T : Window { GetWindowInstanceAsync(typeof(T).Name, _callBack); } private void GetWindowInstanceAsync(string _windowName, Action _callBack) { Action addAction = (bool _ok, UnityEngine.Object _object) => { var prefabName = _windowName; Window window = null; if (!windows.ContainsKey(_windowName)) { var prefab = _object as GameObject; prefab.SetActive(false); var instance = GameObject.Instantiate(prefab); if (AssetSource.uiFromEditor) { prefab.SetActive(true); } UILoader.UnLoadWindowAsset(prefabName); instance.name = _windowName; window = (Window)instance.GetComponent(_windowName); if (window != null) { windows[_windowName] = (Window)window; } else { Debug.LogFormat("无法获得 {0} 的资源!", _windowName); } } if (_callBack != null) { _callBack(_ok && window != null, _object); } }; asyncLoad.PushTask(new WindowAsyncLoad.Task(_windowName, addAction)); } public enum WindowStage { Launch, Login, SelectRole, Other, } public enum CloseAllIgnoreType { Base = 1, System = 2, Custom = 3, BaseAndCustom = 4, SystemAndCustom = 5, } } }