mirror of
https://github.com/Alex-Rachel/TEngine.git
synced 2025-08-07 16:45:10 +00:00
增加通过Tag加载资源对象集合的接口。
增加通过Tag加载资源对象集合的接口。
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using System.Threading;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
@@ -226,6 +227,14 @@ namespace TEngine
|
||||
/// <param name="assetInfo">资源信息。</param>
|
||||
public SubAssetsOperationHandle LoadSubAssetsSync(AssetInfo assetInfo);
|
||||
|
||||
/// <summary>
|
||||
/// 通过Tag加载资源对象集合。
|
||||
/// </summary>
|
||||
/// <param name="assetTag">资源标识。</param>
|
||||
/// <typeparam name="T">资源类型。</typeparam>
|
||||
/// <returns>资源对象集合。</returns>
|
||||
UniTask<List<T>> LoadAssetsByTagAsync<T>(string assetTag) where T : UnityEngine.Object;
|
||||
|
||||
/// <summary>
|
||||
/// 异步加载场景。
|
||||
/// </summary>
|
||||
|
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3b36f61f6af84adcb3bdba5976c7d9e6
|
||||
timeCreated: 1696849773
|
@@ -0,0 +1,125 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using YooAsset;
|
||||
|
||||
namespace TEngine
|
||||
{
|
||||
/// <summary>
|
||||
/// 通过资源标识加载资源。
|
||||
/// </summary>
|
||||
/// <typeparam name="TObject">资源实例类型。</typeparam>
|
||||
public class LoadAssetsByTagOperation<TObject> : GameAsyncOperation where TObject : UnityEngine.Object
|
||||
{
|
||||
private enum ESteps
|
||||
{
|
||||
None,
|
||||
LoadAssets,
|
||||
CheckResult,
|
||||
Done,
|
||||
}
|
||||
|
||||
private readonly string _tag;
|
||||
private ESteps _steps = ESteps.None;
|
||||
private List<AssetOperationHandle> _handles;
|
||||
|
||||
/// <summary>
|
||||
/// 资源对象集合
|
||||
/// </summary>
|
||||
public List<TObject> AssetObjects { private set; get; }
|
||||
|
||||
|
||||
public LoadAssetsByTagOperation(string tag)
|
||||
{
|
||||
_tag = tag;
|
||||
}
|
||||
|
||||
protected override void OnStart()
|
||||
{
|
||||
_steps = ESteps.LoadAssets;
|
||||
}
|
||||
|
||||
protected override void OnUpdate()
|
||||
{
|
||||
if (_steps == ESteps.None || _steps == ESteps.Done)
|
||||
return;
|
||||
|
||||
if (_steps == ESteps.LoadAssets)
|
||||
{
|
||||
AssetInfo[] assetInfos = YooAssets.GetAssetInfos(_tag);
|
||||
_handles = new List<AssetOperationHandle>(assetInfos.Length);
|
||||
foreach (var assetInfo in assetInfos)
|
||||
{
|
||||
var handle = YooAssets.LoadAssetAsync(assetInfo);
|
||||
_handles.Add(handle);
|
||||
}
|
||||
|
||||
_steps = ESteps.CheckResult;
|
||||
}
|
||||
|
||||
if (_steps == ESteps.CheckResult)
|
||||
{
|
||||
int index = 0;
|
||||
foreach (var handle in _handles)
|
||||
{
|
||||
if (handle.IsDone == false)
|
||||
{
|
||||
Progress = (float)index / _handles.Count;
|
||||
return;
|
||||
}
|
||||
|
||||
index++;
|
||||
}
|
||||
|
||||
AssetObjects = new List<TObject>(_handles.Count);
|
||||
foreach (var handle in _handles)
|
||||
{
|
||||
if (handle.Status == EOperationStatus.Succeed)
|
||||
{
|
||||
var assetObject = handle.AssetObject as TObject;
|
||||
if (assetObject != null)
|
||||
{
|
||||
AssetObjects.Add(assetObject);
|
||||
}
|
||||
else
|
||||
{
|
||||
string error = $"资源类型转换失败:{handle.AssetObject.name}";
|
||||
Debug.LogError($"{error}");
|
||||
AssetObjects.Clear();
|
||||
SetFinish(false, error);
|
||||
return;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError($"{handle.LastError}");
|
||||
AssetObjects.Clear();
|
||||
SetFinish(false, handle.LastError);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
SetFinish(true);
|
||||
}
|
||||
}
|
||||
|
||||
private void SetFinish(bool succeed, string error = "")
|
||||
{
|
||||
Error = error;
|
||||
Status = succeed ? EOperationStatus.Succeed : EOperationStatus.Failed;
|
||||
_steps = ESteps.Done;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 释放资源句柄
|
||||
/// </summary>
|
||||
public void ReleaseHandle()
|
||||
{
|
||||
foreach (var handle in _handles)
|
||||
{
|
||||
handle.Release();
|
||||
}
|
||||
|
||||
_handles.Clear();
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b49f6372b68a4871baed40595ac22c61
|
||||
timeCreated: 1696849782
|
@@ -1,4 +1,5 @@
|
||||
using System.Threading;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
@@ -534,6 +535,22 @@ namespace TEngine
|
||||
{
|
||||
return YooAssets.LoadSubAssetsSync(assetInfo);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 通过Tag加载资源对象集合。
|
||||
/// </summary>
|
||||
/// <param name="assetTag">资源标识。</param>
|
||||
/// <typeparam name="T">资源类型。</typeparam>
|
||||
/// <returns>资源对象集合。</returns>
|
||||
public async UniTask<List<T>>LoadAssetsByTagAsync<T>(string assetTag) where T: UnityEngine.Object
|
||||
{
|
||||
LoadAssetsByTagOperation<T> operation = new LoadAssetsByTagOperation<T>(assetTag);
|
||||
YooAssets.StartOperation(operation);
|
||||
await operation.ToUniTask();
|
||||
List<T> assetObjects = operation.AssetObjects;
|
||||
operation.ReleaseHandle();
|
||||
return assetObjects;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步加载场景。
|
||||
|
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
@@ -536,6 +537,17 @@ namespace TEngine
|
||||
|
||||
return m_ResourceManager.LoadSubAssetsSync(assetInfo);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 通过Tag加载资源对象集合。
|
||||
/// </summary>
|
||||
/// <param name="assetTag">资源标识。</param>
|
||||
/// <typeparam name="T">资源类型。</typeparam>
|
||||
/// <returns>资源对象集合。</returns>
|
||||
public async UniTask<List<T>>LoadAssetsByTagAsync<T>(string assetTag) where T: UnityEngine.Object
|
||||
{
|
||||
return await m_ResourceManager.LoadAssetsByTagAsync<T>(assetTag);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步加载场景。
|
||||
|
Reference in New Issue
Block a user