diff --git a/Assets/TEngine/Runtime/GameFramework/UI/UIBase.cs b/Assets/TEngine/Runtime/GameFramework/UI/UIBase.cs index ef55c530..36b2fc74 100644 --- a/Assets/TEngine/Runtime/GameFramework/UI/UIBase.cs +++ b/Assets/TEngine/Runtime/GameFramework/UI/UIBase.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Threading; using Cysharp.Threading.Tasks; using UnityEngine; using YooAsset; @@ -68,9 +69,9 @@ namespace TEngine public AssetOperationHandle Handle { protected set; get; } /// - /// 资源组数据。 + /// 资源引用数据。 /// - public AssetGroup AssetGroup { protected set; get; } + public AssetReference AssetReference { protected set; get; } /// /// 资源是否准备完毕。 @@ -239,7 +240,13 @@ namespace TEngine #endregion #region UIWidget - + /// + /// 创建UIWidget通过父UI位置节点。 + /// + /// 父UI位置节点。 + /// 是否可见。 + /// UIWidget。 + /// UIWidget实例。 public T CreateWidget(string goPath, bool visible = true) where T : UIWidget, new() { var goRootTrans = FindChild(goPath); @@ -248,21 +255,18 @@ namespace TEngine { 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; - } - + + + /// + /// 创建UIWidget通过父UI位置节点。 + /// + /// + /// 父UI位置节点。 + /// 是否可见。 + /// UIWidget。 + /// UIWidget实例。 public T CreateWidget(Transform parentTrans, string goPath, bool visible = true) where T : UIWidget, new() { var goRootTrans = FindChild(parentTrans, goPath); @@ -270,21 +274,54 @@ namespace TEngine { return CreateWidget(goRootTrans.gameObject, visible); } - return null; } + /// + /// 创建UIWidget通过游戏物体。 + /// + /// 游戏物体。 + /// 是否可见。 + /// UIWidget。 + /// UIWidget实例。 + 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; + } + + /// + /// 创建UIWidget通过资源路径。 + /// + /// 资源父节点。 + /// 资源路径。 + /// 是否可见。 + /// UIWidget。 + /// UIWidget实例。 public T CreateWidgetByPath(Transform parentTrans, string assetPath, bool visible = true) where T : UIWidget, new() { - if (AssetGroup == null) + if (AssetReference == null) { - AssetGroup = AssetGroup.Alloc(); + Log.Fatal($"CreateWidgetByPath Failed => {this}.AssetReference is null"); + return null; } - - GameObject goInst = AssetGroup.LoadAsset(assetPath, parentTrans); + GameObject goInst = AssetReference.LoadAsset(assetPath, parentTrans,out var handle); + Handle = handle; return CreateWidget(goInst, visible); } + /// + /// 根据prefab或者模版来创建新的 widget。 + /// + /// 资源创建副本。 + /// 资源父节点。 + /// 是否可见。 + /// UIWidget。 + /// UIWidget实例。 public T CreateWidgetByPrefab(GameObject goPrefab, Transform parentTrans = null, bool visible = true) where T : UIWidget, new() { var widget = new T(); @@ -296,6 +333,13 @@ namespace TEngine return widget; } + /// + /// 通过UI类型来创建widget。 + /// + /// 资源父节点。 + /// 是否可见。 + /// UIWidget。 + /// UIWidget实例。 public T CreateWidgetByType(Transform parentTrans, bool visible = true) where T : UIWidget, new() { return CreateWidgetByPath(parentTrans, typeof(T).Name, visible); @@ -304,25 +348,32 @@ namespace TEngine /// /// 调整图标数量。 /// - public void AdjustIconNum(List listIcon, int tarNum, Transform parent, GameObject prefab = null, string assetPath = "") where T : UIWidget, new() + /// 常用于Icon创建。 + /// 存放Icon的列表。 + /// 创建数目。 + /// 资源父节点。 + /// 资产副本。 + /// 资产地址。 + /// 图标类型。 + public void AdjustIconNum(List listIcon, int number, Transform parentTrans, GameObject prefab = null, string assetPath = "") where T : UIWidget, new() { if (listIcon == null) { listIcon = new List(); } - if (listIcon.Count < tarNum) + if (listIcon.Count < number) { - int needNum = tarNum - listIcon.Count; + int needNum = number - listIcon.Count; for (int iconIdx = 0; iconIdx < needNum; iconIdx++) { - T tmpT = prefab == null ? CreateWidgetByType(parent) : CreateWidgetByPrefab(prefab, parent); + T tmpT = prefab == null ? CreateWidgetByType(parentTrans) : CreateWidgetByPrefab(prefab, parentTrans); listIcon.Add(tmpT); } } - else if (listIcon.Count > tarNum) + else if (listIcon.Count > number) { - RemoveUnUseItem(listIcon, tarNum); + RemoveUnUseItem(listIcon, number); } } @@ -347,6 +398,14 @@ namespace TEngine /// /// 异步创建接口。 /// + /// + /// + /// + /// + /// + /// + /// + /// private async UniTaskVoid AsyncAdjustIconNumInternal(List listIcon, int tarNum, Transform parentTrans, int maxNumPerFrame, Action updateAction, GameObject prefab, string assetPath) where T : UIWidget, new() { @@ -415,11 +474,128 @@ namespace TEngine var icon = removeIcon[index]; listIcon.Remove(icon); icon.OnDestroy(); + icon.OnDestroyWidget(); ListChild.Remove(icon); UnityEngine.Object.Destroy(icon.gameObject); } } #endregion + + #region AssetRefrence Methods + + /// + /// 引用资源数据到资源组内。 + /// + /// 资源操作句柄。 + /// 资源标识。 + /// 是否注册成功。 + public bool Reference(AssetOperationHandle handle, string assetTag = "") + { + if (AssetReference == null) + { + Log.Fatal($"Register Failed => {this}.AssetReference is null"); + return false; + } + return AssetReference.Reference(handle, assetTag); + } + + /// + /// 从资源组内释放资源数据。 + /// + /// + /// + public bool Release(string assetTag) + { + if (AssetReference == null) + { + Log.Fatal($"UnRegister Failed => {this}.AssetReference is null"); + return false; + } + return AssetReference.Release(assetTag); + } + + /// + /// 从资源组内释放资源数据。 + /// + /// + /// + public bool Release(AssetOperationHandle handle) + { + if (AssetReference == null) + { + Log.Fatal($"UnRegister Failed => {this}.AssetReference is null"); + return false; + } + return AssetReference.Release(handle); + } + + + /// + /// 同步加载资源。 + /// + /// 要加载资源的名称。 + /// 要加载资源的类型。 + /// 资源实例。 + public T LoadAsset(string assetName) where T : UnityEngine.Object + { + if (AssetReference == null) + { + Log.Fatal($"LoadAsset Failed => {this}.AssetReference is null"); + return default; + } + return AssetReference.LoadAsset(assetName); + } + + /// + /// 同步加载资源。 + /// + /// 要加载资源的名称。 + /// 父节点位置。 + /// 要加载资源的类型。 + /// 资源实例。 + public T LoadAsset(string assetName, Transform parentTrans) where T : UnityEngine.Object + { + if (AssetReference == null) + { + Log.Fatal($"LoadAsset Failed => {this}.AssetReference is null"); + return default; + } + return AssetReference.LoadAsset(assetName, parentTrans); + } + + /// + /// 异步加载资源实例。 + /// + /// 要加载的实例名称。 + /// 取消操作Token。 + /// 资源实实例。 + public async UniTask LoadAssetAsync(string assetName, CancellationToken cancellationToken) + where T : UnityEngine.Object + { + if (AssetReference == null) + { + Log.Fatal($"LoadAssetAsync Failed => {this}.AssetReference is null"); + return default; + } + return await AssetReference.LoadAssetAsync(assetName, cancellationToken); + } + + /// + /// 异步加载游戏物体。 + /// + /// 要加载的游戏物体名称。 + /// 取消操作Token。 + /// 异步游戏物体实例。 + public async UniTask LoadGameObjectAsync(string assetName, CancellationToken cancellationToken) + { + if (AssetReference == null) + { + Log.Fatal($"LoadAssetAsync Failed => {this}.AssetReference is null"); + return default; + } + return await AssetReference.LoadGameObjectAsync(assetName, cancellationToken); + } + #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 99460db2..82429b4d 100644 --- a/Assets/TEngine/Runtime/GameFramework/UI/UIWidget.cs +++ b/Assets/TEngine/Runtime/GameFramework/UI/UIWidget.cs @@ -18,6 +18,7 @@ namespace TEngine /// /// 窗口组件名称。 /// + // ReSharper disable once InconsistentNaming public string name { private set; get; } = nameof(UIWidget); /// @@ -83,9 +84,9 @@ namespace TEngine { var uiWidget = listChild[i]; - UnityEngine.Profiling.Profiler.BeginSample(uiWidget.name); + TProfiler.BeginSample(uiWidget.name); var needValid = uiWidget.InternalUpdate(); - UnityEngine.Profiling.Profiler.EndSample(); + TProfiler.EndSample(); if (!updateListValid && needValid) { @@ -99,7 +100,7 @@ namespace TEngine } } - UnityEngine.Profiling.Profiler.BeginSample("OnUpdate"); + TProfiler.BeginSample("OnUpdate"); bool needUpdate = false; if (listNextUpdateChild == null || listNextUpdateChild.Count <= 0) @@ -113,7 +114,7 @@ namespace TEngine OnUpdate(); needUpdate = true; } - UnityEngine.Profiling.Profiler.EndSample(); + TProfiler.EndSample(); return needUpdate; } @@ -154,6 +155,7 @@ namespace TEngine { return false; } + AssetReference = AssetReference.BindAssetReference(widgetRoot,parent:parentUI.AssetReference); RestChildCanvas(parentUI); parent = parentUI; Parent.ListChild.Add(this); @@ -200,5 +202,25 @@ namespace TEngine } } #endregion + + #region Destroy + + /// + /// 组件被销毁调用。 + /// 请勿手动调用! + /// + public void OnDestroyWidget() + { + if (Handle != null) + { + if (AssetReference != null && AssetReference.Parent != null) + { + AssetReference.Parent.Release(Handle); + } + } + } + + + #endregion } } \ No newline at end of file diff --git a/Assets/TEngine/Runtime/GameFramework/UI/UIWindow.cs b/Assets/TEngine/Runtime/GameFramework/UI/UIWindow.cs index 87045a30..24166ad9 100644 --- a/Assets/TEngine/Runtime/GameFramework/UI/UIWindow.cs +++ b/Assets/TEngine/Runtime/GameFramework/UI/UIWindow.cs @@ -339,7 +339,9 @@ namespace TEngine for (int i = 0; i < ListChild.Count; i++) { - ListChild[i].OnDestroy(); + var uiChild = ListChild[i]; + uiChild.OnDestroy(); + uiChild.OnDestroyWidget(); } // 注销回调函数 @@ -352,12 +354,6 @@ namespace TEngine Handle = null; } - if (AssetGroup != null) - { - AssetGroup.Release(AssetGroup); - AssetGroup = null; - } - // 销毁面板对象 if (_panel != null) { @@ -382,6 +378,9 @@ namespace TEngine // 实例化对象 _panel = handle.InstantiateSync(UIModule.UIRootStatic); _panel.transform.localPosition = Vector3.zero; + + // 绑定引用 + AssetReference = AssetReference.BindAssetReference(_panel); // 获取组件 _canvas = _panel.GetComponent();