From 3e3314858e6ae5837b4d2e41c02bcaddd04b3a4d Mon Sep 17 00:00:00 2001
From: ALEXTANG <574809918@qq.com>
Date: Mon, 25 Mar 2024 14:27:42 +0800
Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=8A=A0=E8=BD=BD=E6=B8=B8?=
=?UTF-8?q?=E6=88=8F=E7=89=A9=E4=BD=93=E6=8E=A5=E5=8F=A3=EF=BC=8C=E5=B8=B8?=
=?UTF-8?q?=E7=94=A8Parent=E5=8F=82=E6=95=B0=E5=89=8D=E7=BD=AE=EF=BC=8C?=
=?UTF-8?q?=E7=BC=96=E8=BE=91=E5=99=A8=E6=A8=A1=E5=BC=8F=E4=B8=8B=E5=A2=9E?=
=?UTF-8?q?=E5=8A=A0=E8=B6=85=E6=97=B6=E4=BF=9D=E6=8A=A4=E6=8F=90=E7=A4=BA?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
优化加载游戏物体接口,常用Parent参数前置,编辑器模式下增加超时保护提示
---
.../ResourceModule/IResourceManager.cs | 9 +++---
.../Modules/ResourceModule/ResourceManager.cs | 31 +++++++++++++++----
.../Modules/ResourceModule/ResourceModule.cs | 15 +++++----
.../Runtime/Modules/UIModule/UIBase.cs | 2 +-
4 files changed, 37 insertions(+), 20 deletions(-)
diff --git a/UnityProject/Assets/TEngine/Runtime/Modules/ResourceModule/IResourceManager.cs b/UnityProject/Assets/TEngine/Runtime/Modules/ResourceModule/IResourceManager.cs
index 93a3b2ff..25e6f2a2 100644
--- a/UnityProject/Assets/TEngine/Runtime/Modules/ResourceModule/IResourceManager.cs
+++ b/UnityProject/Assets/TEngine/Runtime/Modules/ResourceModule/IResourceManager.cs
@@ -2,7 +2,6 @@
using System.Threading;
using Cysharp.Threading.Tasks;
using UnityEngine;
-using UnityEngine.SceneManagement;
using YooAsset;
namespace TEngine
@@ -217,11 +216,11 @@ namespace TEngine
/// 同步加载游戏物体并实例化。
///
/// 资源的定位地址。
- /// 指定资源包的名称。不传使用默认资源包
/// 资源实例父节点。
+ /// 指定资源包的名称。不传使用默认资源包
/// 资源实例。
/// 会实例化资源到场景,无需主动UnloadAsset,Destroy时自动UnloadAsset。
- GameObject LoadGameObject(string location, string packageName = "", Transform parent = null);
+ GameObject LoadGameObject(string location, Transform parent = null, string packageName = "");
///
/// 异步加载资源。
@@ -262,11 +261,11 @@ namespace TEngine
/// 异步加载游戏物体并实例化。
///
/// 资源定位地址。
+ /// 资源实例父节点。
/// 取消操作Token。
/// 指定资源包的名称。不传使用默认资源包
- /// 资源实例父节点。
/// 异步游戏物体实例。
/// 会实例化资源到场景,无需主动UnloadAsset,Destroy时自动UnloadAsset。
- UniTask LoadGameObjectAsync(string location, CancellationToken cancellationToken = default, string packageName = "", Transform parent = null);
+ UniTask LoadGameObjectAsync(string location, Transform parent = null, CancellationToken cancellationToken = default, string packageName = "");
}
}
\ No newline at end of file
diff --git a/UnityProject/Assets/TEngine/Runtime/Modules/ResourceModule/ResourceManager.cs b/UnityProject/Assets/TEngine/Runtime/Modules/ResourceModule/ResourceManager.cs
index f4baadbe..ca094062 100644
--- a/UnityProject/Assets/TEngine/Runtime/Modules/ResourceModule/ResourceManager.cs
+++ b/UnityProject/Assets/TEngine/Runtime/Modules/ResourceModule/ResourceManager.cs
@@ -14,7 +14,6 @@ namespace TEngine
internal sealed partial class ResourceManager : ModuleImp, IResourceManager
{
#region Propreties
-
///
/// 资源包名称。
///
@@ -532,7 +531,7 @@ namespace TEngine
return ret;
}
- public GameObject LoadGameObject(string location, string packageName = "", Transform parent = null)
+ public GameObject LoadGameObject(string location, Transform parent = null, string packageName = "")
{
if (string.IsNullOrEmpty(location))
{
@@ -663,8 +662,7 @@ namespace TEngine
return handle.AssetObject as T;
}
- public async UniTask LoadGameObjectAsync(string location, CancellationToken cancellationToken = default, string packageName = "",
- Transform parent = null)
+ public async UniTask LoadGameObjectAsync(string location, Transform parent = null, CancellationToken cancellationToken = default, string packageName = "")
{
if (string.IsNullOrEmpty(location))
{
@@ -896,12 +894,33 @@ namespace TEngine
}
}
}
-
+
+ private readonly TimeoutController _timeoutController = new TimeoutController();
+
private async UniTask TryWaitingLoading(string assetObjectKey)
{
if (_assetLoadingList.Contains(assetObjectKey))
{
- await UniTask.WaitUntil(() => !_assetLoadingList.Contains(assetObjectKey), cancellationToken:CancellationToken);
+ try
+ {
+ await UniTask.WaitUntil(
+ () => !_assetLoadingList.Contains(assetObjectKey),
+ cancellationToken:CancellationToken)
+#if UNITY_EDITOR
+ .AttachExternalCancellation(_timeoutController.Timeout(TimeSpan.FromSeconds(60)));
+ _timeoutController.Reset();
+#else
+ ;
+#endif
+
+ }
+ catch (OperationCanceledException ex)
+ {
+ if (_timeoutController.IsTimeout())
+ {
+ Log.Error($"LoadAssetAsync Waiting {assetObjectKey} timeout. reason:{ex.Message}");
+ }
+ }
}
}
#endregion
diff --git a/UnityProject/Assets/TEngine/Runtime/Modules/ResourceModule/ResourceModule.cs b/UnityProject/Assets/TEngine/Runtime/Modules/ResourceModule/ResourceModule.cs
index 48c24101..bd85d52f 100644
--- a/UnityProject/Assets/TEngine/Runtime/Modules/ResourceModule/ResourceModule.cs
+++ b/UnityProject/Assets/TEngine/Runtime/Modules/ResourceModule/ResourceModule.cs
@@ -509,10 +509,10 @@ namespace TEngine
/// 同步加载游戏物体并实例化。
///
/// 资源的定位地址。
- /// 指定资源包的名称。不传使用默认资源包。
/// 资源实例父节点。
+ /// 指定资源包的名称。不传使用默认资源包。
/// 资源实例。
- public GameObject LoadGameObject(string location, string packageName = "", Transform parent = null)
+ public GameObject LoadGameObject(string location, Transform parent = null, string packageName = "")
{
if (string.IsNullOrEmpty(location))
{
@@ -520,7 +520,7 @@ namespace TEngine
return null;
}
- return m_ResourceManager.LoadGameObject(location, packageName, parent);
+ return m_ResourceManager.LoadGameObject(location, parent, packageName);
}
///
@@ -565,13 +565,12 @@ namespace TEngine
/// 异步加载游戏物体并实例化。
///
/// 资源定位地址。
+ /// 资源实例父节点。
/// 取消操作Token。
/// 指定资源包的名称。不传使用默认资源包。
- /// 资源实例父节点。
/// 异步游戏物体实例。
- public async UniTask LoadGameObjectAsync(string location, CancellationToken cancellationToken = default,
- string packageName = "",
- Transform parent = null)
+ public async UniTask LoadGameObjectAsync(string location, Transform parent = null, CancellationToken cancellationToken = default,
+ string packageName = "")
{
if (string.IsNullOrEmpty(location))
{
@@ -579,7 +578,7 @@ namespace TEngine
return null;
}
- return await m_ResourceManager.LoadGameObjectAsync(location, cancellationToken, packageName, parent);
+ return await m_ResourceManager.LoadGameObjectAsync(location, parent, cancellationToken, packageName);
}
internal AssetHandle LoadAssetGetOperation(string location,
diff --git a/UnityProject/Assets/TEngine/Runtime/Modules/UIModule/UIBase.cs b/UnityProject/Assets/TEngine/Runtime/Modules/UIModule/UIBase.cs
index 1bebd941..3459a460 100644
--- a/UnityProject/Assets/TEngine/Runtime/Modules/UIModule/UIBase.cs
+++ b/UnityProject/Assets/TEngine/Runtime/Modules/UIModule/UIBase.cs
@@ -331,7 +331,7 @@ namespace TEngine
/// UIWidget实例。
public async UniTask CreateWidgetByPathAsync(Transform parentTrans, string assetLocation, bool visible = true) where T : UIWidget, new()
{
- GameObject goInst = await GameModule.Resource.LoadGameObjectAsync(assetLocation, gameObject.GetCancellationTokenOnDestroy(), parent: parentTrans);
+ GameObject goInst = await GameModule.Resource.LoadGameObjectAsync(assetLocation, parentTrans, gameObject.GetCancellationTokenOnDestroy());
return CreateWidget(goInst, visible);
}