Update
This commit is contained in:
ALEXTANG
2023-04-04 11:32:21 +08:00
parent 6fbe17d7d5
commit 41a0243d91
3 changed files with 441 additions and 181 deletions

View File

@@ -1,18 +1,36 @@
using YooAsset; using System;
using System.Collections.Generic;
using UnityEngine;
using YooAsset;
namespace TEngine namespace TEngine
{ {
/// <summary>
/// UI类型。
/// </summary>
public enum UIBaseType public enum UIBaseType
{ {
/// <summary>
/// 类型无。
/// </summary>
None, None,
/// <summary>
/// 类型Windows。
/// </summary>
Window, Window,
/// <summary>
/// 类型Widget。
/// </summary>
Widget, Widget,
} }
public class UIBase /// <summary>
/// UI基类。
/// </summary>
public class UIBase:IUIBehaviour
{ {
/// <summary> /// <summary>
/// 所属的window /// 所属UI父节点
/// </summary> /// </summary>
protected UIBase parent = null; protected UIBase parent = null;
@@ -21,6 +39,16 @@ namespace TEngine
/// </summary> /// </summary>
public UIBase Parent => parent; public UIBase Parent => parent;
/// <summary>
/// 窗口的实例资源对象。
/// </summary>
public virtual GameObject gameObject { protected set; get; }
/// <summary>
/// 窗口矩阵位置组件。
/// </summary>
public virtual RectTransform rectTransform { protected set; get; }
/// <summary> /// <summary>
/// UI类型。 /// UI类型。
/// </summary> /// </summary>
@@ -30,5 +58,196 @@ namespace TEngine
/// 资源操作句柄。 /// 资源操作句柄。
/// </summary> /// </summary>
public AssetOperationHandle Handle { protected set; get; } public AssetOperationHandle Handle { protected set; get; }
/// <summary>
/// 资源是否准备完毕。
/// </summary>
public bool IsPrepare { protected set; get; }
/// <summary>
/// UI子组件列表。
/// </summary>
public List<UIWidget> ListChild = new List<UIWidget>();
/// <summary>
/// 存在Update更新的UI子组件列表。
/// </summary>
protected List<UIWidget> m_listUpdateChild = null;
/// <summary>
/// 是否持有Update行为。
/// </summary>
protected bool m_updateListValid = false;
/// <summary>
/// 代码自动生成绑定。
/// </summary>
public virtual void ScriptGenerator()
{
}
/// <summary>
/// 绑定UI成员元素。
/// </summary>
public virtual void BindMemberProperty()
{
}
/// <summary>
/// 注册事件。
/// </summary>
public virtual void RegisterEvent()
{
}
/// <summary>
/// 窗口创建。
/// </summary>
public virtual void OnCreate()
{
}
/// <summary>
/// 窗口刷新
/// </summary>
public virtual void OnRefresh()
{
}
/// <summary>
/// 是否需要Update
/// </summary>
protected bool HasOverrideUpdate = true;
/// <summary>
/// 窗口更新
/// </summary>
public virtual void OnUpdate()
{
HasOverrideUpdate = false;
}
/// <summary>
/// 窗口销毁
/// </summary>
public virtual void OnDestroy()
{
}
/// <summary>
/// 当触发窗口的层级排序
/// </summary>
protected virtual void OnSortDepth(int depth)
{
}
/// <summary>
/// 当因为全屏遮挡触发窗口的显隐
/// </summary>
protected virtual void OnSetVisible(bool visible)
{
}
#region FindChildComponent
public Transform FindChild(string path)
{
return DUnityUtil.FindChild(rectTransform, path);
}
public Transform FindChild(Transform trans, string path)
{
return DUnityUtil.FindChild(trans, path);
}
public T FindChildComponent<T>(string path) where T : Component
{
return DUnityUtil.FindChildComponent<T>(rectTransform, path);
}
public T FindChildComponent<T>(Transform trans, string path) where T : Component
{
return DUnityUtil.FindChildComponent<T>(trans, path);
}
#endregion
#region UIEvent
private GameEventMgr _eventMgr;
protected GameEventMgr EventMgr
{
get
{
if (_eventMgr == null)
{
_eventMgr = MemoryPool.Acquire<GameEventMgr>();
}
return _eventMgr;
}
}
public void AddUIEvent(int eventType, Action handler)
{
EventMgr.AddUIEvent(eventType, handler);
}
protected void AddUIEvent<T>(int eventType, Action<T> handler)
{
EventMgr.AddUIEvent(eventType, handler);
}
protected void AddUIEvent<T, U>(int eventType, Action<T, U> handler)
{
EventMgr.AddUIEvent(eventType, handler);
}
protected void AddUIEvent<T, U, V>(int eventType, Action<T, U, V> handler)
{
EventMgr.AddUIEvent(eventType, handler);
}
protected void AddUIEvent<T, U, V, W>(int eventType, Action<T, U, V, W> handler)
{
EventMgr.AddUIEvent(eventType, handler);
}
protected void RemoveAllUIEvent()
{
if (_eventMgr != null)
{
MemoryPool.Release(_eventMgr);
}
}
#endregion
#region UIWidget
public T CreateWidget<T>(string goPath, bool visible = true) where T : UIWidget,new()
{
var goRootTrans = FindChild(goPath);
if (goRootTrans != null)
{
return CreateWidget<T>(goRootTrans.gameObject, visible);
}
return null;
}
public T CreateWidget<T>(GameObject goRoot, bool visible = true) where T : UIWidget,new()
{
var widget = new T();
if (widget.Create(this, goRoot, visible))
{
return widget;
}
return null;
}
#endregion
} }
} }

