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
{
/// <summary>
/// UI类型。
/// </summary>
public enum UIBaseType
{
/// <summary>
/// 类型无。
/// </summary>
None,
/// <summary>
/// 类型Windows。
/// </summary>
Window,
/// <summary>
/// 类型Widget。
/// </summary>
Widget,
}
public class UIBase
/// <summary>
/// UI基类。
/// </summary>
public class UIBase:IUIBehaviour
{
/// <summary>
/// 所属的window
/// 所属UI父节点
/// </summary>
protected UIBase parent = null;
@@ -21,6 +39,16 @@ namespace TEngine
/// </summary>
public UIBase Parent => parent;
/// <summary>
/// 窗口的实例资源对象。
/// </summary>
public virtual GameObject gameObject { protected set; get; }
/// <summary>
/// 窗口矩阵位置组件。
/// </summary>
public virtual RectTransform rectTransform { protected set; get; }
/// <summary>
/// UI类型。
/// </summary>
@@ -30,5 +58,196 @@ namespace TEngine
/// 资源操作句柄。
/// </summary>
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
{
/// <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>
@@ -24,39 +47,137 @@
}
}
public virtual void ScriptGenerator()
internal bool InternalUpdate()
{
throw new System.NotImplementedException();
if (!IsPrepare)
{
return false;
}
public virtual void BindMemberProperty()
List<UIWidget> listNextUpdateChild = null;
if (ListChild != null && ListChild.Count > 0)
{
throw new System.NotImplementedException();
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();
}
public virtual void RegisterEvent()
listChild = ListChild;
}
else
{
throw new System.NotImplementedException();
listChild = listNextUpdateChild;
}
public virtual void OnCreate()
for (int i = 0; i < listChild.Count; i++)
{
throw new System.NotImplementedException();
var uiWidget = listChild[i];
UnityEngine.Profiling.Profiler.BeginSample(uiWidget.name);
var needValid = uiWidget.InternalUpdate();
UnityEngine.Profiling.Profiler.EndSample();
if (!updateListValid && needValid)
{
listNextUpdateChild.Add(uiWidget);
}
}
public virtual void OnRefresh()
if (!updateListValid)
{
throw new System.NotImplementedException();
m_updateListValid = true;
}
}
public virtual void OnUpdate()
UnityEngine.Profiling.Profiler.BeginSample("OnUpdate");
bool needUpdate = false;
if (listNextUpdateChild == null || listNextUpdateChild.Count <= 0)
{
throw new System.NotImplementedException();
HasOverrideUpdate = true;
OnUpdate();
needUpdate = HasOverrideUpdate;
}
else
{
OnUpdate();
needUpdate = true;
}
UnityEngine.Profiling.Profiler.EndSample();
return needUpdate;
}
public virtual void OnDestroy()
#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);
}
private bool CreateImp(UIBase parentUI, GameObject widgetRoot, bool bindGo, bool visible = true)
{
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;
}
protected bool CreateBase(GameObject go, bool bindGo)
{
if (go == null)
{
return false;
}
rectTransform = go.GetComponent<RectTransform>();
Log.Assert(rectTransform != null, $"{go.name} ui base element need to be RectTransform");
return true;
}
protected void RestChildCanvas(UIBase parentUI)
{
Canvas parentCanvas = parentUI.gameObject.GetComponentInParent<Canvas>();
if (parentCanvas == null)
{
return;
}
var listCanvas = gameObject.GetComponentsInChildren<Canvas>(true);
for (var index = 0; index < listCanvas.Length; index++)
{
var childCanvas = listCanvas[index];
childCanvas.sortingOrder = parentCanvas.sortingOrder + childCanvas.sortingOrder % UIComponent.WINDOW_DEEP;
}
}
#endregion
}
}

View File

@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using YooAsset;
@@ -6,7 +7,7 @@ using Object = UnityEngine.Object;
namespace TEngine
{
public abstract class UIWindow : UIBase, IUIBehaviour
public abstract class UIWindow : UIBase
{
private System.Action<UIWindow> _prepareCallback;
@@ -29,12 +30,12 @@ namespace TEngine
/// <summary>
/// 窗口矩阵位置组件。
/// </summary>
public RectTransform transform => _panel.transform as RectTransform;
public override RectTransform rectTransform => _panel.transform as RectTransform;
/// <summary>
/// 窗口的实例资源对象。
/// </summary>
public GameObject gameObject => _panel;
public override GameObject gameObject => _panel;
/// <summary>
/// 窗口名称。
@@ -204,16 +205,10 @@ namespace TEngine
}
/// <summary>
/// 是否加载完毕
/// 是否加载完毕
/// </summary>
internal bool IsLoadDone => Handle.IsDone;
/// <summary>
/// 是否准备完毕
/// </summary>
public bool IsPrepare { private set; get; }
public void Init(string name, int layer, bool fullScreen, string assetName)
{
WindowName = name;
@@ -222,74 +217,6 @@ namespace TEngine
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)
{
_userDatas = userDatas;
@@ -330,12 +257,75 @@ namespace TEngine
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();
needUpdate = true;
}
UnityEngine.Profiling.Profiler.EndSample();
return needUpdate;
}
internal void InternalDestroy()
@@ -395,80 +385,10 @@ namespace TEngine
_prepareCallback?.Invoke(this);
}
#region FindChildComponent
public Transform FindChild(string path)
protected virtual void Close()
{
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
}
}