mirror of
https://github.com/Alex-Rachel/TEngine.git
synced 2025-08-07 16:45:10 +00:00
更新资源模块接口
更新资源模块接口
This commit is contained in:
@@ -164,18 +164,20 @@ namespace TEngine
|
||||
/// 同步加载资源。
|
||||
/// </summary>
|
||||
/// <param name="location">资源的定位地址。</param>
|
||||
/// <param name="needInstance">是否需要实例化。</param>
|
||||
/// <typeparam name="T">要加载资源的类型。</typeparam>
|
||||
/// <returns>资源实例。</returns>
|
||||
T LoadAsset<T>(string location) where T : Object;
|
||||
T LoadAsset<T>(string location, bool needInstance) where T : Object;
|
||||
|
||||
/// <summary>
|
||||
/// 同步加载资源。
|
||||
/// </summary>
|
||||
/// <param name="location">资源的定位地址。</param>
|
||||
/// <param name="parent">父节点位置。</param>
|
||||
/// <param name="needInstance">是否需要实例化。</param>
|
||||
/// <typeparam name="T">要加载资源的类型。</typeparam>
|
||||
/// <returns>资源实例。</returns>
|
||||
T LoadAsset<T>(string location, Transform parent) where T : Object;
|
||||
T LoadAsset<T>(string location, Transform parent, bool needInstance) where T : Object;
|
||||
|
||||
/// <summary>
|
||||
/// 同步加载资源。
|
||||
@@ -248,7 +250,7 @@ namespace TEngine
|
||||
/// <param name="suspendLoad">加载完毕时是否主动挂起。</param>
|
||||
/// <param name="priority">优先级。</param>
|
||||
/// <returns>异步加载场景句柄。</returns>
|
||||
SceneOperationHandle LoadSceneAsync(string location, LoadSceneMode sceneMode = LoadSceneMode.Single, bool suspendLoad = false,int priority = 100);
|
||||
SceneOperationHandle LoadSceneAsync(string location, LoadSceneMode sceneMode = LoadSceneMode.Single, bool suspendLoad = false, int priority = 100);
|
||||
|
||||
/// <summary>
|
||||
/// 异步加载场景.
|
||||
@@ -266,9 +268,10 @@ namespace TEngine
|
||||
/// </summary>
|
||||
/// <param name="location">资源定位地址。</param>
|
||||
/// <param name="cancellationToken">取消操作Token。</param>
|
||||
/// <param name="needInstance">是否需要实例化。</param>
|
||||
/// <typeparam name="T">要加载资源的类型。</typeparam>
|
||||
/// <returns>异步资源实例。</returns>
|
||||
UniTask<T> LoadAssetAsync<T>(string location, CancellationToken cancellationToken = default) where T : Object;
|
||||
UniTask<T> LoadAssetAsync<T>(string location, CancellationToken cancellationToken = default, bool needInstance = true) where T : Object;
|
||||
|
||||
/// <summary>
|
||||
/// 异步加载游戏物体。
|
||||
|
@@ -90,6 +90,7 @@ namespace TEngine
|
||||
/// 资源缓存表容量。
|
||||
/// </summary>
|
||||
public int ARCTableCapacity { get; set; }
|
||||
|
||||
#endregion
|
||||
|
||||
#region 生命周期
|
||||
@@ -101,10 +102,43 @@ namespace TEngine
|
||||
|
||||
internal override void Shutdown()
|
||||
{
|
||||
ReleaseAllHandle();
|
||||
YooAssets.Destroy();
|
||||
ResourcePool.Destroy();
|
||||
}
|
||||
|
||||
private void ReleaseAllHandle()
|
||||
{
|
||||
var iter = _releaseMaps.Values.GetEnumerator();
|
||||
while (iter.MoveNext())
|
||||
{
|
||||
AssetOperationHandle handle = iter.Current;
|
||||
if (handle != null)
|
||||
{
|
||||
handle.Dispose();
|
||||
handle = null;
|
||||
}
|
||||
}
|
||||
|
||||
iter.Dispose();
|
||||
_releaseMaps.Clear();
|
||||
|
||||
iter = _operationHandlesMaps.Values.GetEnumerator();
|
||||
while (iter.MoveNext())
|
||||
{
|
||||
AssetOperationHandle handle = iter.Current;
|
||||
if (handle != null)
|
||||
{
|
||||
handle.Dispose();
|
||||
handle = null;
|
||||
}
|
||||
}
|
||||
|
||||
iter.Dispose();
|
||||
_operationHandlesMaps.Clear();
|
||||
_arcCacheTable = new ArcCacheTable<string, AssetOperationHandle>(ARCTableCapacity, OnAddAsset, OnRemoveAsset);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 设置接口
|
||||
@@ -141,20 +175,29 @@ namespace TEngine
|
||||
|
||||
private Dictionary<string, AssetOperationHandle> _releaseMaps;
|
||||
|
||||
private Dictionary<string, AssetOperationHandle> _operationHandlesMaps;
|
||||
|
||||
private ArcCacheTable<string, AssetOperationHandle> _arcCacheTable;
|
||||
|
||||
private void OnAddAsset(string location,AssetOperationHandle handle)
|
||||
|
||||
private void OnAddAsset(string location, AssetOperationHandle handle)
|
||||
{
|
||||
_operationHandlesMaps[location] = handle;
|
||||
if (_releaseMaps.ContainsKey(location))
|
||||
{
|
||||
_releaseMaps.Remove(location);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnRemoveAsset(string location,AssetOperationHandle handle)
|
||||
private void OnRemoveAsset(string location, AssetOperationHandle handle)
|
||||
{
|
||||
if (_operationHandlesMaps.ContainsKey(location))
|
||||
{
|
||||
_operationHandlesMaps.Remove(location);
|
||||
}
|
||||
|
||||
_releaseMaps[location] = handle;
|
||||
GameModule.Resource.UnloadUnusedAssets(performGCCollect:false);
|
||||
GameModule.Resource.UnloadUnusedAssets(performGCCollect: false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -173,6 +216,7 @@ namespace TEngine
|
||||
{
|
||||
handle = YooAssets.LoadAssetSync<T>(location);
|
||||
}
|
||||
|
||||
// 对象推入ARC缓存表。
|
||||
_arcCacheTable.PutCache(location, handle);
|
||||
return handle;
|
||||
@@ -194,6 +238,7 @@ namespace TEngine
|
||||
{
|
||||
handle = YooAssets.LoadAssetAsync<T>(location);
|
||||
}
|
||||
|
||||
// 对象推入ARC缓存表。
|
||||
_arcCacheTable.PutCache(location, handle);
|
||||
return handle;
|
||||
@@ -221,6 +266,7 @@ namespace TEngine
|
||||
ResourcePool.Initialize(GameModule.Get<ResourceModule>().gameObject);
|
||||
|
||||
_releaseMaps ??= new Dictionary<string, AssetOperationHandle>(ARCTableCapacity);
|
||||
_operationHandlesMaps ??= new Dictionary<string, AssetOperationHandle>(ARCTableCapacity);
|
||||
_arcCacheTable ??= new ArcCacheTable<string, AssetOperationHandle>(ARCTableCapacity, OnAddAsset, OnRemoveAsset);
|
||||
}
|
||||
|
||||
@@ -317,6 +363,7 @@ namespace TEngine
|
||||
handle = null;
|
||||
}
|
||||
}
|
||||
|
||||
iter.Dispose();
|
||||
_releaseMaps.Clear();
|
||||
|
||||
@@ -451,9 +498,10 @@ namespace TEngine
|
||||
/// 同步加载资源。
|
||||
/// </summary>
|
||||
/// <param name="location">资源的定位地址。</param>
|
||||
/// <param name="needInstance">是否需要实例化。</param>
|
||||
/// <typeparam name="T">要加载资源的类型。</typeparam>
|
||||
/// <returns>资源实例。</returns>
|
||||
public T LoadAsset<T>(string location) where T : Object
|
||||
public T LoadAsset<T>(string location, bool needInstance = true) where T : Object
|
||||
{
|
||||
if (string.IsNullOrEmpty(location))
|
||||
{
|
||||
@@ -465,8 +513,13 @@ namespace TEngine
|
||||
|
||||
if (typeof(T) == typeof(GameObject))
|
||||
{
|
||||
GameObject ret = handle.InstantiateSync();
|
||||
return ret as T;
|
||||
if (needInstance)
|
||||
{
|
||||
GameObject ret = handle.InstantiateSync();
|
||||
return ret as T;
|
||||
}
|
||||
|
||||
return handle.AssetObject as T;
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -480,9 +533,10 @@ namespace TEngine
|
||||
/// </summary>
|
||||
/// <param name="location">资源的定位地址。</param>
|
||||
/// <param name="parent">父节点位置。</param>
|
||||
/// <param name="needInstance">是否需要实例化。</param>
|
||||
/// <typeparam name="T">要加载资源的类型。</typeparam>
|
||||
/// <returns>资源实例。</returns>
|
||||
public T LoadAsset<T>(string location, Transform parent) where T : Object
|
||||
public T LoadAsset<T>(string location, Transform parent, bool needInstance = true) where T : Object
|
||||
{
|
||||
if (string.IsNullOrEmpty(location))
|
||||
{
|
||||
@@ -494,8 +548,13 @@ namespace TEngine
|
||||
|
||||
if (typeof(T) == typeof(GameObject))
|
||||
{
|
||||
GameObject ret = handle.InstantiateSync(parent);
|
||||
return ret as T;
|
||||
if (needInstance)
|
||||
{
|
||||
GameObject ret = handle.InstantiateSync();
|
||||
return ret as T;
|
||||
}
|
||||
|
||||
return handle.AssetObject as T;
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -617,7 +676,7 @@ namespace TEngine
|
||||
/// <param name="assetTag">资源标识。</param>
|
||||
/// <typeparam name="T">资源类型。</typeparam>
|
||||
/// <returns>资源对象集合。</returns>
|
||||
public async UniTask<List<T>>LoadAssetsByTagAsync<T>(string assetTag) where T: UnityEngine.Object
|
||||
public async UniTask<List<T>> LoadAssetsByTagAsync<T>(string assetTag) where T : UnityEngine.Object
|
||||
{
|
||||
LoadAssetsByTagOperation<T> operation = new LoadAssetsByTagOperation<T>(assetTag);
|
||||
YooAssets.StartOperation(operation);
|
||||
@@ -661,8 +720,9 @@ namespace TEngine
|
||||
/// </summary>
|
||||
/// <param name="location">要加载的实例名称。</param>
|
||||
/// <param name="cancellationToken">取消操作Token。</param>
|
||||
/// <param name="needInstance">是否需要实例化。</param>
|
||||
/// <returns>资源实实例。</returns>
|
||||
public async UniTask<T> LoadAssetAsync<T>(string location, CancellationToken cancellationToken = default) where T : Object
|
||||
public async UniTask<T> LoadAssetAsync<T>(string location, CancellationToken cancellationToken = default, bool needInstance = true) where T : Object
|
||||
{
|
||||
AssetOperationHandle handle = LoadAssetAsyncHandle<T>(location);
|
||||
|
||||
@@ -675,9 +735,13 @@ namespace TEngine
|
||||
|
||||
if (typeof(T) == typeof(GameObject))
|
||||
{
|
||||
GameObject ret = handle.InstantiateSync();
|
||||
if (needInstance)
|
||||
{
|
||||
GameObject ret = handle.InstantiateSync();
|
||||
return ret as T;
|
||||
}
|
||||
|
||||
return ret as T;
|
||||
return handle.AssetObject as T;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@@ -448,11 +448,12 @@ namespace TEngine
|
||||
/// 同步加载资源。
|
||||
/// </summary>
|
||||
/// <param name="location">资源的定位地址。</param>
|
||||
/// <param name="needInstance">是否需要实例化。</param>
|
||||
/// <typeparam name="T">要加载资源的类型。</typeparam>
|
||||
/// <returns>资源实例。</returns>
|
||||
public T LoadAsset<T>(string location) where T : UnityEngine.Object
|
||||
public T LoadAsset<T>(string location, bool needInstance = true) where T : UnityEngine.Object
|
||||
{
|
||||
return m_ResourceManager.LoadAsset<T>(location);
|
||||
return m_ResourceManager.LoadAsset<T>(location, needInstance);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -460,11 +461,12 @@ namespace TEngine
|
||||
/// </summary>
|
||||
/// <param name="location">资源的定位地址。</param>
|
||||
/// <param name="parent">父节点位置。</param>
|
||||
/// <param name="needInstance">是否需要实例化。</param>
|
||||
/// <typeparam name="T">要加载资源的类型。</typeparam>
|
||||
/// <returns>资源实例。</returns>
|
||||
public T LoadAsset<T>(string location, Transform parent) where T : UnityEngine.Object
|
||||
public T LoadAsset<T>(string location, Transform parent, bool needInstance = true) where T : UnityEngine.Object
|
||||
{
|
||||
return m_ResourceManager.LoadAsset<T>(location, parent);
|
||||
return m_ResourceManager.LoadAsset<T>(location, parent, needInstance);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -570,7 +572,7 @@ namespace TEngine
|
||||
/// <param name="assetTag">资源标识。</param>
|
||||
/// <typeparam name="T">资源类型。</typeparam>
|
||||
/// <returns>资源对象集合。</returns>
|
||||
public async UniTask<List<T>>LoadAssetsByTagAsync<T>(string assetTag) where T: UnityEngine.Object
|
||||
public async UniTask<List<T>> LoadAssetsByTagAsync<T>(string assetTag) where T : UnityEngine.Object
|
||||
{
|
||||
return await m_ResourceManager.LoadAssetsByTagAsync<T>(assetTag);
|
||||
}
|
||||
@@ -608,11 +610,12 @@ namespace TEngine
|
||||
/// </summary>
|
||||
/// <param name="location">资源的定位地址。</param>
|
||||
/// <param name="cancellationToken">取消操作Token。</param>
|
||||
/// <param name="needInstance">是否需要实例化。</param>
|
||||
/// <typeparam name="T">要加载资源的类型。</typeparam>
|
||||
/// <returns>异步资源实例。</returns>
|
||||
public async UniTask<T> LoadAssetAsync<T>(string location, CancellationToken cancellationToken = default) where T : UnityEngine.Object
|
||||
public async UniTask<T> LoadAssetAsync<T>(string location, CancellationToken cancellationToken = default, bool needInstance = true) where T : UnityEngine.Object
|
||||
{
|
||||
return await m_ResourceManager.LoadAssetAsync<T>(location, cancellationToken);
|
||||
return await m_ResourceManager.LoadAssetAsync<T>(location, cancellationToken, needInstance);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
Reference in New Issue
Block a user