From 1ab7689174b1fc00b3c77a1bf9f02465798138c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BD=95=E5=86=A0=E5=B3=B0?= Date: Tue, 1 Apr 2025 11:44:10 +0800 Subject: [PATCH] fix #524 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新增初始化参数 BundleLoadingMaxConcurrency --- .../YooAsset/Runtime/InitializeParameters.cs | 4 +++ .../Internal/LoadBundleFileOperation.cs | 25 ++++++++++++++++--- .../ResourceManager/ResourceManager.cs | 13 +++++++++- .../ResourcePackage/ResourcePackage.cs | 6 ++++- 4 files changed, 43 insertions(+), 5 deletions(-) diff --git a/Assets/YooAsset/Runtime/InitializeParameters.cs b/Assets/YooAsset/Runtime/InitializeParameters.cs index a2698bca..5818df7f 100644 --- a/Assets/YooAsset/Runtime/InitializeParameters.cs +++ b/Assets/YooAsset/Runtime/InitializeParameters.cs @@ -38,6 +38,10 @@ namespace YooAsset /// public abstract class InitializeParameters { + /// + /// 同时加载Bundle文件的最大并发数 + /// + public int BundleLoadingMaxConcurrency = int.MaxValue; } /// diff --git a/Assets/YooAsset/Runtime/ResourceManager/Operation/Internal/LoadBundleFileOperation.cs b/Assets/YooAsset/Runtime/ResourceManager/Operation/Internal/LoadBundleFileOperation.cs index e27a3ff1..d5837ade 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/Operation/Internal/LoadBundleFileOperation.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/Operation/Internal/LoadBundleFileOperation.cs @@ -9,7 +9,8 @@ namespace YooAsset private enum ESteps { None, - LoadFile, + CheckConcurrency, + LoadBundleFile, Done, } @@ -57,17 +58,32 @@ namespace YooAsset } internal override void InternalStart() { - _steps = ESteps.LoadFile; + _steps = ESteps.CheckConcurrency; } internal override void InternalUpdate() { if (_steps == ESteps.None || _steps == ESteps.Done) return; - if (_steps == ESteps.LoadFile) + if (_steps == ESteps.CheckConcurrency) + { + if (IsWaitForAsyncComplete) + { + _steps = ESteps.LoadBundleFile; + } + else + { + if (_resourceManager.BundleLoadingIsBusy()) + return; + _steps = ESteps.LoadBundleFile; + } + } + + if (_steps == ESteps.LoadBundleFile) { if (_loadBundleOp == null) { + _resourceManager.BundleLoadingCounter++; _loadBundleOp = LoadBundleInfo.LoadBundleFile(); _loadBundleOp.StartOperation(); AddChildOperation(_loadBundleOp); @@ -103,6 +119,9 @@ namespace YooAsset Status = EOperationStatus.Failed; Error = _loadBundleOp.Error; } + + // 统计计数减少 + _resourceManager.BundleLoadingCounter--; } } internal override void InternalWaitForAsyncComplete() diff --git a/Assets/YooAsset/Runtime/ResourceManager/ResourceManager.cs b/Assets/YooAsset/Runtime/ResourceManager/ResourceManager.cs index 03bf2a10..bf386dcd 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/ResourceManager.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/ResourceManager.cs @@ -14,6 +14,7 @@ namespace YooAsset internal readonly List SceneHandles = new List(100); private long _sceneCreateIndex = 0; private IBundleQuery _bundleQuery; + private int _bundleLoadingMaxConcurrency; /// /// 所属包裹 @@ -25,6 +26,11 @@ namespace YooAsset /// public bool LockLoadOperation = false; + /// + /// 统计正在加载的Bundle文件数量 + /// + public int BundleLoadingCounter = 0; + public ResourceManager(string packageName) { @@ -34,8 +40,9 @@ namespace YooAsset /// /// 初始化 /// - public void Initialize(IBundleQuery bundleServices) + public void Initialize(InitializeParameters parameters, IBundleQuery bundleServices) { + _bundleLoadingMaxConcurrency = parameters.BundleLoadingMaxConcurrency; _bundleQuery = bundleServices; SceneManager.sceneUnloaded += OnSceneUnloaded; } @@ -310,6 +317,10 @@ namespace YooAsset { return LoaderDic.Count > 0; } + internal bool BundleLoadingIsBusy() + { + return BundleLoadingCounter >= _bundleLoadingMaxConcurrency; + } private LoadBundleFileOperation CreateBundleFileLoaderInternal(BundleInfo bundleInfo) { diff --git a/Assets/YooAsset/Runtime/ResourcePackage/ResourcePackage.cs b/Assets/YooAsset/Runtime/ResourcePackage/ResourcePackage.cs index 9e62d68c..d30511c8 100644 --- a/Assets/YooAsset/Runtime/ResourcePackage/ResourcePackage.cs +++ b/Assets/YooAsset/Runtime/ResourcePackage/ResourcePackage.cs @@ -100,7 +100,7 @@ namespace YooAsset var playModeImpl = new PlayModeImpl(PackageName, _playMode); _bundleQuery = playModeImpl; _playModeImpl = playModeImpl; - _resourceManager.Initialize(_bundleQuery); + _resourceManager.Initialize(parameters, _bundleQuery); // 初始化资源系统 InitializationOperation initializeOperation; @@ -162,6 +162,10 @@ namespace YooAsset throw new Exception($"Editor simulate mode only support unity editor."); #endif + // 检测初始化参数 + if (parameters.BundleLoadingMaxConcurrency <= 0) + throw new Exception($"{nameof(parameters.BundleLoadingMaxConcurrency)} value must be greater than zero."); + // 鉴定运行模式 if (parameters is EditorSimulateModeParameters) _playMode = EPlayMode.EditorSimulateMode;