From 41a0243d91c96fbc5c0abd010680baee8f76c98c Mon Sep 17 00:00:00 2001
From: ALEXTANG <574809918@qq.com>
Date: Tue, 4 Apr 2023 11:32:21 +0800
Subject: [PATCH] Update
Update
---
.../Runtime/GameFramework/UI/UIBase.cs | 225 ++++++++++++++++-
.../Runtime/GameFramework/UI/UIWidget.cs | 167 +++++++++++--
.../Runtime/GameFramework/UI/UIWindow.cs | 230 ++++++------------
3 files changed, 441 insertions(+), 181 deletions(-)
diff --git a/Assets/TEngine/Runtime/GameFramework/UI/UIBase.cs b/Assets/TEngine/Runtime/GameFramework/UI/UIBase.cs
index 9b1d889d..7b5e72ce 100644
--- a/Assets/TEngine/Runtime/GameFramework/UI/UIBase.cs
+++ b/Assets/TEngine/Runtime/GameFramework/UI/UIBase.cs
@@ -1,18 +1,36 @@
-using YooAsset;
+using System;
+using System.Collections.Generic;
+using UnityEngine;
+using YooAsset;
namespace TEngine
{
+ ///
+ /// UI类型。
+ ///
public enum UIBaseType
{
+ ///
+ /// 类型无。
+ ///
None,
+ ///
+ /// 类型Windows。
+ ///
Window,
+ ///
+ /// 类型Widget。
+ ///
Widget,
}
- public class UIBase
+ ///
+ /// UI基类。
+ ///
+ public class UIBase:IUIBehaviour
{
///
- /// 所属的window。
+ /// 所属UI父节点。
///
protected UIBase parent = null;
@@ -21,6 +39,16 @@ namespace TEngine
///
public UIBase Parent => parent;
+ ///
+ /// 窗口的实例资源对象。
+ ///
+ public virtual GameObject gameObject { protected set; get; }
+
+ ///
+ /// 窗口矩阵位置组件。
+ ///
+ public virtual RectTransform rectTransform { protected set; get; }
+
///
/// UI类型。
///
@@ -30,5 +58,196 @@ namespace TEngine
/// 资源操作句柄。
///
public AssetOperationHandle Handle { protected set; get; }
+
+ ///
+ /// 资源是否准备完毕。
+ ///
+ public bool IsPrepare { protected set; get; }
+
+ ///
+ /// UI子组件列表。
+ ///
+ public List ListChild = new List();
+
+ ///
+ /// 存在Update更新的UI子组件列表。
+ ///
+ protected List m_listUpdateChild = null;
+
+ ///
+ /// 是否持有Update行为。
+ ///
+ protected bool m_updateListValid = false;
+
+ ///
+ /// 代码自动生成绑定。
+ ///
+ public virtual void ScriptGenerator()
+ {
+ }
+
+ ///
+ /// 绑定UI成员元素。
+ ///
+ public virtual void BindMemberProperty()
+ {
+ }
+
+ ///
+ /// 注册事件。
+ ///
+ public virtual void RegisterEvent()
+ {
+ }
+
+ ///
+ /// 窗口创建。
+ ///
+ public virtual void OnCreate()
+ {
+ }
+
+ ///
+ /// 窗口刷新
+ ///
+ public virtual void OnRefresh()
+ {
+ }
+
+ ///
+ /// 是否需要Update
+ ///
+ protected bool HasOverrideUpdate = true;
+ ///
+ /// 窗口更新
+ ///
+ public virtual void OnUpdate()
+ {
+ HasOverrideUpdate = false;
+ }
+
+ ///
+ /// 窗口销毁
+ ///
+ public virtual void OnDestroy()
+ {
+ }
+
+ ///
+ /// 当触发窗口的层级排序
+ ///
+ protected virtual void OnSortDepth(int depth)
+ {
+ }
+
+ ///
+ /// 当因为全屏遮挡触发窗口的显隐
+ ///
+ 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(string path) where T : Component
+ {
+ return DUnityUtil.FindChildComponent(rectTransform, path);
+ }
+
+ public T FindChildComponent(Transform trans, string path) where T : Component
+ {
+ return DUnityUtil.FindChildComponent(trans, path);
+ }
+
+ #endregion
+
+ #region UIEvent
+
+ private GameEventMgr _eventMgr;
+
+ protected GameEventMgr EventMgr
+ {
+ get
+ {
+ if (_eventMgr == null)
+ {
+ _eventMgr = MemoryPool.Acquire();
+ }
+
+ return _eventMgr;
+ }
+ }
+
+ public void AddUIEvent(int eventType, Action handler)
+ {
+ EventMgr.AddUIEvent(eventType, handler);
+ }
+
+ protected void AddUIEvent(int eventType, Action handler)
+ {
+ EventMgr.AddUIEvent(eventType, handler);
+ }
+
+ protected void AddUIEvent(int eventType, Action handler)
+ {
+ EventMgr.AddUIEvent(eventType, handler);
+ }
+
+ protected void AddUIEvent(int eventType, Action handler)
+ {
+ EventMgr.AddUIEvent(eventType, handler);
+ }
+
+ protected void AddUIEvent(int eventType, Action handler)
+ {
+ EventMgr.AddUIEvent(eventType, handler);
+ }
+
+ protected void RemoveAllUIEvent()
+ {
+ if (_eventMgr != null)
+ {
+ MemoryPool.Release(_eventMgr);
+ }
+ }
+
+ #endregion
+
+ #region UIWidget
+ public T CreateWidget(string goPath, bool visible = true) where T : UIWidget,new()
+ {
+ var goRootTrans = FindChild(goPath);
+
+ if (goRootTrans != null)
+ {
+ return CreateWidget(goRootTrans.gameObject, visible);
+ }
+
+ return null;
+ }
+
+ public T CreateWidget(GameObject goRoot, bool visible = true) where T : UIWidget,new()
+ {
+ var widget = new T();
+ if (widget.Create(this, goRoot, visible))
+ {
+ return widget;
+ }
+
+ return null;
+ }
+
+ #endregion
}
}
\ No newline at end of file
diff --git a/Assets/TEngine/Runtime/GameFramework/UI/UIWidget.cs b/Assets/TEngine/Runtime/GameFramework/UI/UIWidget.cs
index 968f01b3..9f45e3c6 100644
--- a/Assets/TEngine/Runtime/GameFramework/UI/UIWidget.cs
+++ b/Assets/TEngine/Runtime/GameFramework/UI/UIWidget.cs
@@ -1,7 +1,30 @@
-namespace TEngine
+using System.Collections.Generic;
+using UnityEngine;
+
+namespace TEngine
{
public abstract class UIWidget:UIBase,IUIBehaviour
{
+ ///
+ /// 窗口组件的实例资源对象。
+ ///
+ public override GameObject gameObject { protected set; get; }
+
+ ///
+ /// 窗口组件矩阵位置组件。
+ ///
+ public override RectTransform rectTransform { protected set; get; }
+
+ ///
+ /// 窗口组件名称。
+ ///
+ public string name { private set; get; } = nameof(UIWidget);
+
+ ///
+ /// UI类型。
+ ///
+ public override UIBaseType BaseType => UIBaseType.Widget;
+
///
/// 所属的窗口。
///
@@ -24,39 +47,137 @@
}
}
- public virtual void ScriptGenerator()
+ internal bool InternalUpdate()
{
- throw new System.NotImplementedException();
+ if (!IsPrepare)
+ {
+ return false;
+ }
+
+ List listNextUpdateChild = null;
+ if (ListChild != null && ListChild.Count > 0)
+ {
+ listNextUpdateChild = m_listUpdateChild;
+ var updateListValid = m_updateListValid;
+ List listChild = null;
+ if (!updateListValid)
+ {
+ if (listNextUpdateChild == null)
+ {
+ listNextUpdateChild = new List();
+ 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
+ ///
+ /// 创建窗口内嵌的界面。
+ ///
+ /// 父节点UI。
+ /// 组件根节点。
+ /// 是否可见。
+ ///
+ 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();
+ 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();
- }
-
- public virtual void OnUpdate()
- {
- throw new System.NotImplementedException();
- }
-
- public virtual void OnDestroy()
- {
- throw new System.NotImplementedException();
+ Canvas parentCanvas = parentUI.gameObject.GetComponentInParent