diff --git a/Assets/YooAsset/Runtime/AssetSystem/AssetSystem.cs b/Assets/YooAsset/Runtime/AssetSystem/AssetSystem.cs index f003613..0c9bfe0 100644 --- a/Assets/YooAsset/Runtime/AssetSystem/AssetSystem.cs +++ b/Assets/YooAsset/Runtime/AssetSystem/AssetSystem.cs @@ -1,6 +1,7 @@ using System; using System.Collections; using System.Collections.Generic; +using System.Diagnostics; using UnityEngine; using UnityEngine.SceneManagement; @@ -20,25 +21,37 @@ namespace YooAsset private bool _isUnloadSafe = true; private string _packageName; private bool _simulationOnEditor; - public int LoadingMaxNumber { private set; get; } + private long _loadingMaxTimeSlice; public int DownloadFailedTryAgain { private set; get; } public IDecryptionServices DecryptionServices { private set; get; } public IBundleServices BundleServices { private set; get; } + // 计时器相关 + private Stopwatch _watch; + private long _frameTime; + private bool IsBusy + { + get + { + return _watch.ElapsedMilliseconds - _frameTime >= _loadingMaxTimeSlice; + } + } + /// /// 初始化 /// 注意:在使用AssetSystem之前需要初始化 /// - public void Initialize(string packageName, bool simulationOnEditor, int loadingMaxNumber, int downloadFailedTryAgain, + public void Initialize(string packageName, bool simulationOnEditor, long loadingMaxTimeSlice, int downloadFailedTryAgain, IDecryptionServices decryptionServices, IBundleServices bundleServices) { _packageName = packageName; _simulationOnEditor = simulationOnEditor; - LoadingMaxNumber = loadingMaxNumber; + _loadingMaxTimeSlice = loadingMaxTimeSlice; DownloadFailedTryAgain = downloadFailedTryAgain; DecryptionServices = decryptionServices; BundleServices = bundleServices; + _watch = Stopwatch.StartNew(); } /// @@ -46,6 +59,8 @@ namespace YooAsset /// public void Update() { + _frameTime = _watch.ElapsedMilliseconds; + // 更新加载器 foreach (var loader in _loaderList) { @@ -56,22 +71,11 @@ namespace YooAsset // 注意:循环更新的时候,可能会扩展列表 // 注意:不能限制场景对象的加载 _isUnloadSafe = false; - int loadingCount = 0; for (int i = 0; i < _providerList.Count; i++) { - var provider = _providerList[i]; - if (provider.IsSceneProvider()) - { - provider.Update(); - } - else - { - if (loadingCount < LoadingMaxNumber) - provider.Update(); - - if (provider.IsDone == false) - loadingCount++; - } + if (IsBusy) + break; + _providerList[i].Update(); } _isUnloadSafe = true; } diff --git a/Assets/YooAsset/Runtime/InitializeParameters.cs b/Assets/YooAsset/Runtime/InitializeParameters.cs index 077b78d..a6e157e 100644 --- a/Assets/YooAsset/Runtime/InitializeParameters.cs +++ b/Assets/YooAsset/Runtime/InitializeParameters.cs @@ -39,13 +39,14 @@ namespace YooAsset public IDecryptionServices DecryptionServices = null; /// - /// 资源加载的最大数量 + /// 资源加载每帧处理的最大时间片段 /// 注意:默认值为MaxValue /// - public int AssetLoadingMaxNumber = int.MaxValue; - + public long LoadingMaxTimeSlice = long.MaxValue; + /// /// 下载失败尝试次数 + /// 注意:默认值为MaxValue /// public int DownloadFailedTryAgain = int.MaxValue; } diff --git a/Assets/YooAsset/Runtime/ResourcePackage.cs b/Assets/YooAsset/Runtime/ResourcePackage.cs index 879bdad..3fd3df1 100644 --- a/Assets/YooAsset/Runtime/ResourcePackage.cs +++ b/Assets/YooAsset/Runtime/ResourcePackage.cs @@ -88,7 +88,7 @@ namespace YooAsset _bundleServices = editorSimulateModeImpl; _playModeServices = editorSimulateModeImpl; _assetSystemImpl.Initialize(PackageName, true, - parameters.AssetLoadingMaxNumber, parameters.DownloadFailedTryAgain, + parameters.LoadingMaxTimeSlice, parameters.DownloadFailedTryAgain, parameters.DecryptionServices, _bundleServices); var initializeParameters = parameters as EditorSimulateModeParameters; @@ -100,7 +100,7 @@ namespace YooAsset _bundleServices = offlinePlayModeImpl; _playModeServices = offlinePlayModeImpl; _assetSystemImpl.Initialize(PackageName, false, - parameters.AssetLoadingMaxNumber, parameters.DownloadFailedTryAgain, + parameters.LoadingMaxTimeSlice, parameters.DownloadFailedTryAgain, parameters.DecryptionServices, _bundleServices); var initializeParameters = parameters as OfflinePlayModeParameters; @@ -112,7 +112,7 @@ namespace YooAsset _bundleServices = hostPlayModeImpl; _playModeServices = hostPlayModeImpl; _assetSystemImpl.Initialize(PackageName, false, - parameters.AssetLoadingMaxNumber, parameters.DownloadFailedTryAgain, + parameters.LoadingMaxTimeSlice, parameters.DownloadFailedTryAgain, parameters.DecryptionServices, _bundleServices); var initializeParameters = parameters as HostPlayModeParameters; @@ -188,10 +188,10 @@ namespace YooAsset throw new NotImplementedException(); // 检测参数范围 - if (parameters.AssetLoadingMaxNumber < 1) + if (parameters.LoadingMaxTimeSlice < 10) { - parameters.AssetLoadingMaxNumber = 1; - YooLogger.Warning($"{nameof(parameters.AssetLoadingMaxNumber)} minimum value is 1"); + parameters.LoadingMaxTimeSlice = 10; + YooLogger.Warning($"{nameof(parameters.LoadingMaxTimeSlice)} minimum value is 10 milliseconds."); } if (parameters.DownloadFailedTryAgain < 1) {