View File

@@ -1,7 +1,30 @@
namespace TEngine using System.Collections.Generic;
using UnityEngine;
namespace TEngine
{ {
public abstract class UIWidget:UIBase,IUIBehaviour public abstract class UIWidget:UIBase,IUIBehaviour
{ {
/// <summary>
/// 窗口组件的实例资源对象。
/// </summary>
public override GameObject gameObject { protected set; get; }
/// <summary>
/// 窗口组件矩阵位置组件。
/// </summary>
public override RectTransform rectTransform { protected set; get; }
/// <summary>
/// 窗口组件名称。
/// </summary>
public string name { private set; get; } = nameof(UIWidget);
/// <summary>
/// UI类型。
/// </summary>
public override UIBaseType BaseType => UIBaseType.Widget;
/// <summary> /// <summary>
/// 所属的窗口。 /// 所属的窗口。
/// </summary> /// </summary>
@@ -24,39 +47,137 @@
} }
} }
public virtual void ScriptGenerator() internal bool InternalUpdate()
{ {
throw new System.NotImplementedException(); if (!IsPrepare)
{
return false;
}
List<UIWidget> listNextUpdateChild = null;
if (ListChild != null && ListChild.Count > 0)
{
listNextUpdateChild = m_listUpdateChild;
var updateListValid = m_updateListValid;
List<UIWidget> listChild = null;
if (!updateListValid)
{
if (listNextUpdateChild == null)
{
listNextUpdateChild = new List<UIWidget>();
m_listUpdateChild = listNextUpdateChild;
}
else
{
listNextUpdateChild.Clear();
}
listChild = ListChild;
}
else
{
listChild = listNextUpdateChild;
}
for (int i = 0; i < listChild.Count; i++)
{
var uiWidget = listChild[i];
UnityEngine.Profiling.Profiler.BeginSample(uiWidget.name);
var needValid = uiWidget.InternalUpdate();
UnityEngine.Profiling.Profiler.EndSample();
if (!updateListValid && needValid)
{
listNextUpdateChild.Add(uiWidget);
}
}
if (!updateListValid)
{
m_updateListValid = true;
}
}
UnityEngine.Profiling.Profiler.BeginSample("OnUpdate");
bool needUpdate = false;
if (listNextUpdateChild == null || listNextUpdateChild.Count <= 0)
{
HasOverrideUpdate = true;
OnUpdate();
needUpdate = HasOverrideUpdate;
}
else
{
OnUpdate();
needUpdate = true;
}
UnityEngine.Profiling.Profiler.EndSample();
return needUpdate;
} }
public virtual void BindMemberProperty() #region Create
/// <summary>
/// 创建窗口内嵌的界面。
/// </summary>
/// <param name="parentUI">父节点UI。</param>
/// <param name="widgetRoot">组件根节点。</param>
/// <param name="visible">是否可见。</param>
/// <returns></returns>
public bool Create(UIBase parentUI, GameObject widgetRoot, bool visible = true)
{ {
throw new System.NotImplementedException(); return CreateImp(parentUI, widgetRoot, false, visible);
} }
public virtual void RegisterEvent() private bool CreateImp(UIBase parentUI, GameObject widgetRoot, bool bindGo, bool visible = true)
{ {
throw new System.NotImplementedException(); if (!CreateBase(widgetRoot, bindGo))
{
return false;
}
RestChildCanvas(parentUI);
parent = parentUI;
Parent.ListChild.Add(this);
ScriptGenerator();
BindMemberProperty();
RegisterEvent();
OnCreate();
if (!visible)
{
gameObject.SetActive(false);
}
return true;
} }
public virtual void OnCreate() protected bool CreateBase(GameObject go, bool bindGo)
{ {
throw new System.NotImplementedException(); if (go == null)
{
return false;
}
rectTransform = go.GetComponent<RectTransform>();
Log.Assert(rectTransform != null, $"{go.name} ui base element need to be RectTransform");
return true;
} }
public virtual void OnRefresh() protected void RestChildCanvas(UIBase parentUI)
{ {
throw new System.NotImplementedException(); Canvas parentCanvas = parentUI.gameObject.GetComponentInParent<Canvas>();
} if (parentCanvas == null)
{
public virtual void OnUpdate() return;
{ }
throw new System.NotImplementedException(); var listCanvas = gameObject.GetComponentsInChildren<Canvas>(true);
} for (var index = 0; index < listCanvas.Length; index++)
{
public virtual void OnDestroy() var childCanvas = listCanvas[index];
{ childCanvas.sortingOrder = parentCanvas.sortingOrder + childCanvas.sortingOrder % UIComponent.WINDOW_DEEP;
throw new System.NotImplementedException(); }
} }
#endregion
} }
} }

