update asset system

新增了初始化参数LoadingMaxTimeSlice
移除了参数AssetLoadingMaxNumber
pull/86/head
hevinci 2023-03-24 17:01:35 +08:00
parent 82c83fcdf7
commit c60cc1e84f
3 changed files with 31 additions and 26 deletions

View File

@ -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;
}
}
/// <summary>
/// 初始化
/// 注意在使用AssetSystem之前需要初始化
/// </summary>
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();
}
/// <summary>
@ -46,6 +59,8 @@ namespace YooAsset
/// </summary>
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;
}

View File

@ -39,13 +39,14 @@ namespace YooAsset
public IDecryptionServices DecryptionServices = null;
/// <summary>
/// 资源加载的最大数量
/// 资源加载每帧处理的最大时间片段
/// 注意默认值为MaxValue
/// </summary>
public int AssetLoadingMaxNumber = int.MaxValue;
public long LoadingMaxTimeSlice = long.MaxValue;
/// <summary>
/// 下载失败尝试次数
/// 注意默认值为MaxValue
/// </summary>
public int DownloadFailedTryAgain = int.MaxValue;
}

View File

@ -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)
{