优化加载游戏物体接口,常用Parent参数前置,编辑器模式下增加超时保护提示

优化加载游戏物体接口,常用Parent参数前置,编辑器模式下增加超时保护提示
This commit is contained in:
ALEXTANG
2024-03-25 14:27:42 +08:00
parent da57ed845f
commit 3e3314858e
4 changed files with 37 additions and 20 deletions

View File

@@ -2,7 +2,6 @@
using System.Threading; using System.Threading;
using Cysharp.Threading.Tasks; using Cysharp.Threading.Tasks;
using UnityEngine; using UnityEngine;
using UnityEngine.SceneManagement;
using YooAsset; using YooAsset;
namespace TEngine namespace TEngine
@@ -217,11 +216,11 @@ namespace TEngine
/// 同步加载游戏物体并实例化。 /// 同步加载游戏物体并实例化。
/// </summary> /// </summary>
/// <param name="location">资源的定位地址。</param> /// <param name="location">资源的定位地址。</param>
/// <param name="packageName">指定资源包的名称。不传使用默认资源包</param>
/// <param name="parent">资源实例父节点。</param> /// <param name="parent">资源实例父节点。</param>
/// <param name="packageName">指定资源包的名称。不传使用默认资源包</param>
/// <returns>资源实例。</returns> /// <returns>资源实例。</returns>
/// <remarks>会实例化资源到场景无需主动UnloadAssetDestroy时自动UnloadAsset。</remarks> /// <remarks>会实例化资源到场景无需主动UnloadAssetDestroy时自动UnloadAsset。</remarks>
GameObject LoadGameObject(string location, string packageName = "", Transform parent = null); GameObject LoadGameObject(string location, Transform parent = null, string packageName = "");
/// <summary> /// <summary>
/// 异步加载资源。 /// 异步加载资源。
@@ -262,11 +261,11 @@ namespace TEngine
/// 异步加载游戏物体并实例化。 /// 异步加载游戏物体并实例化。
/// </summary> /// </summary>
/// <param name="location">资源定位地址。</param> /// <param name="location">资源定位地址。</param>
/// <param name="parent">资源实例父节点。</param>
/// <param name="cancellationToken">取消操作Token。</param> /// <param name="cancellationToken">取消操作Token。</param>
/// <param name="packageName">指定资源包的名称。不传使用默认资源包</param> /// <param name="packageName">指定资源包的名称。不传使用默认资源包</param>
/// <param name="parent">资源实例父节点。</param>
/// <returns>异步游戏物体实例。</returns> /// <returns>异步游戏物体实例。</returns>
/// <remarks>会实例化资源到场景无需主动UnloadAssetDestroy时自动UnloadAsset。</remarks> /// <remarks>会实例化资源到场景无需主动UnloadAssetDestroy时自动UnloadAsset。</remarks>
UniTask<GameObject> LoadGameObjectAsync(string location, CancellationToken cancellationToken = default, string packageName = "", Transform parent = null); UniTask<GameObject> LoadGameObjectAsync(string location, Transform parent = null, CancellationToken cancellationToken = default, string packageName = "");
} }
} }

View File

@@ -14,7 +14,6 @@ namespace TEngine
internal sealed partial class ResourceManager : ModuleImp, IResourceManager internal sealed partial class ResourceManager : ModuleImp, IResourceManager
{ {
#region Propreties #region Propreties
/// <summary> /// <summary>
/// 资源包名称。 /// 资源包名称。
/// </summary> /// </summary>
@@ -532,7 +531,7 @@ namespace TEngine
return ret; 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)) if (string.IsNullOrEmpty(location))
{ {
@@ -663,8 +662,7 @@ namespace TEngine
return handle.AssetObject as T; return handle.AssetObject as T;
} }
public async UniTask<GameObject> LoadGameObjectAsync(string location, CancellationToken cancellationToken = default, string packageName = "", public async UniTask<GameObject> LoadGameObjectAsync(string location, Transform parent = null, CancellationToken cancellationToken = default, string packageName = "")
Transform parent = null)
{ {
if (string.IsNullOrEmpty(location)) if (string.IsNullOrEmpty(location))
{ {
@@ -897,11 +895,32 @@ namespace TEngine
} }
} }
private readonly TimeoutController _timeoutController = new TimeoutController();
private async UniTask TryWaitingLoading(string assetObjectKey) private async UniTask TryWaitingLoading(string assetObjectKey)
{ {
if (_assetLoadingList.Contains(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 #endregion

View File

@@ -509,10 +509,10 @@ namespace TEngine
/// 同步加载游戏物体并实例化。 /// 同步加载游戏物体并实例化。
/// </summary> /// </summary>
/// <param name="location">资源的定位地址。</param> /// <param name="location">资源的定位地址。</param>
/// <param name="packageName">指定资源包的名称。不传使用默认资源包。</param>
/// <param name="parent">资源实例父节点。</param> /// <param name="parent">资源实例父节点。</param>
/// <param name="packageName">指定资源包的名称。不传使用默认资源包。</param>
/// <returns>资源实例。</returns> /// <returns>资源实例。</returns>
public GameObject LoadGameObject(string location, string packageName = "", Transform parent = null) public GameObject LoadGameObject(string location, Transform parent = null, string packageName = "")
{ {
if (string.IsNullOrEmpty(location)) if (string.IsNullOrEmpty(location))
{ {
@@ -520,7 +520,7 @@ namespace TEngine
return null; return null;
} }
return m_ResourceManager.LoadGameObject(location, packageName, parent); return m_ResourceManager.LoadGameObject(location, parent, packageName);
} }
/// <summary> /// <summary>
@@ -565,13 +565,12 @@ namespace TEngine
/// 异步加载游戏物体并实例化。 /// 异步加载游戏物体并实例化。
/// </summary> /// </summary>
/// <param name="location">资源定位地址。</param> /// <param name="location">资源定位地址。</param>
/// <param name="parent">资源实例父节点。</param>
/// <param name="cancellationToken">取消操作Token。</param> /// <param name="cancellationToken">取消操作Token。</param>
/// <param name="packageName">指定资源包的名称。不传使用默认资源包。</param> /// <param name="packageName">指定资源包的名称。不传使用默认资源包。</param>
/// <param name="parent">资源实例父节点。</param>
/// <returns>异步游戏物体实例。</returns> /// <returns>异步游戏物体实例。</returns>
public async UniTask<GameObject> LoadGameObjectAsync(string location, CancellationToken cancellationToken = default, public async UniTask<GameObject> LoadGameObjectAsync(string location, Transform parent = null, CancellationToken cancellationToken = default,
string packageName = "", string packageName = "")
Transform parent = null)
{ {
if (string.IsNullOrEmpty(location)) if (string.IsNullOrEmpty(location))
{ {
@@ -579,7 +578,7 @@ namespace TEngine
return null; return null;
} }
return await m_ResourceManager.LoadGameObjectAsync(location, cancellationToken, packageName, parent); return await m_ResourceManager.LoadGameObjectAsync(location, parent, cancellationToken, packageName);
} }
internal AssetHandle LoadAssetGetOperation<T>(string location, internal AssetHandle LoadAssetGetOperation<T>(string location,

View File

@@ -331,7 +331,7 @@ namespace TEngine
/// <returns>UIWidget实例。</returns> /// <returns>UIWidget实例。</returns>
public async UniTask<T> CreateWidgetByPathAsync<T>(Transform parentTrans, string assetLocation, bool visible = true) where T : UIWidget, new() public async UniTask<T> CreateWidgetByPathAsync<T>(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<T>(goInst, visible); return CreateWidget<T>(goInst, visible);
} }