View File

@@ -1,4 +1,5 @@
using System; using System;
using System.Collections.Generic;
using UnityEngine; using UnityEngine;
using UnityEngine.UI; using UnityEngine.UI;
using YooAsset; using YooAsset;
@@ -6,7 +7,7 @@ using Object = UnityEngine.Object;
namespace TEngine namespace TEngine
{ {
public abstract class UIWindow : UIBase, IUIBehaviour public abstract class UIWindow : UIBase
{ {
private System.Action<UIWindow> _prepareCallback; private System.Action<UIWindow> _prepareCallback;
@@ -29,12 +30,12 @@ namespace TEngine
/// <summary> /// <summary>
/// 窗口矩阵位置组件。 /// 窗口矩阵位置组件。
/// </summary> /// </summary>
public RectTransform transform => _panel.transform as RectTransform; public override RectTransform rectTransform => _panel.transform as RectTransform;
/// <summary> /// <summary>
/// 窗口的实例资源对象。 /// 窗口的实例资源对象。
/// </summary> /// </summary>
public GameObject gameObject => _panel; public override GameObject gameObject => _panel;
/// <summary> /// <summary>
/// 窗口名称。 /// 窗口名称。
@@ -204,16 +205,10 @@ namespace TEngine
} }
/// <summary> /// <summary>
/// 是否加载完毕 /// 是否加载完毕
/// </summary> /// </summary>
internal bool IsLoadDone => Handle.IsDone; internal bool IsLoadDone => Handle.IsDone;
/// <summary>
/// 是否准备完毕
/// </summary>
public bool IsPrepare { private set; get; }
public void Init(string name, int layer, bool fullScreen, string assetName) public void Init(string name, int layer, bool fullScreen, string assetName)
{ {
WindowName = name; WindowName = name;
@@ -221,75 +216,7 @@ namespace TEngine
FullScreen = fullScreen; FullScreen = fullScreen;
AssetName = assetName; AssetName = assetName;
} }
/// <summary>
/// 代码自动生成绑定。
/// </summary>
public virtual void ScriptGenerator()
{
}
/// <summary>
/// 绑定UI成员元素。
/// </summary>
public virtual void BindMemberProperty()
{
}
/// <summary>
/// 注册事件。
/// </summary>
public virtual void RegisterEvent()
{
}
/// <summary>
/// 窗口创建。
/// </summary>
public virtual void OnCreate()
{
}
/// <summary>
/// 窗口刷新
/// </summary>
public virtual void OnRefresh()
{
}
/// <summary>
/// 窗口更新
/// </summary>
public virtual void OnUpdate()
{
}
/// <summary>
/// 窗口销毁
/// </summary>
public virtual void OnDestroy()
{
}
protected virtual void Close()
{
GameModule.UI.CloseWindow(this.GetType());
}
/// <summary>
/// 当触发窗口的层级排序
/// </summary>
protected virtual void OnSortDepth(int depth)
{
}
/// <summary>
/// 当因为全屏遮挡触发窗口的显隐
/// </summary>
protected virtual void OnSetVisible(bool visible)
{
}
internal void TryInvoke(System.Action<UIWindow> prepareCallback, System.Object[] userDatas) internal void TryInvoke(System.Action<UIWindow> prepareCallback, System.Object[] userDatas)
{ {
_userDatas = userDatas; _userDatas = userDatas;
@@ -330,12 +257,75 @@ namespace TEngine
OnRefresh(); OnRefresh();
} }
internal void InternalUpdate() internal bool InternalUpdate()
{ {
if (IsPrepare) if (!IsPrepare ||!Visible)
{
return false;
}
List<UIWidget> listNextUpdateChild = null;
if (ListChild != null && ListChild.Count > 0)
{
listNextUpdateChild = m_listUpdateChild;
var updateListValid = m_updateListValid;
List<UIWidget> listChild = null;
if (!updateListValid)
{
if (listNextUpdateChild == null)
{
listNextUpdateChild = new List<UIWidget>();
m_listUpdateChild = listNextUpdateChild;
}
else
{
listNextUpdateChild.Clear();
}
listChild = ListChild;
}
else
{
listChild = listNextUpdateChild;
}
for (int i = 0; i < listChild.Count; i++)
{
var uiWidget = listChild[i];
UnityEngine.Profiling.Profiler.BeginSample(uiWidget.name);
var needValid = uiWidget.InternalUpdate();
UnityEngine.Profiling.Profiler.EndSample();
if (!updateListValid && needValid)
{
listNextUpdateChild.Add(uiWidget);
}
}
if (!updateListValid)
{
m_updateListValid = true;
}
}
UnityEngine.Profiling.Profiler.BeginSample("OnUpdate");
bool needUpdate = false;
if (listNextUpdateChild == null || listNextUpdateChild.Count <= 0)
{
HasOverrideUpdate = true;
OnUpdate();
needUpdate = HasOverrideUpdate;
}
else
{ {
OnUpdate(); OnUpdate();
needUpdate = true;
} }
UnityEngine.Profiling.Profiler.EndSample();
return needUpdate;
} }
internal void InternalDestroy() internal void InternalDestroy()
@@ -394,81 +384,11 @@ namespace TEngine
IsPrepare = true; IsPrepare = true;
_prepareCallback?.Invoke(this); _prepareCallback?.Invoke(this);
} }
#region FindChildComponent
protected virtual void Close()
public Transform FindChild(string path)
{ {
return DUnityUtil.FindChild(transform, path); GameModule.UI.CloseWindow(this.GetType());
} }
public Transform FindChild(Transform trans, string path)
{
return DUnityUtil.FindChild(trans, path);
}
public T FindChildComponent<T>(string path) where T : Component
{
return DUnityUtil.FindChildComponent<T>(transform, path);
}
public T FindChildComponent<T>(Transform trans, string path) where T : Component
{
return DUnityUtil.FindChildComponent<T>(trans, path);
}
#endregion
#region UIEvent
private GameEventMgr _eventMgr;
protected GameEventMgr EventMgr
{
get
{
if (_eventMgr == null)
{
_eventMgr = MemoryPool.Acquire<GameEventMgr>();
}
return _eventMgr;
}
}
public void AddUIEvent(int eventType, Action handler)
{
EventMgr.AddUIEvent(eventType, handler);
}
protected void AddUIEvent<T>(int eventType, Action<T> handler)
{
EventMgr.AddUIEvent(eventType, handler);
}
protected void AddUIEvent<T, U>(int eventType, Action<T, U> handler)
{
EventMgr.AddUIEvent(eventType, handler);
}
protected void AddUIEvent<T, U, V>(int eventType, Action<T, U, V> handler)
{
EventMgr.AddUIEvent(eventType, handler);
}
protected void AddUIEvent<T, U, V, W>(int eventType, Action<T, U, V, W> handler)
{
EventMgr.AddUIEvent(eventType, handler);
}
private void RemoveAllUIEvent()
{
if (_eventMgr != null)
{
MemoryPool.Release(_eventMgr);
}
}
#endregion
} }
} }