From 642eab28496966a742aae582928dd0bf56461590 Mon Sep 17 00:00:00 2001 From: ALEXTANG <574809918@qq.com> Date: Wed, 7 Sep 2022 17:35:58 +0800 Subject: [PATCH] UIWindows --- .../UIModule/Scripts/Mgr/IUIController.cs | 7 -- .../UIModule/Scripts/Mgr/UIControllerBase.cs | 7 ++ ...oller.cs.meta => UIControllerBase.cs.meta} | 0 .../Runtime/UIModule/Scripts/Mgr/UIManager.cs | 93 +++++-------------- .../UIModule/Scripts/Mgr/UISys.Register.cs | 57 ++++++++++-- .../Runtime/UIModule/Scripts/Mgr/UISys.cs | 10 +- .../UIModule/Scripts/Mgr/UIWindowStack.cs | 13 ++- 7 files changed, 86 insertions(+), 101 deletions(-) delete mode 100644 Assets/TEngine/Scripts/Runtime/UIModule/Scripts/Mgr/IUIController.cs create mode 100644 Assets/TEngine/Scripts/Runtime/UIModule/Scripts/Mgr/UIControllerBase.cs rename Assets/TEngine/Scripts/Runtime/UIModule/Scripts/Mgr/{IUIController.cs.meta => UIControllerBase.cs.meta} (100%) diff --git a/Assets/TEngine/Scripts/Runtime/UIModule/Scripts/Mgr/IUIController.cs b/Assets/TEngine/Scripts/Runtime/UIModule/Scripts/Mgr/IUIController.cs deleted file mode 100644 index a54614e4..00000000 --- a/Assets/TEngine/Scripts/Runtime/UIModule/Scripts/Mgr/IUIController.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace TEngine.Runtime.UIModule -{ - public interface IUIController - { - void RegisterUIEvent(); - } -} \ No newline at end of file diff --git a/Assets/TEngine/Scripts/Runtime/UIModule/Scripts/Mgr/UIControllerBase.cs b/Assets/TEngine/Scripts/Runtime/UIModule/Scripts/Mgr/UIControllerBase.cs new file mode 100644 index 00000000..c9e9bfe0 --- /dev/null +++ b/Assets/TEngine/Scripts/Runtime/UIModule/Scripts/Mgr/UIControllerBase.cs @@ -0,0 +1,7 @@ +namespace TEngine.Runtime.UIModule +{ + public abstract class UIControllerBase + { + public abstract void RegisterUIEvent(); + } +} \ No newline at end of file diff --git a/Assets/TEngine/Scripts/Runtime/UIModule/Scripts/Mgr/IUIController.cs.meta b/Assets/TEngine/Scripts/Runtime/UIModule/Scripts/Mgr/UIControllerBase.cs.meta similarity index 100% rename from Assets/TEngine/Scripts/Runtime/UIModule/Scripts/Mgr/IUIController.cs.meta rename to Assets/TEngine/Scripts/Runtime/UIModule/Scripts/Mgr/UIControllerBase.cs.meta diff --git a/Assets/TEngine/Scripts/Runtime/UIModule/Scripts/Mgr/UIManager.cs b/Assets/TEngine/Scripts/Runtime/UIModule/Scripts/Mgr/UIManager.cs index 11620eeb..9d186562 100644 --- a/Assets/TEngine/Scripts/Runtime/UIModule/Scripts/Mgr/UIManager.cs +++ b/Assets/TEngine/Scripts/Runtime/UIModule/Scripts/Mgr/UIManager.cs @@ -51,9 +51,9 @@ namespace TEngine.Runtime.UIModule for (int i = 0; i < (int)WindowStackIndex.StackMax; i++) { m_listWindowStack[i] = new UIWindowStack(); - m_listWindowStack[i].m_stackIndex = (WindowStackIndex)i; - m_listWindowStack[i].m_baseOrder = baseOrder; - m_listWindowStack[i].m_parentTrans = windowRoot; + m_listWindowStack[i].StackIndex = (WindowStackIndex)i; + m_listWindowStack[i].BaseOrder = baseOrder; + m_listWindowStack[i].ParentTrans = windowRoot; baseOrder += 1000; } @@ -65,7 +65,7 @@ namespace TEngine.Runtime.UIModule CanvasScaler canvasScale = m_canvas.GetComponent(); if (canvasScale != null) { - canvasScale.referenceResolution = new Vector2(UISys.DesginWidth, UISys.DesginHeight); + canvasScale.referenceResolution = new Vector2(UISys.DesignWidth, UISys.DesignHeight); float sceneScale = Screen.width / (float)Screen.height; float designScale = canvasScale.referenceResolution.x / canvasScale.referenceResolution.y; canvasScale.matchWidthOrHeight = sceneScale > designScale ? 1 : 0; @@ -109,8 +109,7 @@ namespace TEngine.Runtime.UIModule } #region Methods - - public T ShowWindow(bool isAsync = false) where T : UIWindow, new() + public T ShowWindow() where T : UIWindow, new() { string typeName = GetWindowTypeName(); @@ -118,12 +117,11 @@ namespace TEngine.Runtime.UIModule if (window == null) { window = new T(); - if (!CreateWindowByType(window, typeName, isAsync)) + if (!CreateWindowByType(window, typeName)) { return null; } } - ShowWindow(window, -1); return window; } @@ -171,7 +169,7 @@ namespace TEngine.Runtime.UIModule #endregion - private bool CreateWindowByType(UIWindow window, string typeName, bool async = false) + private bool CreateWindowByType(UIWindow window, string typeName) { //先判断是否有缓存 GameObject uiGo = null; @@ -184,59 +182,12 @@ namespace TEngine.Runtime.UIModule } UIWindowStack windowStack = GetUIWindowStack(window); + uiGo = TResources.Load(resPath, windowStack.ParentTrans); - if (async) - { - TResources.LoadAsync(resPath, (obj) => - { - if (obj == null) - { - Debug.LogErrorFormat("CreateWindowByType failed, typeName:{0}, load prefab failed: {1}", - typeName, resPath); - } - - if (obj != null && windowStack.m_parentTrans != null) - { - obj.transform.SetParent(windowStack.m_parentTrans); - } - - obj.name = typeName; - - window.AllocWindowId(); - - var rectTrans_ = obj.transform as RectTransform; - if (window.NeedCenterUI()) - { - rectTrans_.SetMax(); //localPosition = new Vector3(0, 0, 0); - } - - rectTrans_.localRotation = Quaternion.identity; - rectTrans_.localScale = Vector3.one; - - if (!window.Create(this, obj)) - { - Debug.LogErrorFormat("window create failed, typeName:{0}", typeName); - if (obj != null) - { - Object.Destroy(obj); - obj = null; - } - } - - m_typeToInst[typeName] = window; - m_allWindow[window.WindowId] = window; - m_tmpWindowListDirty = true; - }); - return true; - } - - uiGo = TResources.Load(resPath, windowStack.m_parentTrans); if (uiGo == null) { Debug.LogErrorFormat("CreateWindowByType failed, typeName:{0}, load prefab failed: {1}", typeName, resPath); - //UISys.Mgr.ShowTipMsg(TextDefine.DOWNLOAD_TIP_UI); - //GameEvent.Get().ShowDownloadUI(); return false; } @@ -247,7 +198,7 @@ namespace TEngine.Runtime.UIModule RectTransform rectTrans = uiGo.transform as RectTransform; if (window.NeedCenterUI()) { - rectTrans.SetMax(); //localPosition = new Vector3(0, 0, 0); + rectTrans.SetMax(); } rectTrans.localRotation = Quaternion.identity; @@ -289,7 +240,7 @@ namespace TEngine.Runtime.UIModule private void ShowWindow(UIWindow window, int showIndex) { UIWindowStack windowStack = GetUIWindowStack(window); - List windowList = windowStack.m_windowList; + List windowList = windowStack.WindowsList; int resortIndex = -1; int findIndex = windowList.IndexOf(window.WindowId); if (findIndex >= 0) @@ -306,23 +257,23 @@ namespace TEngine.Runtime.UIModule private void ResortStackUI(UIWindowStack stack, int startIdx) { - if (stack.m_windowList.Count > 0) + if (stack.WindowsList.Count > 0) { - startIdx = startIdx < 0 ? (stack.m_windowList.Count - 1) : startIdx; - for (int i = startIdx; i < stack.m_windowList.Count; i++) + startIdx = startIdx < 0 ? (stack.WindowsList.Count - 1) : startIdx; + for (int i = startIdx; i < stack.WindowsList.Count; i++) { - uint windowId = stack.m_windowList[i]; + uint windowId = stack.WindowsList[i]; UIWindow window = FindWindow(windowId); if (window != null) { int order; if (window.IsFixedSortingOrder) { - order = stack.m_baseOrder + window.FixedAdditionalOrder; + order = stack.BaseOrder + window.FixedAdditionalOrder; } else { - order = stack.m_baseOrder + i * UIWindow.MaxCanvasSortingOrder; + order = stack.BaseOrder + i * UIWindow.MaxCanvasSortingOrder; } window.SortingOrder = order; @@ -333,12 +284,12 @@ namespace TEngine.Runtime.UIModule private void ShowTopUI(UIWindowStack stack) { - if (stack.m_windowList.Count > 0) + if (stack.WindowsList.Count > 0) { bool hasTop = false; - for (int i = stack.m_windowList.Count - 1; i >= 0; i--) + for (int i = stack.WindowsList.Count - 1; i >= 0; i--) { - uint windowId = stack.m_windowList[i]; + uint windowId = stack.WindowsList[i]; UIWindow window = FindWindow(windowId); if (window != null) { @@ -370,7 +321,7 @@ namespace TEngine.Runtime.UIModule continue; } - var listWindow = stack.m_windowList; + var listWindow = stack.WindowsList; for (int k = 0; k < listWindow.Count; k++) { var winId = listWindow[k]; @@ -387,7 +338,6 @@ namespace TEngine.Runtime.UIModule } } } - //SceneSys.Instance.CameraMgr.SetSceneCameraEnableByUI(true); } public UIWindow FindWindow(uint windowId) @@ -422,7 +372,6 @@ namespace TEngine.Runtime.UIModule int findIndex = windowStack.FindIndex(window.WindowId); - //window.Destroy(); DestroyWindowObject(window); ResortStackUI(windowStack, findIndex); @@ -441,7 +390,7 @@ namespace TEngine.Runtime.UIModule uint windowId = window.WindowId; m_allWindow.Remove(windowId); UIWindowStack windowStack = GetUIWindowStack(window); - windowStack.m_windowList.Remove(windowId); + windowStack.WindowsList.Remove(windowId); window.Destroy(); m_tmpWindowListDirty = true; } diff --git a/Assets/TEngine/Scripts/Runtime/UIModule/Scripts/Mgr/UISys.Register.cs b/Assets/TEngine/Scripts/Runtime/UIModule/Scripts/Mgr/UISys.Register.cs index 6477da79..b1071c59 100644 --- a/Assets/TEngine/Scripts/Runtime/UIModule/Scripts/Mgr/UISys.Register.cs +++ b/Assets/TEngine/Scripts/Runtime/UIModule/Scripts/Mgr/UISys.Register.cs @@ -1,21 +1,62 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; +using System.Reflection; namespace TEngine.Runtime.UIModule { public partial class UISys { - private List m_listController = new List(); + /// + /// UIController + /// + private readonly List _listController = new List(); - public void RegisterAllController() + /// + /// 自动注册UIController + /// + /// nameSpace TEngine.Runtime.UIModule + private void RegisterAllController() { - //AddController(); + Type handlerBaseType = typeof(UIControllerBase); + Assembly assembly = Assembly.GetExecutingAssembly(); + Type[] types = assembly.GetTypes(); + for (int i = 0; i < types.Length; i++) + { + if (!types[i].IsClass || types[i].IsAbstract) + { + continue; + } + if (types[i].BaseType == handlerBaseType) + { + UIControllerBase controller = (UIControllerBase)Activator.CreateInstance(types[i]); + AddController(controller); + } + } } - private void AddController() where T : IUIController, new() + private void AddController(UIControllerBase controller) { - for (int i = 0; i < m_listController.Count; i++) + for (int i = 0; i < _listController.Count; i++) { - var type = m_listController[i].GetType(); + var type = _listController[i].GetType(); + + if (type == controller.GetType()) + { + Log.Error(Utility.Text.Format("repeat controller type: {0}", controller.GetType())); + return; + } + } + + _listController.Add(controller); + + controller.RegisterUIEvent(); + } + + public void AddController() where T : UIControllerBase, new() + { + for (int i = 0; i < _listController.Count; i++) + { + var type = _listController[i].GetType(); if (type == typeof(T)) { @@ -26,7 +67,7 @@ namespace TEngine.Runtime.UIModule var controller = new T(); - m_listController.Add(controller); + _listController.Add(controller); controller.RegisterUIEvent(); } diff --git a/Assets/TEngine/Scripts/Runtime/UIModule/Scripts/Mgr/UISys.cs b/Assets/TEngine/Scripts/Runtime/UIModule/Scripts/Mgr/UISys.cs index d3e883b1..5906545a 100644 --- a/Assets/TEngine/Scripts/Runtime/UIModule/Scripts/Mgr/UISys.cs +++ b/Assets/TEngine/Scripts/Runtime/UIModule/Scripts/Mgr/UISys.cs @@ -2,14 +2,11 @@ { public partial class UISys : BehaviourSingleton { - public static int DesginWidth => 750; + public static int DesignWidth => 750; - public static int DesginHeight => 1624; + public static int DesignHeight => 1624; - public static UIManager Mgr - { - get { return UIManager.Instance; } - } + public static UIManager Mgr => UIManager.Instance; public override void Active() { @@ -20,7 +17,6 @@ public override void Update() { - base.Update(); UIManager.Instance.OnUpdate(); } } diff --git a/Assets/TEngine/Scripts/Runtime/UIModule/Scripts/Mgr/UIWindowStack.cs b/Assets/TEngine/Scripts/Runtime/UIModule/Scripts/Mgr/UIWindowStack.cs index 4bc2ad48..65ae1db9 100644 --- a/Assets/TEngine/Scripts/Runtime/UIModule/Scripts/Mgr/UIWindowStack.cs +++ b/Assets/TEngine/Scripts/Runtime/UIModule/Scripts/Mgr/UIWindowStack.cs @@ -12,21 +12,20 @@ namespace TEngine.Runtime.UIModule public class UIWindowStack { - public WindowStackIndex m_stackIndex; - public int m_baseOrder = 0; - public List m_windowList = new List(); - public Transform m_parentTrans; + public WindowStackIndex StackIndex; + public int BaseOrder = 0; + public readonly List WindowsList = new List(); + public Transform ParentTrans; public int FindIndex(uint windowId) { - for (int i = 0; i < m_windowList.Count; i++) + for (int i = 0; i < WindowsList.Count; i++) { - if (m_windowList[i] == windowId) + if (WindowsList[i] == windowId) { return i; } } - return -1; } }