| New file |
| | |
| | | using System.Collections; |
| | | using System.Collections.Generic; |
| | | using UnityEngine; |
| | | using System; |
| | | |
| | | namespace Snxxz.UI |
| | | { |
| | | public class WindowController : SingletonMonobehaviour<WindowController> |
| | | { |
| | | WindowAsyncLoad m_AnyncLoad; |
| | | public WindowAsyncLoad asyncLoad { |
| | | get { |
| | | if (m_AnyncLoad == null) |
| | | { |
| | | var gameObject = new GameObject("WindowAnyncLoad"); |
| | | m_AnyncLoad = gameObject.AddMissingComponent<WindowAsyncLoad>(); |
| | | GameObject.DontDestroyOnLoad(gameObject); |
| | | } |
| | | |
| | | return m_AnyncLoad; |
| | | } |
| | | } |
| | | |
| | | Dictionary<string, Window> windows = new Dictionary<string, Window>(); |
| | | |
| | | List<OpenCommand> openCommands = new List<OpenCommand>(); |
| | | List<CloseCommand> closeCommands = new List<CloseCommand>(); |
| | | |
| | | public void OpenFromLocal<T>() where T : Window |
| | | { |
| | | var command = new OpenCommand() |
| | | { |
| | | name = typeof(T).Name, |
| | | sync = true, |
| | | playAnimatioin = false, |
| | | fromBuiltIn = true, |
| | | functionOrder = 0 |
| | | }; |
| | | |
| | | PushOpenCommand(command); |
| | | } |
| | | |
| | | public void OpenFromLocal(string name) |
| | | { |
| | | var command = new OpenCommand() |
| | | { |
| | | name = name, |
| | | sync = true, |
| | | playAnimatioin = false, |
| | | fromBuiltIn = true, |
| | | functionOrder = 0 |
| | | }; |
| | | |
| | | PushOpenCommand(command); |
| | | } |
| | | |
| | | public void Open<T>(bool forceSync = false, int functionOrder = 0) where T : Window |
| | | { |
| | | var command = new OpenCommand() |
| | | { |
| | | name = typeof(T).Name, |
| | | sync = forceSync, |
| | | playAnimatioin = true, |
| | | fromBuiltIn = false, |
| | | functionOrder = functionOrder |
| | | }; |
| | | |
| | | PushOpenCommand(command); |
| | | } |
| | | |
| | | public void Open(string name, bool forceSync = false, int functionOrder = 0) |
| | | { |
| | | var command = new OpenCommand() |
| | | { |
| | | name = name, |
| | | sync = forceSync, |
| | | playAnimatioin = true, |
| | | fromBuiltIn = false, |
| | | functionOrder = functionOrder |
| | | }; |
| | | |
| | | PushOpenCommand(command); |
| | | } |
| | | |
| | | public void OpenWithoutAnimation<T>() where T : Window |
| | | { |
| | | var command = new OpenCommand() |
| | | { |
| | | name = typeof(T).Name, |
| | | sync = false, |
| | | playAnimatioin = false, |
| | | fromBuiltIn = false, |
| | | functionOrder = 0 |
| | | }; |
| | | |
| | | PushOpenCommand(command); |
| | | } |
| | | |
| | | public void OpenWithoutAnimation(string name) |
| | | { |
| | | var command = new OpenCommand() |
| | | { |
| | | name = name, |
| | | sync = false, |
| | | playAnimatioin = false, |
| | | fromBuiltIn = false, |
| | | functionOrder = 0 |
| | | }; |
| | | |
| | | PushOpenCommand(command); |
| | | } |
| | | |
| | | private void PushOpenCommand(OpenCommand command) |
| | | { |
| | | var name = command.name; |
| | | var index = closeCommands.FindIndex((x) => { return x.name == name; }); |
| | | if (index != -1) |
| | | { |
| | | closeCommands.RemoveAt(index); |
| | | } |
| | | |
| | | var exist = openCommands.Exists((x) => { return x.name == name; }); |
| | | if (!exist) |
| | | { |
| | | openCommands.Add(command); |
| | | } |
| | | } |
| | | |
| | | public void Close<T>() where T : Window |
| | | { |
| | | var name = typeof(T).Name; |
| | | |
| | | var index = openCommands.FindIndex((x) => { return x.name == name; }); |
| | | if (index != -1) |
| | | { |
| | | openCommands.RemoveAt(index); |
| | | } |
| | | |
| | | var exist = closeCommands.Exists((x) => { return x.name == name; }); |
| | | if (!exist) |
| | | { |
| | | closeCommands.Add(new CloseCommand() |
| | | { |
| | | name = name, |
| | | }); |
| | | } |
| | | |
| | | } |
| | | |
| | | private void LateUpdate() |
| | | { |
| | | foreach (var command in closeCommands) |
| | | { |
| | | Window window = null; |
| | | if (windows.TryGetValue(command.name, out window)) |
| | | { |
| | | if (window.windowState != Window.WindowState.Closed) |
| | | { |
| | | window.CloseImmediately(); |
| | | } |
| | | } |
| | | |
| | | asyncLoad.StopTask(command.name); |
| | | } |
| | | |
| | | closeCommands.Clear(); |
| | | |
| | | foreach (var command in openCommands) |
| | | { |
| | | Window window = null; |
| | | if (windows.TryGetValue(command.name, out window)) |
| | | { |
| | | if (window.windowState == Window.WindowState.Closed) |
| | | { |
| | | window.playAnimation = command.playAnimatioin; |
| | | window.functionOrder = command.functionOrder; |
| | | window.Open(); |
| | | } |
| | | else |
| | | { |
| | | DebugEx.LogFormat("{0} 窗口已经打开!", command.name); |
| | | } |
| | | } |
| | | else |
| | | { |
| | | ExecuteFirstOpen(command); |
| | | } |
| | | } |
| | | |
| | | openCommands.Clear(); |
| | | } |
| | | |
| | | private void ExecuteFirstOpen(OpenCommand command) |
| | | { |
| | | GetWindowPrefab(command, (bool ok, GameObject prefab) => |
| | | { |
| | | if (ok) |
| | | { |
| | | CreateWindowInstance(command, prefab); |
| | | Window window = null; |
| | | if (windows.TryGetValue(command.name, out window)) |
| | | { |
| | | window.playAnimation = command.playAnimatioin; |
| | | window.functionOrder = command.functionOrder; |
| | | window.Open(); |
| | | } |
| | | } |
| | | }); |
| | | } |
| | | |
| | | private void GetWindowPrefab(OpenCommand command, Action<bool, GameObject> callBack) |
| | | { |
| | | if (command.sync) |
| | | { |
| | | var name = command.name; |
| | | var fromBuiltIn = command.fromBuiltIn; |
| | | var prefab = fromBuiltIn ? BuiltInLoader.LoadPrefab(name) : UILoader.LoadWindow(name); |
| | | |
| | | if (callBack != null) |
| | | { |
| | | callBack(prefab != null, prefab); |
| | | } |
| | | } |
| | | else |
| | | { |
| | | var name = command.name; |
| | | var task = new WindowAsyncLoad.Task(name, (bool ok, UnityEngine.Object @object) => |
| | | { |
| | | var prefab = @object as GameObject; |
| | | if (callBack != null) |
| | | { |
| | | callBack(prefab != null, prefab); |
| | | } |
| | | }); |
| | | |
| | | asyncLoad.PushTask(task); |
| | | } |
| | | } |
| | | |
| | | private void CreateWindowInstance(OpenCommand command, GameObject prefab) |
| | | { |
| | | var name = command.name; |
| | | Window window = null; |
| | | if (!windows.ContainsKey(name)) |
| | | { |
| | | prefab.SetActive(false); |
| | | var instance = GameObject.Instantiate(prefab); |
| | | |
| | | if (AssetSource.uiFromEditor) |
| | | { |
| | | prefab.SetActive(true); |
| | | } |
| | | |
| | | if (command.fromBuiltIn) |
| | | { |
| | | BuiltInLoader.UnLoadPrefab(name); |
| | | } |
| | | else |
| | | { |
| | | UILoader.UnLoadWindowAsset(name); |
| | | } |
| | | |
| | | window = instance.GetComponent<Window>(); |
| | | var typeName = window.GetType().Name; |
| | | instance.name = typeName; |
| | | if (window != null) |
| | | { |
| | | windows[typeName] = window; |
| | | } |
| | | else |
| | | { |
| | | Debug.LogFormat("无法获得 {0} 的资源!", name); |
| | | } |
| | | } |
| | | } |
| | | |
| | | public struct OpenCommand |
| | | { |
| | | public string name; |
| | | public bool sync; |
| | | public int functionOrder; |
| | | public bool playAnimatioin; |
| | | public bool fromBuiltIn; |
| | | } |
| | | |
| | | public struct CloseCommand |
| | | { |
| | | public string name; |
| | | } |
| | | |
| | | } |
| | | |
| | | } |
| | | |