From 933785205d192c7684f4981f08dcf9ca22fc9b51 Mon Sep 17 00:00:00 2001
From: ALEXTANG <574809918@qq.com>
Date: Sat, 1 Apr 2023 23:13:04 +0800
Subject: [PATCH] ResourceModule
ResourceModule
---
Assets/Scenes/main.unity | 14 +-
Assets/TEngine/Runtime/Resource/Constant.cs | 13 ++
.../TEngine/Runtime/Resource/Constant.cs.meta | 3 +
.../Runtime/Resource/HasAssetResult.cs | 38 +++++
.../Runtime/Resource/HasAssetResult.cs.meta | 3 +
.../Runtime/Resource/IResourceManager.cs | 156 ++++++++++++++++++
.../Runtime/Resource/IResourceManager.cs.meta | 3 +
.../TEngine/Runtime/Resource/Operation.meta | 3 +
.../Operation/LoadAssetsByTagOperation.cs | 120 ++++++++++++++
.../LoadAssetsByTagOperation.cs.meta | 3 +
.../Runtime/Resource/ReadWritePathType.cs | 23 +++
.../Resource/ReadWritePathType.cs.meta | 3 +
.../Runtime/Resource/ResourceManager.cs | 91 ++++++++++
.../Runtime/Resource/ResourceManager.cs.meta | 3 +
14 files changed, 469 insertions(+), 7 deletions(-)
create mode 100644 Assets/TEngine/Runtime/Resource/Constant.cs
create mode 100644 Assets/TEngine/Runtime/Resource/Constant.cs.meta
create mode 100644 Assets/TEngine/Runtime/Resource/HasAssetResult.cs
create mode 100644 Assets/TEngine/Runtime/Resource/HasAssetResult.cs.meta
create mode 100644 Assets/TEngine/Runtime/Resource/IResourceManager.cs
create mode 100644 Assets/TEngine/Runtime/Resource/IResourceManager.cs.meta
create mode 100644 Assets/TEngine/Runtime/Resource/Operation.meta
create mode 100644 Assets/TEngine/Runtime/Resource/Operation/LoadAssetsByTagOperation.cs
create mode 100644 Assets/TEngine/Runtime/Resource/Operation/LoadAssetsByTagOperation.cs.meta
create mode 100644 Assets/TEngine/Runtime/Resource/ReadWritePathType.cs
create mode 100644 Assets/TEngine/Runtime/Resource/ReadWritePathType.cs.meta
create mode 100644 Assets/TEngine/Runtime/Resource/ResourceManager.cs
create mode 100644 Assets/TEngine/Runtime/Resource/ResourceManager.cs.meta
diff --git a/Assets/Scenes/main.unity b/Assets/Scenes/main.unity
index 0b0bc483..a273df22 100644
--- a/Assets/Scenes/main.unity
+++ b/Assets/Scenes/main.unity
@@ -151,7 +151,7 @@ Transform:
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 2061060682}
- m_RootOrder: 3
+ m_RootOrder: 2
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &43232119
GameObject:
@@ -181,7 +181,7 @@ Transform:
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 2061060682}
- m_RootOrder: 2
+ m_RootOrder: 1
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &230270237
GameObject:
@@ -211,7 +211,7 @@ Transform:
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 2061060682}
- m_RootOrder: 1
+ m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &1513514422
GameObject:
@@ -241,7 +241,7 @@ Transform:
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 2061060682}
- m_RootOrder: 4
+ m_RootOrder: 3
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &1947304784
GameObject:
@@ -272,7 +272,7 @@ Transform:
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 2061060682}
- m_RootOrder: 5
+ m_RootOrder: 4
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &1947304786
MonoBehaviour:
@@ -398,12 +398,12 @@ Transform:
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children:
- - {fileID: 2108959336}
- {fileID: 230270238}
- {fileID: 43232120}
- {fileID: 23410075}
- {fileID: 1513514423}
- {fileID: 1947304785}
+ - {fileID: 2108959336}
m_Father: {fileID: 0}
m_RootOrder: 1
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
@@ -457,5 +457,5 @@ Transform:
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 2061060682}
- m_RootOrder: 0
+ m_RootOrder: 5
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
diff --git a/Assets/TEngine/Runtime/Resource/Constant.cs b/Assets/TEngine/Runtime/Resource/Constant.cs
new file mode 100644
index 00000000..f068527e
--- /dev/null
+++ b/Assets/TEngine/Runtime/Resource/Constant.cs
@@ -0,0 +1,13 @@
+namespace TEngine
+{
+ ///
+ /// 资源相关常量。
+ ///
+ internal static class Constant
+ {
+ ///
+ /// 默认资源加载优先级。
+ ///
+ internal const int DefaultPriority = 0;
+ }
+}
diff --git a/Assets/TEngine/Runtime/Resource/Constant.cs.meta b/Assets/TEngine/Runtime/Resource/Constant.cs.meta
new file mode 100644
index 00000000..afa6ed31
--- /dev/null
+++ b/Assets/TEngine/Runtime/Resource/Constant.cs.meta
@@ -0,0 +1,3 @@
+fileFormatVersion: 2
+guid: 073f5d61de554169be4859dc3a577f9f
+timeCreated: 1680342144
\ No newline at end of file
diff --git a/Assets/TEngine/Runtime/Resource/HasAssetResult.cs b/Assets/TEngine/Runtime/Resource/HasAssetResult.cs
new file mode 100644
index 00000000..99bcf951
--- /dev/null
+++ b/Assets/TEngine/Runtime/Resource/HasAssetResult.cs
@@ -0,0 +1,38 @@
+namespace TEngine
+{
+ ///
+ /// 检查资源是否存在的结果。
+ ///
+ public enum HasAssetResult : byte
+ {
+ ///
+ /// 资源不存在。
+ ///
+ NotExist = 0,
+
+ ///
+ /// 资源尚未准备完毕。
+ ///
+ NotReady,
+
+ ///
+ /// 存在资源且存储在磁盘上。
+ ///
+ AssetOnDisk,
+
+ ///
+ /// 存在资源且存储在文件系统里。
+ ///
+ AssetOnFileSystem,
+
+ ///
+ /// 存在二进制资源且存储在磁盘上。
+ ///
+ BinaryOnDisk,
+
+ ///
+ /// 存在二进制资源且存储在文件系统里。
+ ///
+ BinaryOnFileSystem
+ }
+}
diff --git a/Assets/TEngine/Runtime/Resource/HasAssetResult.cs.meta b/Assets/TEngine/Runtime/Resource/HasAssetResult.cs.meta
new file mode 100644
index 00000000..a208a58a
--- /dev/null
+++ b/Assets/TEngine/Runtime/Resource/HasAssetResult.cs.meta
@@ -0,0 +1,3 @@
+fileFormatVersion: 2
+guid: 31bf1827322742d5af25eec6f4e4c447
+timeCreated: 1680342202
\ No newline at end of file
diff --git a/Assets/TEngine/Runtime/Resource/IResourceManager.cs b/Assets/TEngine/Runtime/Resource/IResourceManager.cs
new file mode 100644
index 00000000..c06ca3ae
--- /dev/null
+++ b/Assets/TEngine/Runtime/Resource/IResourceManager.cs
@@ -0,0 +1,156 @@
+using System.Threading;
+using Cysharp.Threading.Tasks;
+using UnityEngine;
+using YooAsset;
+
+namespace TEngine
+{
+ public interface IResourceManager
+ {
+ ///
+ /// 获取当前资源适用的游戏版本号。
+ ///
+ string ApplicableGameVersion { get; }
+
+ ///
+ /// 获取当前内部资源版本号。
+ ///
+ int InternalResourceVersion { get; }
+
+ ///
+ /// 同时下载的最大数目。
+ ///
+ int DownloadingMaxNum { get; set; }
+
+ ///
+ /// 失败重试最大数目。
+ ///
+ int FailedTryAgain { get; set; }
+
+ ///
+ /// 获取资源只读区路径。
+ ///
+ string ReadOnlyPath { get; }
+
+ ///
+ /// 获取资源读写区路径。
+ ///
+ string ReadWritePath { get; }
+
+ ///
+ /// 获取或设置资源包名称。
+ ///
+ string PackageName { get; set; }
+
+ ///
+ /// 获取或设置运行模式。
+ ///
+ EPlayMode PlayMode { get; set; }
+
+ ///
+ /// 获取或设置下载文件校验等级。
+ ///
+ EVerifyLevel VerifyLevel { get; set; }
+
+ ///
+ /// 获取或设置异步系统参数,每帧执行消耗的最大时间切片(单位:毫秒)。
+ ///
+ long Milliseconds { get; set; }
+
+ ///
+ /// 设置资源只读区路径。
+ ///
+ /// 资源只读区路径。
+ void SetReadOnlyPath(string readOnlyPath);
+
+ ///
+ /// 设置资源读写区路径。
+ ///
+ /// 资源读写区路径。
+ void SetReadWritePath(string readWritePath);
+
+ ///
+ /// 初始化接口。
+ ///
+ void Initialize();
+
+ ///
+ /// 初始化操作。
+ ///
+ ///
+ InitializationOperation InitPackage();
+
+ ///
+ /// 卸载资源。
+ ///
+ /// 要卸载的资源。
+ void UnloadAsset(object asset);
+
+ ///
+ /// 资源回收(卸载引用计数为零的资源)
+ ///
+ void UnloadUnusedAssets();
+
+ ///
+ /// 强制回收所有资源
+ ///
+ void ForceUnloadAllAssets();
+
+ ///
+ /// 检查资源是否存在。
+ ///
+ /// 要检查资源的名称。
+ /// 检查资源是否存在的结果。
+ HasAssetResult HasAsset(string assetName);
+
+ ///
+ /// 同步加载资源。
+ ///
+ /// 要加载资源的名称。
+ /// 要加载资源的类型。
+ /// 资源实例。
+ T LoadAsset(string assetName) where T : UnityEngine.Object;
+
+ ///
+ /// 同步加载资源。
+ ///
+ /// 要加载资源的名称。
+ /// 父节点位置。
+ /// 要加载资源的类型。
+ /// 资源实例。
+ T LoadAsset(string assetName, Transform parent) where T : UnityEngine.Object;
+
+ ///
+ /// 异步加载资源。
+ ///
+ /// 要加载资源的名称。
+ /// 取消操作Token。
+ /// 要加载资源的类型。
+ /// 异步资源实例。
+ UniTask LoadAssetAsync(string assetName,CancellationToken cancellationToken) where T : UnityEngine.Object;
+
+ ///
+ /// 异步加载游戏物体。
+ ///
+ /// 要加载的游戏物体名称。
+ /// 取消操作Token。
+ /// 异步游戏物体实例。
+ UniTask LoadGameObjectAsync(string assetName,CancellationToken cancellationToken);
+
+ ///
+ /// 同步加载资源并获取句柄。
+ ///
+ /// 要加载资源的名称。
+ /// 要加载资源的类型。
+ /// 同步加载资源句柄。
+ AssetOperationHandle LoadAssetGetOperation(string assetName) where T : UnityEngine.Object;
+
+ ///
+ /// 异步加载资源并获取句柄。
+ ///
+ /// 要加载资源的名称。
+ /// 要加载资源的类型。
+ /// 同步加载资源句柄。
+ AssetOperationHandle LoadAssetAsyncHandle(string assetName) where T : UnityEngine.Object;
+ }
+}
\ No newline at end of file
diff --git a/Assets/TEngine/Runtime/Resource/IResourceManager.cs.meta b/Assets/TEngine/Runtime/Resource/IResourceManager.cs.meta
new file mode 100644
index 00000000..4bdcb12d
--- /dev/null
+++ b/Assets/TEngine/Runtime/Resource/IResourceManager.cs.meta
@@ -0,0 +1,3 @@
+fileFormatVersion: 2
+guid: 4b0d8823b6744ba5b2ed48ffcce56e54
+timeCreated: 1680342298
\ No newline at end of file
diff --git a/Assets/TEngine/Runtime/Resource/Operation.meta b/Assets/TEngine/Runtime/Resource/Operation.meta
new file mode 100644
index 00000000..ef3cbae2
--- /dev/null
+++ b/Assets/TEngine/Runtime/Resource/Operation.meta
@@ -0,0 +1,3 @@
+fileFormatVersion: 2
+guid: 1d3946c878f8452385e7127eb12bd5a4
+timeCreated: 1680342235
\ No newline at end of file
diff --git a/Assets/TEngine/Runtime/Resource/Operation/LoadAssetsByTagOperation.cs b/Assets/TEngine/Runtime/Resource/Operation/LoadAssetsByTagOperation.cs
new file mode 100644
index 00000000..07aa15a7
--- /dev/null
+++ b/Assets/TEngine/Runtime/Resource/Operation/LoadAssetsByTagOperation.cs
@@ -0,0 +1,120 @@
+using System.Collections.Generic;
+using YooAsset;
+
+namespace TEngine
+{
+ public class LoadAssetsByTagOperation : GameAsyncOperation where TObject : UnityEngine.Object
+ {
+ private enum ESteps
+ {
+ None,
+ LoadAssets,
+ CheckResult,
+ Done,
+ }
+
+ private readonly string _tag;
+ private ESteps _steps = ESteps.None;
+ private List _handles;
+
+ ///
+ /// 资源对象集合
+ ///
+ public List 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(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(_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;
+ }
+
+ ///
+ /// 释放资源句柄
+ ///
+ public void ReleaseHandle()
+ {
+ foreach (var handle in _handles)
+ {
+ handle.Release();
+ }
+
+ _handles.Clear();
+ }
+ }
+}
\ No newline at end of file
diff --git a/Assets/TEngine/Runtime/Resource/Operation/LoadAssetsByTagOperation.cs.meta b/Assets/TEngine/Runtime/Resource/Operation/LoadAssetsByTagOperation.cs.meta
new file mode 100644
index 00000000..a895d855
--- /dev/null
+++ b/Assets/TEngine/Runtime/Resource/Operation/LoadAssetsByTagOperation.cs.meta
@@ -0,0 +1,3 @@
+fileFormatVersion: 2
+guid: cd96a13f5f98496ba608a667fb7d9144
+timeCreated: 1680342243
\ No newline at end of file
diff --git a/Assets/TEngine/Runtime/Resource/ReadWritePathType.cs b/Assets/TEngine/Runtime/Resource/ReadWritePathType.cs
new file mode 100644
index 00000000..93d49ee9
--- /dev/null
+++ b/Assets/TEngine/Runtime/Resource/ReadWritePathType.cs
@@ -0,0 +1,23 @@
+namespace TEngine
+{
+ ///
+ /// 读写区路径类型。
+ ///
+ public enum ReadWritePathType : byte
+ {
+ ///
+ /// 未指定。
+ ///
+ Unspecified = 0,
+
+ ///
+ /// 临时缓存。
+ ///
+ TemporaryCache,
+
+ ///
+ /// 持久化数据。
+ ///
+ PersistentData,
+ }
+}
diff --git a/Assets/TEngine/Runtime/Resource/ReadWritePathType.cs.meta b/Assets/TEngine/Runtime/Resource/ReadWritePathType.cs.meta
new file mode 100644
index 00000000..0662e868
--- /dev/null
+++ b/Assets/TEngine/Runtime/Resource/ReadWritePathType.cs.meta
@@ -0,0 +1,3 @@
+fileFormatVersion: 2
+guid: 458eb2d0dde14960adf21204432a5979
+timeCreated: 1680342271
\ No newline at end of file
diff --git a/Assets/TEngine/Runtime/Resource/ResourceManager.cs b/Assets/TEngine/Runtime/Resource/ResourceManager.cs
new file mode 100644
index 00000000..3c8e951e
--- /dev/null
+++ b/Assets/TEngine/Runtime/Resource/ResourceManager.cs
@@ -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(string assetName) where T : Object
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public T LoadAsset(string assetName, Transform parent) where T : Object
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public UniTask LoadAssetAsync(string assetName, CancellationToken cancellationToken) where T : Object
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public UniTask LoadGameObjectAsync(string assetName, CancellationToken cancellationToken)
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public AssetOperationHandle LoadAssetGetOperation(string assetName) where T : Object
+ {
+ throw new System.NotImplementedException();
+ }
+
+ public AssetOperationHandle LoadAssetAsyncHandle(string assetName) where T : Object
+ {
+ throw new System.NotImplementedException();
+ }
+ }
+}
\ No newline at end of file
diff --git a/Assets/TEngine/Runtime/Resource/ResourceManager.cs.meta b/Assets/TEngine/Runtime/Resource/ResourceManager.cs.meta
new file mode 100644
index 00000000..cb48600b
--- /dev/null
+++ b/Assets/TEngine/Runtime/Resource/ResourceManager.cs.meta
@@ -0,0 +1,3 @@
+fileFormatVersion: 2
+guid: 56829b699b8c4fcda57e0df5e9516e49
+timeCreated: 1680361920
\ No newline at end of file