From e3228d406e1c7a8165913bc6a4e8deb59da10574 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BD=95=E5=86=A0=E5=B3=B0?= Date: Sat, 8 Mar 2025 10:58:19 +0800 Subject: [PATCH] update resource manager MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 优化Handle生成和移除逻辑 --- .../ResourceManager/Handle/HandleFactory.cs | 26 ++++++++++++++++++ .../Handle/HandleFactory.cs.meta | 11 ++++++++ .../Provider/ProviderOperation.cs | 27 +++++-------------- 3 files changed, 44 insertions(+), 20 deletions(-) create mode 100644 Assets/YooAsset/Runtime/ResourceManager/Handle/HandleFactory.cs create mode 100644 Assets/YooAsset/Runtime/ResourceManager/Handle/HandleFactory.cs.meta diff --git a/Assets/YooAsset/Runtime/ResourceManager/Handle/HandleFactory.cs b/Assets/YooAsset/Runtime/ResourceManager/Handle/HandleFactory.cs new file mode 100644 index 00000000..2173f888 --- /dev/null +++ b/Assets/YooAsset/Runtime/ResourceManager/Handle/HandleFactory.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; + +namespace YooAsset +{ + internal static class HandleFactory + { + private static readonly Dictionary> _handleFactory = new() + { + { typeof(AssetHandle), op => new AssetHandle(op) }, + { typeof(SceneHandle), op => new SceneHandle(op) }, + { typeof(SubAssetsHandle), op => new SubAssetsHandle(op) }, + { typeof(AllAssetsHandle), op => new AllAssetsHandle(op) }, + { typeof(RawFileHandle), op => new RawFileHandle(op) } + }; + + public static HandleBase CreateHandle(ProviderOperation operation, Type type) + { + if (_handleFactory.TryGetValue(type, out var factory) == false) + { + throw new NotImplementedException($"Handle type {type.FullName} is not supported."); + } + return factory(operation); + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/ResourceManager/Handle/HandleFactory.cs.meta b/Assets/YooAsset/Runtime/ResourceManager/Handle/HandleFactory.cs.meta new file mode 100644 index 00000000..1deec4f5 --- /dev/null +++ b/Assets/YooAsset/Runtime/ResourceManager/Handle/HandleFactory.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 4d6ef91e069948c48b7ca60be4c218ee +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Runtime/ResourceManager/Provider/ProviderOperation.cs b/Assets/YooAsset/Runtime/ResourceManager/Provider/ProviderOperation.cs index e0cf31ef..ee81fb6f 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/Provider/ProviderOperation.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/Provider/ProviderOperation.cs @@ -1,7 +1,7 @@ using System.Collections; using System.Collections.Generic; using System.Diagnostics; -using System; +using System.Linq; namespace YooAsset { @@ -69,8 +69,8 @@ namespace YooAsset private ESteps _steps = ESteps.None; private readonly LoadBundleFileOperation _mainBundleLoader; - private readonly List _bundleLoaders = new List(); - private readonly List _handles = new List(); + private readonly List _bundleLoaders = new List(10); + private readonly HashSet _handles = new HashSet(); public ProviderOperation(ResourceManager manager, string providerGUID, AssetInfo assetInfo) @@ -220,20 +220,7 @@ namespace YooAsset // 引用计数增加 RefCount++; - HandleBase handle; - if (typeof(T) == typeof(AssetHandle)) - handle = new AssetHandle(this); - else if (typeof(T) == typeof(SceneHandle)) - handle = new SceneHandle(this); - else if (typeof(T) == typeof(SubAssetsHandle)) - handle = new SubAssetsHandle(this); - else if (typeof(T) == typeof(AllAssetsHandle)) - handle = new AllAssetsHandle(this); - else if (typeof(T) == typeof(RawFileHandle)) - handle = new RawFileHandle(this); - else - throw new System.NotImplementedException(); - + HandleBase handle = HandleFactory.CreateHandle(this, typeof(T)); _handles.Add(handle); return handle as T; } @@ -258,9 +245,9 @@ namespace YooAsset /// public void ReleaseAllHandles() { - for (int i = _handles.Count - 1; i >= 0; i--) + List tempers = _handles.ToList(); + foreach (var handle in tempers) { - var handle = _handles[i]; handle.Release(); } } @@ -276,7 +263,7 @@ namespace YooAsset // 注意:创建临时列表是为了防止外部逻辑在回调函数内创建或者释放资源句柄。 // 注意:回调方法如果发生异常,会阻断列表里的后续回调方法! - List tempers = new List(_handles); + List tempers = _handles.ToList(); foreach (var hande in tempers) { if (hande.IsValid)