ResourceModule

ResourceModule
This commit is contained in:
ALEXTANG
2023-04-01 23:13:04 +08:00
parent f571605b9a
commit 933785205d
14 changed files with 469 additions and 7 deletions

View File

@@ -151,7 +151,7 @@ Transform:
m_LocalScale: {x: 1, y: 1, z: 1} m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: [] m_Children: []
m_Father: {fileID: 2061060682} m_Father: {fileID: 2061060682}
m_RootOrder: 3 m_RootOrder: 2
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &43232119 --- !u!1 &43232119
GameObject: GameObject:
@@ -181,7 +181,7 @@ Transform:
m_LocalScale: {x: 1, y: 1, z: 1} m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: [] m_Children: []
m_Father: {fileID: 2061060682} m_Father: {fileID: 2061060682}
m_RootOrder: 2 m_RootOrder: 1
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &230270237 --- !u!1 &230270237
GameObject: GameObject:
@@ -211,7 +211,7 @@ Transform:
m_LocalScale: {x: 1, y: 1, z: 1} m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: [] m_Children: []
m_Father: {fileID: 2061060682} m_Father: {fileID: 2061060682}
m_RootOrder: 1 m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &1513514422 --- !u!1 &1513514422
GameObject: GameObject:
@@ -241,7 +241,7 @@ Transform:
m_LocalScale: {x: 1, y: 1, z: 1} m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: [] m_Children: []
m_Father: {fileID: 2061060682} m_Father: {fileID: 2061060682}
m_RootOrder: 4 m_RootOrder: 3
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &1947304784 --- !u!1 &1947304784
GameObject: GameObject:
@@ -272,7 +272,7 @@ Transform:
m_LocalScale: {x: 1, y: 1, z: 1} m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: [] m_Children: []
m_Father: {fileID: 2061060682} m_Father: {fileID: 2061060682}
m_RootOrder: 5 m_RootOrder: 4
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &1947304786 --- !u!114 &1947304786
MonoBehaviour: MonoBehaviour:
@@ -398,12 +398,12 @@ Transform:
m_LocalPosition: {x: 0, y: 0, z: 0} m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1} m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: m_Children:
- {fileID: 2108959336}
- {fileID: 230270238} - {fileID: 230270238}
- {fileID: 43232120} - {fileID: 43232120}
- {fileID: 23410075} - {fileID: 23410075}
- {fileID: 1513514423} - {fileID: 1513514423}
- {fileID: 1947304785} - {fileID: 1947304785}
- {fileID: 2108959336}
m_Father: {fileID: 0} m_Father: {fileID: 0}
m_RootOrder: 1 m_RootOrder: 1
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
@@ -457,5 +457,5 @@ Transform:
m_LocalScale: {x: 1, y: 1, z: 1} m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: [] m_Children: []
m_Father: {fileID: 2061060682} m_Father: {fileID: 2061060682}
m_RootOrder: 0 m_RootOrder: 5
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}

View File

