UIWindows

This commit is contained in:
ALEXTANG
2022-09-07 17:35:58 +08:00
parent af7ff8749b
commit 642eab2849
7 changed files with 86 additions and 101 deletions

View File

@@ -1,7 +0,0 @@
namespace TEngine.Runtime.UIModule
{
public interface IUIController
{
void RegisterUIEvent();
}
}

View File

@@ -0,0 +1,7 @@
namespace TEngine.Runtime.UIModule
{
public abstract class UIControllerBase
{
public abstract void RegisterUIEvent();
}
}

View File

@@ -51,9 +51,9 @@ namespace TEngine.Runtime.UIModule
for (int i = 0; i < (int)WindowStackIndex.StackMax; i++) for (int i = 0; i < (int)WindowStackIndex.StackMax; i++)
{ {
m_listWindowStack[i] = new UIWindowStack(); m_listWindowStack[i] = new UIWindowStack();
m_listWindowStack[i].m_stackIndex = (WindowStackIndex)i; m_listWindowStack[i].StackIndex = (WindowStackIndex)i;
m_listWindowStack[i].m_baseOrder = baseOrder; m_listWindowStack[i].BaseOrder = baseOrder;
m_listWindowStack[i].m_parentTrans = windowRoot; m_listWindowStack[i].ParentTrans = windowRoot;
baseOrder += 1000; baseOrder += 1000;
} }
@@ -65,7 +65,7 @@ namespace TEngine.Runtime.UIModule
CanvasScaler canvasScale = m_canvas.GetComponent<CanvasScaler>(); CanvasScaler canvasScale = m_canvas.GetComponent<CanvasScaler>();
if (canvasScale != null) 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 sceneScale = Screen.width / (float)Screen.height;
float designScale = canvasScale.referenceResolution.x / canvasScale.referenceResolution.y; float designScale = canvasScale.referenceResolution.x / canvasScale.referenceResolution.y;
canvasScale.matchWidthOrHeight = sceneScale > designScale ? 1 : 0; canvasScale.matchWidthOrHeight = sceneScale > designScale ? 1 : 0;
@@ -109,8 +109,7 @@ namespace TEngine.Runtime.UIModule
} }
#region Methods #region Methods
public T ShowWindow<T>() where T : UIWindow, new()
public T ShowWindow<T>(bool isAsync = false) where T : UIWindow, new()
{ {
string typeName = GetWindowTypeName<T>(); string typeName = GetWindowTypeName<T>();
@@ -118,12 +117,11 @@ namespace TEngine.Runtime.UIModule
if (window == null) if (window == null)
{ {
window = new T(); window = new T();
if (!CreateWindowByType(window, typeName, isAsync)) if (!CreateWindowByType(window, typeName))
{ {
return null; return null;
} }
} }
ShowWindow(window, -1); ShowWindow(window, -1);
return window; return window;
} }
@@ -171,7 +169,7 @@ namespace TEngine.Runtime.UIModule
#endregion #endregion
private bool CreateWindowByType(UIWindow window, string typeName, bool async = false) private bool CreateWindowByType(UIWindow window, string typeName)
{ {
//先判断是否有缓存 //先判断是否有缓存
GameObject uiGo = null; GameObject uiGo = null;
@@ -184,59 +182,12 @@ namespace TEngine.Runtime.UIModule
} }
UIWindowStack windowStack = GetUIWindowStack(window); 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) if (uiGo == null)
{ {
Debug.LogErrorFormat("CreateWindowByType failed, typeName:{0}, load prefab failed: {1}", typeName, Debug.LogErrorFormat("CreateWindowByType failed, typeName:{0}, load prefab failed: {1}", typeName,
resPath); resPath);
//UISys.Mgr.ShowTipMsg(TextDefine.DOWNLOAD_TIP_UI);
//GameEvent.Get<IHomePageUI>().ShowDownloadUI();
return false; return false;
} }
@@ -247,7 +198,7 @@ namespace TEngine.Runtime.UIModule
RectTransform rectTrans = uiGo.transform as RectTransform; RectTransform rectTrans = uiGo.transform as RectTransform;
if (window.NeedCenterUI()) if (window.NeedCenterUI())
{ {
rectTrans.SetMax(); //localPosition = new Vector3(0, 0, 0); rectTrans.SetMax();
} }
rectTrans.localRotation = Quaternion.identity; rectTrans.localRotation = Quaternion.identity;
@@ -289,7 +240,7 @@ namespace TEngine.Runtime.UIModule
private void ShowWindow(UIWindow window, int showIndex) private void ShowWindow(UIWindow window, int showIndex)
{ {
UIWindowStack windowStack = GetUIWindowStack(window); UIWindowStack windowStack = GetUIWindowStack(window);
List<uint> windowList = windowStack.m_windowList; List<uint> windowList = windowStack.WindowsList;
int resortIndex = -1; int resortIndex = -1;
int findIndex = windowList.IndexOf(window.WindowId); int findIndex = windowList.IndexOf(window.WindowId);
if (findIndex >= 0) if (findIndex >= 0)
@@ -306,23 +257,23 @@ namespace TEngine.Runtime.UIModule
private void ResortStackUI(UIWindowStack stack, int startIdx) 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; startIdx = startIdx < 0 ? (stack.WindowsList.Count - 1) : startIdx;
for (int i = startIdx; i < stack.m_windowList.Count; i++) for (int i = startIdx; i < stack.WindowsList.Count; i++)
{ {
uint windowId = stack.m_windowList[i]; uint windowId = stack.WindowsList[i];
UIWindow window = FindWindow(windowId); UIWindow window = FindWindow(windowId);
if (window != null) if (window != null)
{ {
int order; int order;
if (window.IsFixedSortingOrder) if (window.IsFixedSortingOrder)
{ {
order = stack.m_baseOrder + window.FixedAdditionalOrder; order = stack.BaseOrder + window.FixedAdditionalOrder;
} }
else else
{ {
order = stack.m_baseOrder + i * UIWindow.MaxCanvasSortingOrder; order = stack.BaseOrder + i * UIWindow.MaxCanvasSortingOrder;
} }
window.SortingOrder = order; window.SortingOrder = order;
@@ -333,12 +284,12 @@ namespace TEngine.Runtime.UIModule
private void ShowTopUI(UIWindowStack stack) private void ShowTopUI(UIWindowStack stack)
{ {
if (stack.m_windowList.Count > 0) if (stack.WindowsList.Count > 0)
{ {
bool hasTop = false; 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); UIWindow window = FindWindow(windowId);
if (window != null) if (window != null)
{ {
@@ -370,7 +321,7 @@ namespace TEngine.Runtime.UIModule
continue; continue;
} }
var listWindow = stack.m_windowList; var listWindow = stack.WindowsList;
for (int k = 0; k < listWindow.Count; k++) for (int k = 0; k < listWindow.Count; k++)
{ {
var winId = listWindow[k]; var winId = listWindow[k];
@@ -387,7 +338,6 @@ namespace TEngine.Runtime.UIModule
} }
} }
} }
//SceneSys.Instance.CameraMgr.SetSceneCameraEnableByUI(true);
} }
public UIWindow FindWindow(uint windowId) public UIWindow FindWindow(uint windowId)
@@ -422,7 +372,6 @@ namespace TEngine.Runtime.UIModule
int findIndex = windowStack.FindIndex(window.WindowId); int findIndex = windowStack.FindIndex(window.WindowId);
//window.Destroy();
DestroyWindowObject(window); DestroyWindowObject(window);
ResortStackUI(windowStack, findIndex); ResortStackUI(windowStack, findIndex);
@@ -441,7 +390,7 @@ namespace TEngine.Runtime.UIModule
uint windowId = window.WindowId; uint windowId = window.WindowId;
m_allWindow.Remove(windowId); m_allWindow.Remove(windowId);
UIWindowStack windowStack = GetUIWindowStack(window); UIWindowStack windowStack = GetUIWindowStack(window);
windowStack.m_windowList.Remove(windowId); windowStack.WindowsList.Remove(windowId);
window.Destroy(); window.Destroy();
m_tmpWindowListDirty = true; m_tmpWindowListDirty = true;
} }

View File

@@ -1,21 +1,62 @@
using System.Collections.Generic; using System;
using System.Collections.Generic;
using System.Reflection;
namespace TEngine.Runtime.UIModule namespace TEngine.Runtime.UIModule
{ {
public partial class UISys public partial class UISys
{ {
private List<IUIController> m_listController = new List<IUIController>(); /// <summary>
/// UIController
/// </summary>
private readonly List<UIControllerBase> _listController = new List<UIControllerBase>();
public void RegisterAllController() /// <summary>
/// 自动注册UIController
/// </summary>
/// <remarks>nameSpace TEngine.Runtime.UIModule</remarks>
private void RegisterAllController()
{ {
//AddController<LoadingUIController>(); 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<T>() 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<T>() where T : UIControllerBase, new()
{
for (int i = 0; i < _listController.Count; i++)
{
var type = _listController[i].GetType();
if (type == typeof(T)) if (type == typeof(T))
{ {
@@ -26,7 +67,7 @@ namespace TEngine.Runtime.UIModule
var controller = new T(); var controller = new T();
m_listController.Add(controller); _listController.Add(controller);
controller.RegisterUIEvent(); controller.RegisterUIEvent();
} }

View File

@@ -2,14 +2,11 @@
{ {
public partial class UISys : BehaviourSingleton<UISys> public partial class UISys : BehaviourSingleton<UISys>
{ {
public static int DesginWidth => 750; public static int DesignWidth => 750;
public static int DesginHeight => 1624; public static int DesignHeight => 1624;
public static UIManager Mgr public static UIManager Mgr => UIManager.Instance;
{
get { return UIManager.Instance; }
}
public override void Active() public override void Active()
{ {
@@ -20,7 +17,6 @@
public override void Update() public override void Update()
{ {
base.Update();
UIManager.Instance.OnUpdate(); UIManager.Instance.OnUpdate();
} }
} }

View File

@@ -12,21 +12,20 @@ namespace TEngine.Runtime.UIModule
public class UIWindowStack public class UIWindowStack
{ {
public WindowStackIndex m_stackIndex; public WindowStackIndex StackIndex;
public int m_baseOrder = 0; public int BaseOrder = 0;
public List<uint> m_windowList = new List<uint>(); public readonly List<uint> WindowsList = new List<uint>();
public Transform m_parentTrans; public Transform ParentTrans;
public int FindIndex(uint windowId) 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 i;
} }
} }
return -1; return -1;
} }
} }