@@ -0,0 +1,13 @@
namespace TEngine
{
/// <summary>
/// 资源相关常量。
/// </summary>
internal static class Constant
{
/// <summary>
/// 默认资源加载优先级。
/// </summary>
internal const int DefaultPriority = 0;
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 073f5d61de554169be4859dc3a577f9f
timeCreated: 1680342144

View File

@@ -0,0 +1,38 @@
namespace TEngine
{
/// <summary>
/// 检查资源是否存在的结果。
/// </summary>
public enum HasAssetResult : byte
{
/// <summary>
/// 资源不存在。
/// </summary>
NotExist = 0,
/// <summary>
/// 资源尚未准备完毕。
/// </summary>
NotReady,
/// <summary>
/// 存在资源且存储在磁盘上。
/// </summary>
AssetOnDisk,
/// <summary>
/// 存在资源且存储在文件系统里。
/// </summary>
AssetOnFileSystem,
/// <summary>
/// 存在二进制资源且存储在磁盘上。
/// </summary>
BinaryOnDisk,
/// <summary>
/// 存在二进制资源且存储在文件系统里。
/// </summary>
BinaryOnFileSystem
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 31bf1827322742d5af25eec6f4e4c447
timeCreated: 1680342202

View File

@@ -0,0 +1,156 @@
using System.Threading;
using Cysharp.Threading.Tasks;
using UnityEngine;
using YooAsset;
namespace TEngine
{
public interface IResourceManager
{
/// <summary>
/// 获取当前资源适用的游戏版本号。
/// </summary>
string ApplicableGameVersion { get; }
/// <summary>
/// 获取当前内部资源版本号。
/// </summary>
int InternalResourceVersion { get; }
/// <summary>
/// 同时下载的最大数目。
/// </summary>
int DownloadingMaxNum { get; set; }
/// <summary>
/// 失败重试最大数目。
/// </summary>
int FailedTryAgain { get; set; }
/// <summary>
/// 获取资源只读区路径。
/// </summary>
string ReadOnlyPath { get; }
/// <summary>
/// 获取资源读写区路径。
/// </summary>
string ReadWritePath { get; }
/// <summary>
/// 获取或设置资源包名称。
/// </summary>
string PackageName { get; set; }
/// <summary>
/// 获取或设置运行模式。
/// </summary>
EPlayMode PlayMode { get; set; }
/// <summary>
/// 获取或设置下载文件校验等级。
/// </summary>
EVerifyLevel VerifyLevel { get; set; }
/// <summary>
/// 获取或设置异步系统参数,每帧执行消耗的最大时间切片(单位:毫秒)。
/// </summary>
long Milliseconds { get; set; }
/// <summary>
/// 设置资源只读区路径。
/// </summary>
/// <param name="readOnlyPath">资源只读区路径。</param>
void SetReadOnlyPath(string readOnlyPath);
/// <summary>
/// 设置资源读写区路径。
/// </summary>
/// <param name="readWritePath">资源读写区路径。</param>
void SetReadWritePath(string readWritePath);
/// <summary>
/// 初始化接口。
/// </summary>
void Initialize();
/// <summary>
/// 初始化操作。
/// </summary>
/// <returns></returns>
InitializationOperation InitPackage();
/// <summary>
/// 卸载资源。
/// </summary>
/// <param name="asset">要卸载的资源。</param>
void UnloadAsset(object asset);
/// <summary>
/// 资源回收(卸载引用计数为零的资源)
/// </summary>
void UnloadUnusedAssets();
/// <summary>
/// 强制回收所有资源
/// </summary>
void ForceUnloadAllAssets();
/// <summary>
/// 检查资源是否存在。
/// </summary>
/// <param name="assetName">要检查资源的名称。</param>
/// <returns>检查资源是否存在的结果。</returns>
HasAssetResult HasAsset(string assetName);
/// <summary>
/// 同步加载资源。
/// </summary>
/// <param name="assetName">要加载资源的名称。</param>
/// <typeparam name="T">要加载资源的类型。</typeparam>
/// <returns>资源实例。</returns>
T LoadAsset<T>(string assetName) where T : UnityEngine.Object;
/// <summary>
/// 同步加载资源。
/// </summary>
/// <param name="assetName">要加载资源的名称。</param>
/// <param name="parent">父节点位置。</param>
/// <typeparam name="T">要加载资源的类型。</typeparam>
/// <returns>资源实例。</returns>
T LoadAsset<T>(string assetName, Transform parent) where T : UnityEngine.Object;
/// <summary>
/// 异步加载资源。
/// </summary>
/// <param name="assetName">要加载资源的名称。</param>
/// <param name="cancellationToken">取消操作Token。</param>
/// <typeparam name="T">要加载资源的类型。</typeparam>
/// <returns>异步资源实例。</returns>
UniTask<T> LoadAssetAsync<T>(string assetName,CancellationToken cancellationToken) where T : UnityEngine.Object;
/// <summary>
/// 异步加载游戏物体。
/// </summary>
/// <param name="assetName">要加载的游戏物体名称。</param>
/// <param name="cancellationToken">取消操作Token。</param>
/// <returns>异步游戏物体实例。</returns>
UniTask<UnityEngine.GameObject> LoadGameObjectAsync(string assetName,CancellationToken cancellationToken);
/// <summary>
/// 同步加载资源并获取句柄。
/// </summary>
/// <param name="assetName">要加载资源的名称。</param>
/// <typeparam name="T">要加载资源的类型。</typeparam>
/// <returns>同步加载资源句柄。</returns>
AssetOperationHandle LoadAssetGetOperation<T>(string assetName) where T : UnityEngine.Object;
/// <summary>
/// 异步加载资源并获取句柄。
/// </summary>
/// <param name="assetName">要加载资源的名称。</param>
/// <typeparam name="T">要加载资源的类型。</typeparam>
/// <returns>同步加载资源句柄。</returns>
AssetOperationHandle LoadAssetAsyncHandle<T>(string assetName) where T : UnityEngine.Object;
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 4b0d8823b6744ba5b2ed48ffcce56e54
timeCreated: 1680342298

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 1d3946c878f8452385e7127eb12bd5a4
timeCreated: 1680342235

View File

@@ -0,0 +1,120 @@
using System.Collections.Generic;
using YooAsset;
namespace TEngine
{
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}";
Log.Error($"{error}");
AssetObjects.Clear();
SetFinish(false, error);
return;
}
}
else
{
Log.Error($"{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();
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: cd96a13f5f98496ba608a667fb7d9144
timeCreated: 1680342243

View File

@@ -0,0 +1,23 @@
namespace TEngine
{
/// <summary>
/// 读写区路径类型。
/// </summary>
public enum ReadWritePathType : byte
{
/// <summary>
/// 未指定。
/// </summary>
Unspecified = 0,
/// <summary>
/// 临时缓存。
/// </summary>
TemporaryCache,
/// <summary>
/// 持久化数据。
/// </summary>
PersistentData,
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 458eb2d0dde14960adf21204432a5979
timeCreated: 1680342271

View File

@@ -0,0 +1,91 @@
using System.Threading;
using Cysharp.Threading.Tasks;
using UnityEngine;
using YooAsset;
namespace TEngine
{
internal partial class ResourceManager:IResourceManager
{
public string ApplicableGameVersion { get; }
public int InternalResourceVersion { get; }
public int DownloadingMaxNum { get; set; }
public int FailedTryAgain { get; set; }
public string ReadOnlyPath { get; }
public string ReadWritePath { get; }
public string PackageName { get; set; }
public EPlayMode PlayMode { get; set; }
public EVerifyLevel VerifyLevel { get; set; }
public long Milliseconds { get; set; }
public void SetReadOnlyPath(string readOnlyPath)
{
throw new System.NotImplementedException();
}
public void SetReadWritePath(string readWritePath)
{
throw new System.NotImplementedException();
}
public void Initialize()
{
throw new System.NotImplementedException();
}
public InitializationOperation InitPackage()
{
throw new System.NotImplementedException();
}
public void UnloadAsset(object asset)
{
throw new System.NotImplementedException();
}
public void UnloadUnusedAssets()
{
throw new System.NotImplementedException();
}
public void ForceUnloadAllAssets()
{
throw new System.NotImplementedException();
}
public HasAssetResult HasAsset(string assetName)
{
throw new System.NotImplementedException();
}
public T LoadAsset<T>(string assetName) where T : Object
{
throw new System.NotImplementedException();
}
public T LoadAsset<T>(string assetName, Transform parent) where T : Object
{
throw new System.NotImplementedException();
}
public UniTask<T> LoadAssetAsync<T>(string assetName, CancellationToken cancellationToken) where T : Object
{
throw new System.NotImplementedException();
}
public UniTask<GameObject> LoadGameObjectAsync(string assetName, CancellationToken cancellationToken)
{
throw new System.NotImplementedException();
}
public AssetOperationHandle LoadAssetGetOperation<T>(string assetName) where T : Object
{
throw new System.NotImplementedException();
}
public AssetOperationHandle LoadAssetAsyncHandle<T>(string assetName) where T : Object
{
throw new System.NotImplementedException();
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 56829b699b8c4fcda57e0df5e9516e49
timeCreated: 1680361920