update operation system

异步操作增加执行优先级
pull/229/head
hevinci 2023-10-27 16:08:38 +08:00
parent 9418544264
commit bc9a4e07e2
20 changed files with 391 additions and 429 deletions

View File

@ -224,7 +224,7 @@ namespace YooAsset.Editor
// Status
StyleColor textColor;
if (providerInfo.Status == ProviderBase.EStatus.Failed.ToString())
if (providerInfo.Status == EOperationStatus.Failed.ToString())
textColor = new StyleColor(Color.yellow);
else
textColor = label1.style.color;

View File

@ -80,12 +80,6 @@ namespace YooAsset
/// </summary>
public bool AutoDestroyAssetProvider = false;
/// <summary>
/// 资源加载每帧处理的最大时间片段
/// 注意默认值为MaxValue
/// </summary>
public long LoadingMaxTimeSlice = long.MaxValue;
/// <summary>
/// 启用断点续传参数
/// 说明:当文件的大小大于设置的字节数时启用断点续传下载器

View File

@ -5,16 +5,24 @@ using System.Threading.Tasks;
namespace YooAsset
{
public abstract class AsyncOperationBase : IEnumerator
public abstract class AsyncOperationBase : IEnumerator, IComparable<AsyncOperationBase>
{
// 用户请求的回调
private Action<AsyncOperationBase> _callback;
// 是否已经完成
internal bool IsFinish = false;
/// <summary>
/// 所属包裹
/// </summary>
public string PackageName { private set; get; }
/// <summary>
/// 优先级
/// </summary>
public uint Priority { set; get; } = 0;
/// <summary>
/// 状态
/// </summary>
@ -80,7 +88,7 @@ namespace YooAsset
internal abstract void InternalOnUpdate();
internal virtual void InternalOnAbort() { }
internal void SetPackageName(string packageName)
internal void Init(string packageName)
{
PackageName = packageName;
}
@ -91,8 +99,14 @@ namespace YooAsset
}
internal void SetFinish()
{
IsFinish = true;
// 进度百分百完成
Progress = 1f;
_callback?.Invoke(this); //注意如果完成回调内发生异常会导致Task无限期等待
//注意如果完成回调内发生异常会导致Task无限期等待
_callback?.Invoke(this);
if (_taskCompletionSource != null)
_taskCompletionSource.TrySetResult(null);
}
@ -115,6 +129,13 @@ namespace YooAsset
_callback = null;
}
#region 排序接口实现
public int CompareTo(AsyncOperationBase other)
{
return other.Priority.CompareTo(this.Priority);
}
#endregion
#region 异步编程相关
bool IEnumerator.MoveNext()
{

View File

@ -45,29 +45,50 @@ namespace YooAsset
{
_frameTime = _watch.ElapsedMilliseconds;
// 添加新的异步操作
// 添加新的异步操作
if (_newList.Count > 0)
{
_operations.AddRange(_newList);
_newList.Clear();
bool sorting = false;
foreach (var operation in _newList)
{
if (operation.Priority > 0)
{
sorting = true;
break;
}
}
// 更新所有的异步操作
for (int i = _operations.Count - 1; i >= 0; i--)
_operations.AddRange(_newList);
_newList.Clear();
// 重新排序优先级
if (sorting)
_operations.Sort();
}
// 更新进行中的异步操作
for (int i = 0; i < _operations.Count; i++)
{
if (IsBusy)
break;
var operation = _operations[i];
if (operation.IsFinish)
continue;
if (operation.IsDone == false)
operation.InternalOnUpdate();
if (operation.IsDone)
{
// 注意:如果业务端发生异常,保证异步操作提前移除。
_operations.RemoveAt(i);
operation.SetFinish();
}
// 移除已经完成的异步操作
for (int i = _operations.Count - 1; i >= 0; i--)
{
var operation = _operations[i];
if (operation.IsFinish)
_operations.RemoveAt(i);
}
}
@ -113,7 +134,7 @@ namespace YooAsset
public static void StartOperation(string packageName, AsyncOperationBase operation)
{
_newList.Add(operation);
operation.SetPackageName(packageName);
operation.Init(packageName);
operation.SetStart();
}
}

View File

@ -45,15 +45,7 @@ namespace YooAsset
if (IsValidWithWarning == false)
return EOperationStatus.None;
var status = Provider.Status;
if (status == ProviderBase.EStatus.None)
return EOperationStatus.None;
else if (status == ProviderBase.EStatus.Succeed)
return EOperationStatus.Succeed;
else if (status == ProviderBase.EStatus.Failed)
return EOperationStatus.Failed;
else
return EOperationStatus.Processing;
return Provider.Status;
}
}
@ -66,7 +58,7 @@ namespace YooAsset
{
if (IsValidWithWarning == false)
return string.Empty;
return Provider.LastError;
return Provider.Error;
}
}

View File

@ -110,12 +110,6 @@ namespace YooAsset
/// </summary>
public void TryDestroyProviders()
{
// TODO 不用等待资源包加载完成
/*
if (IsDone() == false)
return;
*/
// 获取移除列表
_removeList.Clear();
foreach (var provider in _providers)

View File

@ -8,23 +8,25 @@ namespace YooAsset
{
private AssetBundleRequest _cacheRequest;
public BundledAllAssetsProvider(ResourceManager manager, string providerGUID, uint providerPriority, AssetInfo assetInfo) : base(manager, providerGUID, providerPriority, assetInfo)
public BundledAllAssetsProvider(ResourceManager manager, string providerGUID, AssetInfo assetInfo) : base(manager, providerGUID, assetInfo)
{
}
public override void Update()
internal override void InternalOnStart()
{
DebugBeginRecording();
}
internal override void InternalOnUpdate()
{
if (IsDone)
return;
if (Status == EStatus.None)
if (_steps == ESteps.None)
{
Status = EStatus.CheckBundle;
_steps = ESteps.CheckBundle;
}
// 1. 检测资源包
if (Status == EStatus.CheckBundle)
if (_steps == ESteps.CheckBundle)
{
if (IsWaitForAsyncComplete)
{
@ -39,17 +41,15 @@ namespace YooAsset
if (DependBundles.IsSucceed() == false)
{
Status = EStatus.Failed;
LastError = DependBundles.GetLastError();
InvokeCompletion();
string error = DependBundles.GetLastError();
InvokeCompletion(error, EOperationStatus.Failed);
return;
}
if (OwnerBundle.Status != BundleLoaderBase.EStatus.Succeed)
{
Status = EStatus.Failed;
LastError = OwnerBundle.LastError;
InvokeCompletion();
string error = OwnerBundle.LastError;
InvokeCompletion(error, EOperationStatus.Failed);
return;
}
@ -59,11 +59,11 @@ namespace YooAsset
return;
}
Status = EStatus.Loading;
_steps = ESteps.Loading;
}
// 2. 加载资源对象
if (Status == EStatus.Loading)
if (_steps == ESteps.Loading)
{
if (IsWaitForAsyncComplete || IsForceDestroyComplete)
{
@ -79,11 +79,11 @@ namespace YooAsset
else
_cacheRequest = OwnerBundle.CacheBundle.LoadAllAssetsAsync(MainAssetInfo.AssetType);
}
Status = EStatus.Checking;
_steps = ESteps.Checking;
}
// 3. 检测加载结果
if (Status == EStatus.Checking)
if (_steps == ESteps.Checking)
{
if (_cacheRequest != null)
{
@ -102,16 +102,20 @@ namespace YooAsset
}
}
Status = AllAssetObjects == null ? EStatus.Failed : EStatus.Succeed;
if (Status == EStatus.Failed)
if (AllAssetObjects == null)
{
string error;
if (MainAssetInfo.AssetType == null)
LastError = $"Failed to load all assets : {MainAssetInfo.AssetPath} AssetType : null AssetBundle : {OwnerBundle.MainBundleInfo.Bundle.BundleName}";
error = $"Failed to load all assets : {MainAssetInfo.AssetPath} AssetType : null AssetBundle : {OwnerBundle.MainBundleInfo.Bundle.BundleName}";
else
LastError = $"Failed to load all assets : {MainAssetInfo.AssetPath} AssetType : {MainAssetInfo.AssetType} AssetBundle : {OwnerBundle.MainBundleInfo.Bundle.BundleName}";
YooLogger.Error(LastError);
error = $"Failed to load all assets : {MainAssetInfo.AssetPath} AssetType : {MainAssetInfo.AssetType} AssetBundle : {OwnerBundle.MainBundleInfo.Bundle.BundleName}";
YooLogger.Error(error);
InvokeCompletion(error, EOperationStatus.Failed);
}
else
{
InvokeCompletion(string.Empty, EOperationStatus.Succeed);
}
InvokeCompletion();
}
}
}

View File

@ -8,23 +8,25 @@ namespace YooAsset
{
private AssetBundleRequest _cacheRequest;
public BundledAssetProvider(ResourceManager manager, string providerGUID, uint providerPriority, AssetInfo assetInfo) : base(manager, providerGUID, providerPriority, assetInfo)
public BundledAssetProvider(ResourceManager manager, string providerGUID, AssetInfo assetInfo) : base(manager, providerGUID, assetInfo)
{
}
public override void Update()
internal override void InternalOnStart()
{
DebugBeginRecording();
}
internal override void InternalOnUpdate()
{
if (IsDone)
return;
if (Status == EStatus.None)
if (_steps == ESteps.None)
{
Status = EStatus.CheckBundle;
_steps = ESteps.CheckBundle;
}
// 1. 检测资源包
if (Status == EStatus.CheckBundle)
if (_steps == ESteps.CheckBundle)
{
if (IsWaitForAsyncComplete)
{
@ -39,17 +41,15 @@ namespace YooAsset
if (DependBundles.IsSucceed() == false)
{
Status = EStatus.Failed;
LastError = DependBundles.GetLastError();
InvokeCompletion();
string error = DependBundles.GetLastError();
InvokeCompletion(error, EOperationStatus.Failed);
return;
}
if (OwnerBundle.Status != BundleLoaderBase.EStatus.Succeed)
{
Status = EStatus.Failed;
LastError = OwnerBundle.LastError;
InvokeCompletion();
string error = OwnerBundle.LastError;
InvokeCompletion(error, EOperationStatus.Failed);
return;
}
@ -59,11 +59,11 @@ namespace YooAsset
return;
}
Status = EStatus.Loading;
_steps = ESteps.Loading;
}
// 2. 加载资源对象
if (Status == EStatus.Loading)
if (_steps == ESteps.Loading)
{
if (IsWaitForAsyncComplete || IsForceDestroyComplete)
{
@ -79,11 +79,11 @@ namespace YooAsset
else
_cacheRequest = OwnerBundle.CacheBundle.LoadAssetAsync(MainAssetInfo.AssetPath, MainAssetInfo.AssetType);
}
Status = EStatus.Checking;
_steps = ESteps.Checking;
}
// 3. 检测加载结果
if (Status == EStatus.Checking)
if (_steps == ESteps.Checking)
{
if (_cacheRequest != null)
{
@ -102,16 +102,20 @@ namespace YooAsset
}
}
Status = AssetObject == null ? EStatus.Failed : EStatus.Succeed;
if (Status == EStatus.Failed)
if (AssetObject == null)
{
string error;
if (MainAssetInfo.AssetType == null)
LastError = $"Failed to load asset : {MainAssetInfo.AssetPath} AssetType : null AssetBundle : {OwnerBundle.MainBundleInfo.Bundle.BundleName}";
error = $"Failed to load asset : {MainAssetInfo.AssetPath} AssetType : null AssetBundle : {OwnerBundle.MainBundleInfo.Bundle.BundleName}";
else
LastError = $"Failed to load asset : {MainAssetInfo.AssetPath} AssetType : {MainAssetInfo.AssetType} AssetBundle : {OwnerBundle.MainBundleInfo.Bundle.BundleName}";
YooLogger.Error(LastError);
error = $"Failed to load asset : {MainAssetInfo.AssetPath} AssetType : {MainAssetInfo.AssetType} AssetBundle : {OwnerBundle.MainBundleInfo.Bundle.BundleName}";
YooLogger.Error(error);
InvokeCompletion(error, EOperationStatus.Failed);
}
else
{
InvokeCompletion(string.Empty, EOperationStatus.Succeed);
}
InvokeCompletion();
}
}
}

View File

@ -3,23 +3,25 @@ namespace YooAsset
{
internal class BundledRawFileProvider : ProviderBase
{
public BundledRawFileProvider(ResourceManager manager, string providerGUID, uint providerPriority, AssetInfo assetInfo) : base(manager, providerGUID, providerPriority, assetInfo)
public BundledRawFileProvider(ResourceManager manager, string providerGUID, AssetInfo assetInfo) : base(manager, providerGUID, assetInfo)
{
}
public override void Update()
internal override void InternalOnStart()
{
DebugBeginRecording();
}
internal override void InternalOnUpdate()
{
if (IsDone)
return;
if (Status == EStatus.None)
if (_steps == ESteps.None)
{
Status = EStatus.CheckBundle;
_steps = ESteps.CheckBundle;
}
// 1. 检测资源包
if (Status == EStatus.CheckBundle)
if (_steps == ESteps.CheckBundle)
{
if (IsWaitForAsyncComplete)
{
@ -31,21 +33,19 @@ namespace YooAsset
if (OwnerBundle.Status != BundleLoaderBase.EStatus.Succeed)
{
Status = EStatus.Failed;
LastError = OwnerBundle.LastError;
InvokeCompletion();
string error = OwnerBundle.LastError;
InvokeCompletion(error, EOperationStatus.Failed);
return;
}
Status = EStatus.Checking;
_steps = ESteps.Checking;
}
// 2. 检测加载结果
if (Status == EStatus.Checking)
if (_steps == ESteps.Checking)
{
RawFilePath = OwnerBundle.FileLoadPath;
Status = EStatus.Succeed;
InvokeCompletion();
InvokeCompletion(string.Empty, EOperationStatus.Succeed);
}
}
}

View File

@ -12,26 +12,28 @@ namespace YooAsset
private readonly bool _suspendLoad;
private AsyncOperation _asyncOperation;
public BundledSceneProvider(ResourceManager manager, string providerGUID, uint providerPriority, AssetInfo assetInfo, LoadSceneMode sceneMode, bool suspendLoad) : base(manager, providerGUID, providerPriority, assetInfo)
public BundledSceneProvider(ResourceManager manager, string providerGUID, AssetInfo assetInfo, LoadSceneMode sceneMode, bool suspendLoad) : base(manager, providerGUID, assetInfo)
{
SceneMode = sceneMode;
SceneName = Path.GetFileNameWithoutExtension(assetInfo.AssetPath);
_suspendLoad = suspendLoad;
}
public override void Update()
internal override void InternalOnStart()
{
DebugBeginRecording();
}
internal override void InternalOnUpdate()
{
if (IsDone)
return;
if (Status == EStatus.None)
if (_steps == ESteps.None)
{
Status = EStatus.CheckBundle;
_steps = ESteps.CheckBundle;
}
// 1. 检测资源包
if (Status == EStatus.CheckBundle)
if (_steps == ESteps.CheckBundle)
{
if (DependBundles.IsDone() == false)
return;
@ -40,57 +42,57 @@ namespace YooAsset
if (DependBundles.IsSucceed() == false)
{
Status = EStatus.Failed;
LastError = DependBundles.GetLastError();
InvokeCompletion();
string error = DependBundles.GetLastError();
InvokeCompletion(error, EOperationStatus.Failed);
return;
}
if (OwnerBundle.Status != BundleLoaderBase.EStatus.Succeed)
{
Status = EStatus.Failed;
LastError = OwnerBundle.LastError;
InvokeCompletion();
string error = OwnerBundle.LastError;
InvokeCompletion(error, EOperationStatus.Failed);
return;
}
Status = EStatus.Loading;
_steps = ESteps.Loading;
}
// 2. 加载场景
if (Status == EStatus.Loading)
if (_steps == ESteps.Loading)
{
// 注意如果场景不存在则返回NULL
_asyncOperation = SceneManager.LoadSceneAsync(MainAssetInfo.AssetPath, SceneMode);
if (_asyncOperation != null)
{
_asyncOperation.allowSceneActivation = !_suspendLoad;
_asyncOperation.priority = (int)ProviderPriority;
_asyncOperation.priority = 100;
SceneObject = SceneManager.GetSceneAt(SceneManager.sceneCount - 1);
Status = EStatus.Checking;
_steps = ESteps.Checking;
}
else
{
Status = EStatus.Failed;
LastError = $"Failed to load scene : {MainAssetInfo.AssetPath}";
YooLogger.Error(LastError);
InvokeCompletion();
string error = $"Failed to load scene : {MainAssetInfo.AssetPath}";
YooLogger.Error(error);
InvokeCompletion(error, EOperationStatus.Failed);
}
}
// 3. 检测加载结果
if (Status == EStatus.Checking)
if (_steps == ESteps.Checking)
{
Progress = _asyncOperation.progress;
if (_asyncOperation.isDone)
{
Status = SceneObject.IsValid() ? EStatus.Succeed : EStatus.Failed;
if (Status == EStatus.Failed)
if (SceneObject.IsValid())
{
LastError = $"The load scene is invalid : {MainAssetInfo.AssetPath}";
YooLogger.Error(LastError);
InvokeCompletion(string.Empty, EOperationStatus.Succeed);
}
else
{
string error = $"The load scene is invalid : {MainAssetInfo.AssetPath}";
YooLogger.Error(error);
InvokeCompletion(error, EOperationStatus.Failed);
}
InvokeCompletion();
}
}
}

View File

@ -8,23 +8,25 @@ namespace YooAsset
{
private AssetBundleRequest _cacheRequest;
public BundledSubAssetsProvider(ResourceManager manager, string providerGUID, uint providerPriority, AssetInfo assetInfo) : base(manager, providerGUID, providerPriority, assetInfo)
public BundledSubAssetsProvider(ResourceManager manager, string providerGUID, AssetInfo assetInfo) : base(manager, providerGUID, assetInfo)
{
}
public override void Update()
internal override void InternalOnStart()
{
DebugBeginRecording();
}
internal override void InternalOnUpdate()
{
if (IsDone)
return;
if (Status == EStatus.None)
if (_steps == ESteps.None)
{
Status = EStatus.CheckBundle;
_steps = ESteps.CheckBundle;
}
// 1. 检测资源包
if (Status == EStatus.CheckBundle)
if (_steps == ESteps.CheckBundle)
{
if (IsWaitForAsyncComplete)
{
@ -39,17 +41,15 @@ namespace YooAsset
if (DependBundles.IsSucceed() == false)
{
Status = EStatus.Failed;
LastError = DependBundles.GetLastError();
InvokeCompletion();
string error = DependBundles.GetLastError();
InvokeCompletion(error, EOperationStatus.Failed);
return;
}
if (OwnerBundle.Status != BundleLoaderBase.EStatus.Succeed)
{
Status = EStatus.Failed;
LastError = OwnerBundle.LastError;
InvokeCompletion();
string error = OwnerBundle.LastError;
InvokeCompletion(error, EOperationStatus.Failed);
return;
}
@ -59,11 +59,11 @@ namespace YooAsset
return;
}
Status = EStatus.Loading;
_steps = ESteps.Loading;
}
// 2. 加载资源对象
if (Status == EStatus.Loading)
if (_steps == ESteps.Loading)
{
if (IsWaitForAsyncComplete || IsForceDestroyComplete)
{
@ -79,11 +79,11 @@ namespace YooAsset
else
_cacheRequest = OwnerBundle.CacheBundle.LoadAssetWithSubAssetsAsync(MainAssetInfo.AssetPath, MainAssetInfo.AssetType);
}
Status = EStatus.Checking;
_steps = ESteps.Checking;
}
// 3. 检测加载结果
if (Status == EStatus.Checking)
if (_steps == ESteps.Checking)
{
if (_cacheRequest != null)
{
@ -102,16 +102,20 @@ namespace YooAsset
}
}
Status = AllAssetObjects == null ? EStatus.Failed : EStatus.Succeed;
if (Status == EStatus.Failed)
if (AllAssetObjects == null)
{
string error;
if (MainAssetInfo.AssetType == null)
LastError = $"Failed to load sub assets : {MainAssetInfo.AssetPath} AssetType : null AssetBundle : {OwnerBundle.MainBundleInfo.Bundle.BundleName}";
error = $"Failed to load sub assets : {MainAssetInfo.AssetPath} AssetType : null AssetBundle : {OwnerBundle.MainBundleInfo.Bundle.BundleName}";
else
LastError = $"Failed to load sub assets : {MainAssetInfo.AssetPath} AssetType : {MainAssetInfo.AssetType} AssetBundle : {OwnerBundle.MainBundleInfo.Bundle.BundleName}";
YooLogger.Error(LastError);
error = $"Failed to load sub assets : {MainAssetInfo.AssetPath} AssetType : {MainAssetInfo.AssetType} AssetBundle : {OwnerBundle.MainBundleInfo.Bundle.BundleName}";
YooLogger.Error(error);
InvokeCompletion(error, EOperationStatus.Failed);
}
else
{
InvokeCompletion(string.Empty, EOperationStatus.Succeed);
}
InvokeCompletion();
}
}
}

View File

@ -3,19 +3,22 @@ namespace YooAsset
{
internal sealed class CompletedProvider : ProviderBase
{
public CompletedProvider(AssetInfo assetInfo) : base(null, string.Empty, 0, assetInfo)
public CompletedProvider(AssetInfo assetInfo) : base(null, string.Empty, assetInfo)
{
}
public override void Update()
internal override void InternalOnStart()
{
}
internal override void InternalOnUpdate()
{
}
public void SetCompleted(string error)
{
if (Status == EStatus.None)
if (_steps == ESteps.None)
{
Status = EStatus.Failed;
LastError = error;
InvokeCompletion();
InvokeCompletion(error, EOperationStatus.Failed);
}
}
}

View File

@ -6,29 +6,32 @@ namespace YooAsset
{
internal sealed class DatabaseAllAssetsProvider : ProviderBase
{
public DatabaseAllAssetsProvider(ResourceManager manager, string providerGUID, uint providerPriority, AssetInfo assetInfo) : base(manager, providerGUID, providerPriority, assetInfo)
public DatabaseAllAssetsProvider(ResourceManager manager, string providerGUID, AssetInfo assetInfo) : base(manager, providerGUID, assetInfo)
{
}
public override void Update()
internal override void InternalOnStart()
{
DebugBeginRecording();
}
internal override void InternalOnUpdate()
{
#if UNITY_EDITOR
if (IsDone)
return;
if (Status == EStatus.None)
if (_steps == ESteps.None)
{
// 检测资源文件是否存在
string guid = UnityEditor.AssetDatabase.AssetPathToGUID(MainAssetInfo.AssetPath);
if (string.IsNullOrEmpty(guid))
{
Status = EStatus.Failed;
LastError = $"Not found asset : {MainAssetInfo.AssetPath}";
YooLogger.Error(LastError);
InvokeCompletion();
string error = $"Not found asset : {MainAssetInfo.AssetPath}";
YooLogger.Error(error);
InvokeCompletion(error, EOperationStatus.Failed);
return;
}
Status = EStatus.CheckBundle;
_steps = ESteps.CheckBundle;
// 注意:模拟异步加载效果提前返回
if (IsWaitForAsyncComplete == false)
@ -36,7 +39,7 @@ namespace YooAsset
}
// 1. 检测资源包
if (Status == EStatus.CheckBundle)
if (_steps == ESteps.CheckBundle)
{
if (IsWaitForAsyncComplete)
{
@ -48,17 +51,16 @@ namespace YooAsset
if (OwnerBundle.Status != BundleLoaderBase.EStatus.Succeed)
{
Status = EStatus.Failed;
LastError = OwnerBundle.LastError;
InvokeCompletion();
string error = OwnerBundle.LastError;
InvokeCompletion(error, EOperationStatus.Failed);
return;
}
Status = EStatus.Loading;
_steps = ESteps.Loading;
}
// 2. 加载资源对象
if (Status == EStatus.Loading)
if (_steps == ESteps.Loading)
{
if (MainAssetInfo.AssetType == null)
{
@ -82,22 +84,26 @@ namespace YooAsset
}
AllAssetObjects = result.ToArray();
}
Status = EStatus.Checking;
_steps = ESteps.Checking;
}
// 3. 检测加载结果
if (Status == EStatus.Checking)
if (_steps == ESteps.Checking)
{
Status = AllAssetObjects == null ? EStatus.Failed : EStatus.Succeed;
if (Status == EStatus.Failed)
if (AllAssetObjects == null)
{
string error;
if (MainAssetInfo.AssetType == null)
LastError = $"Failed to load all assets : {MainAssetInfo.AssetPath} AssetType : null";
error = $"Failed to load all assets : {MainAssetInfo.AssetPath} AssetType : null";
else
LastError = $"Failed to load all assets : {MainAssetInfo.AssetPath} AssetType : {MainAssetInfo.AssetType}";
YooLogger.Error(LastError);
error = $"Failed to load all assets : {MainAssetInfo.AssetPath} AssetType : {MainAssetInfo.AssetType}";
YooLogger.Error(error);
InvokeCompletion(error, EOperationStatus.Failed);
}
else
{
InvokeCompletion(string.Empty, EOperationStatus.Succeed);
}
InvokeCompletion();
}
#endif
}

View File

@ -6,29 +6,32 @@ namespace YooAsset
{
internal sealed class DatabaseAssetProvider : ProviderBase
{
public DatabaseAssetProvider(ResourceManager manager, string providerGUID, uint providerPriority, AssetInfo assetInfo) : base(manager, providerGUID, providerPriority, assetInfo)
public DatabaseAssetProvider(ResourceManager manager, string providerGUID, AssetInfo assetInfo) : base(manager, providerGUID, assetInfo)
{
}
public override void Update()
internal override void InternalOnStart()
{
DebugBeginRecording();
}
internal override void InternalOnUpdate()
{
#if UNITY_EDITOR
if (IsDone)
return;
if (Status == EStatus.None)
if (_steps == ESteps.None)
{
// 检测资源文件是否存在
string guid = UnityEditor.AssetDatabase.AssetPathToGUID(MainAssetInfo.AssetPath);
if (string.IsNullOrEmpty(guid))
{
Status = EStatus.Failed;
LastError = $"Not found asset : {MainAssetInfo.AssetPath}";
YooLogger.Error(LastError);
InvokeCompletion();
string error = $"Not found asset : {MainAssetInfo.AssetPath}";
YooLogger.Error(error);
InvokeCompletion(error, EOperationStatus.Failed);
return;
}
Status = EStatus.CheckBundle;
_steps = ESteps.CheckBundle;
// 注意:模拟异步加载效果提前返回
if (IsWaitForAsyncComplete == false)
@ -36,7 +39,7 @@ namespace YooAsset
}
// 1. 检测资源包
if (Status == EStatus.CheckBundle)
if (_steps == ESteps.CheckBundle)
{
if (IsWaitForAsyncComplete)
{
@ -48,38 +51,41 @@ namespace YooAsset
if (OwnerBundle.Status != BundleLoaderBase.EStatus.Succeed)
{
Status = EStatus.Failed;
LastError = OwnerBundle.LastError;
InvokeCompletion();
string error = OwnerBundle.LastError;
InvokeCompletion(error, EOperationStatus.Failed);
return;
}
Status = EStatus.Loading;
_steps = ESteps.Loading;
}
// 2. 加载资源对象
if (Status == EStatus.Loading)
if (_steps == ESteps.Loading)
{
if (MainAssetInfo.AssetType == null)
AssetObject = UnityEditor.AssetDatabase.LoadMainAssetAtPath(MainAssetInfo.AssetPath);
else
AssetObject = UnityEditor.AssetDatabase.LoadAssetAtPath(MainAssetInfo.AssetPath, MainAssetInfo.AssetType);
Status = EStatus.Checking;
_steps = ESteps.Checking;
}
// 3. 检测加载结果
if (Status == EStatus.Checking)
if (_steps == ESteps.Checking)
{
Status = AssetObject == null ? EStatus.Failed : EStatus.Succeed;
if (Status == EStatus.Failed)
if (AssetObject == null)
{
string error;
if (MainAssetInfo.AssetType == null)
LastError = $"Failed to load asset object : {MainAssetInfo.AssetPath} AssetType : null";
error = $"Failed to load asset object : {MainAssetInfo.AssetPath} AssetType : null";
else
LastError = $"Failed to load asset object : {MainAssetInfo.AssetPath} AssetType : {MainAssetInfo.AssetType}";
YooLogger.Error(LastError);
error = $"Failed to load asset object : {MainAssetInfo.AssetPath} AssetType : {MainAssetInfo.AssetType}";
YooLogger.Error(error);
InvokeCompletion(error, EOperationStatus.Failed);
}
else
{
InvokeCompletion(string.Empty, EOperationStatus.Succeed);
}
InvokeCompletion();
}
#endif
}

View File

@ -3,29 +3,32 @@ namespace YooAsset
{
internal class DatabaseRawFileProvider : ProviderBase
{
public DatabaseRawFileProvider(ResourceManager manager, string providerGUID, uint providerPriority, AssetInfo assetInfo) : base(manager, providerGUID, providerPriority, assetInfo)
public DatabaseRawFileProvider(ResourceManager manager, string providerGUID, AssetInfo assetInfo) : base(manager, providerGUID, assetInfo)
{
}
public override void Update()
internal override void InternalOnStart()
{
DebugBeginRecording();
}
internal override void InternalOnUpdate()
{
#if UNITY_EDITOR
if (IsDone)
return;
if (Status == EStatus.None)
if (_steps == ESteps.None)
{
// 检测资源文件是否存在
string guid = UnityEditor.AssetDatabase.AssetPathToGUID(MainAssetInfo.AssetPath);
if (string.IsNullOrEmpty(guid))
{
Status = EStatus.Failed;
LastError = $"Not found asset : {MainAssetInfo.AssetPath}";
YooLogger.Error(LastError);
InvokeCompletion();
string error = $"Not found asset : {MainAssetInfo.AssetPath}";
YooLogger.Error(error);
InvokeCompletion(error, EOperationStatus.Failed);
return;
}
Status = EStatus.CheckBundle;
_steps = ESteps.CheckBundle;
// 注意:模拟异步加载效果提前返回
if (IsWaitForAsyncComplete == false)
@ -33,7 +36,7 @@ namespace YooAsset
}
// 1. 检测资源包
if (Status == EStatus.CheckBundle)
if (_steps == ESteps.CheckBundle)
{
if (IsWaitForAsyncComplete)
{
@ -45,21 +48,19 @@ namespace YooAsset
if (OwnerBundle.Status != BundleLoaderBase.EStatus.Succeed)
{
Status = EStatus.Failed;
LastError = OwnerBundle.LastError;
InvokeCompletion();
string error = OwnerBundle.LastError;
InvokeCompletion(error, EOperationStatus.Failed);
return;
}
Status = EStatus.Checking;
_steps = ESteps.Checking;
}
// 2. 检测加载结果
if (Status == EStatus.Checking)
if (_steps == ESteps.Checking)
{
RawFilePath = MainAssetInfo.AssetPath;
Status = EStatus.Succeed;
InvokeCompletion();
InvokeCompletion(string.Empty, EOperationStatus.Succeed);
}
#endif
}

View File

@ -12,25 +12,29 @@ namespace YooAsset
private readonly bool _suspendLoad;
private AsyncOperation _asyncOperation;
public DatabaseSceneProvider(ResourceManager manager, string providerGUID, uint providerPriority, AssetInfo assetInfo, LoadSceneMode sceneMode, bool suspendLoad) : base(manager, providerGUID, providerPriority, assetInfo)
public DatabaseSceneProvider(ResourceManager manager, string providerGUID, AssetInfo assetInfo, LoadSceneMode sceneMode, bool suspendLoad) : base(manager, providerGUID, assetInfo)
{
SceneMode = sceneMode;
SceneName = Path.GetFileNameWithoutExtension(assetInfo.AssetPath);
_suspendLoad = suspendLoad;
}
public override void Update()
internal override void InternalOnStart()
{
DebugBeginRecording();
}
internal override void InternalOnUpdate()
{
#if UNITY_EDITOR
if (IsDone)
return;
if (Status == EStatus.None)
if (_steps == ESteps.None)
{
Status = EStatus.CheckBundle;
_steps = ESteps.CheckBundle;
}
// 1. 检测资源包
if (Status == EStatus.CheckBundle)
if (_steps == ESteps.CheckBundle)
{
if (IsWaitForAsyncComplete)
{
@ -42,17 +46,16 @@ namespace YooAsset
if (OwnerBundle.Status != BundleLoaderBase.EStatus.Succeed)
{
Status = EStatus.Failed;
LastError = OwnerBundle.LastError;
InvokeCompletion();
string error = OwnerBundle.LastError;
InvokeCompletion(error, EOperationStatus.Failed);
return;
}
Status = EStatus.Loading;
_steps = ESteps.Loading;
}
// 2. 加载资源对象
if (Status == EStatus.Loading)
if (_steps == ESteps.Loading)
{
LoadSceneParameters loadSceneParameters = new LoadSceneParameters();
loadSceneParameters.loadSceneMode = SceneMode;
@ -60,32 +63,34 @@ namespace YooAsset
if (_asyncOperation != null)
{
_asyncOperation.allowSceneActivation = !_suspendLoad;
_asyncOperation.priority = (int)ProviderPriority;
_asyncOperation.priority = 100;
SceneObject = SceneManager.GetSceneAt(SceneManager.sceneCount - 1);
Status = EStatus.Checking;
_steps = ESteps.Checking;
}
else
{
Status = EStatus.Failed;
LastError = $"Failed to load scene : {MainAssetInfo.AssetPath}";
YooLogger.Error(LastError);
InvokeCompletion();
string error = $"Failed to load scene : {MainAssetInfo.AssetPath}";
YooLogger.Error(error);
InvokeCompletion(error, EOperationStatus.Failed);
}
}
// 3. 检测加载结果
if (Status == EStatus.Checking)
if (_steps == ESteps.Checking)
{
Progress = _asyncOperation.progress;
if (_asyncOperation.isDone)
{
Status = SceneObject.IsValid() ? EStatus.Succeed : EStatus.Failed;
if (Status == EStatus.Failed)
if (SceneObject.IsValid())
{
LastError = $"The loaded scene is invalid : {MainAssetInfo.AssetPath}";
YooLogger.Error(LastError);
InvokeCompletion(string.Empty, EOperationStatus.Succeed);
}
else
{
string error = $"The loaded scene is invalid : {MainAssetInfo.AssetPath}";
YooLogger.Error(error);
InvokeCompletion(error, EOperationStatus.Failed);
}
InvokeCompletion();
}
}
#endif

View File

@ -6,29 +6,32 @@ namespace YooAsset
{
internal sealed class DatabaseSubAssetsProvider : ProviderBase
{
public DatabaseSubAssetsProvider(ResourceManager manager, string providerGUID, uint providerPriority, AssetInfo assetInfo) : base(manager, providerGUID, providerPriority, assetInfo)
public DatabaseSubAssetsProvider(ResourceManager manager, string providerGUID, AssetInfo assetInfo) : base(manager, providerGUID, assetInfo)
{
}
public override void Update()
internal override void InternalOnStart()
{
DebugBeginRecording();
}
internal override void InternalOnUpdate()
{
#if UNITY_EDITOR
if (IsDone)
return;
if (Status == EStatus.None)
if (_steps == ESteps.None)
{
// 检测资源文件是否存在
string guid = UnityEditor.AssetDatabase.AssetPathToGUID(MainAssetInfo.AssetPath);
if (string.IsNullOrEmpty(guid))
{
Status = EStatus.Failed;
LastError = $"Not found asset : {MainAssetInfo.AssetPath}";
YooLogger.Error(LastError);
InvokeCompletion();
string error = $"Not found asset : {MainAssetInfo.AssetPath}";
YooLogger.Error(error);
InvokeCompletion(error, EOperationStatus.Failed);
return;
}
Status = EStatus.CheckBundle;
_steps = ESteps.CheckBundle;
// 注意:模拟异步加载效果提前返回
if (IsWaitForAsyncComplete == false)
@ -36,7 +39,7 @@ namespace YooAsset
}
// 1. 检测资源包
if (Status == EStatus.CheckBundle)
if (_steps == ESteps.CheckBundle)
{
if (IsWaitForAsyncComplete)
{
@ -48,17 +51,16 @@ namespace YooAsset
if (OwnerBundle.Status != BundleLoaderBase.EStatus.Succeed)
{
Status = EStatus.Failed;
LastError = OwnerBundle.LastError;
InvokeCompletion();
string error = OwnerBundle.LastError;
InvokeCompletion(error, EOperationStatus.Failed);
return;
}
Status = EStatus.Loading;
_steps = ESteps.Loading;
}
// 2. 加载资源对象
if (Status == EStatus.Loading)
if (_steps == ESteps.Loading)
{
if (MainAssetInfo.AssetType == null)
{
@ -75,22 +77,26 @@ namespace YooAsset
}
AllAssetObjects = result.ToArray();
}
Status = EStatus.Checking;
_steps = ESteps.Checking;
}
// 3. 检测加载结果
if (Status == EStatus.Checking)
if (_steps == ESteps.Checking)
{
Status = AllAssetObjects == null ? EStatus.Failed : EStatus.Succeed;
if (Status == EStatus.Failed)
if (AllAssetObjects == null)
{
string error;
if (MainAssetInfo.AssetType == null)
LastError = $"Failed to load sub assets : {MainAssetInfo.AssetPath} AssetType : null";
error = $"Failed to load sub assets : {MainAssetInfo.AssetPath} AssetType : null";
else
LastError = $"Failed to load sub assets : {MainAssetInfo.AssetPath} AssetType : {MainAssetInfo.AssetType}";
YooLogger.Error(LastError);
error = $"Failed to load sub assets : {MainAssetInfo.AssetPath} AssetType : {MainAssetInfo.AssetType}";
YooLogger.Error(error);
InvokeCompletion(error, EOperationStatus.Failed);
}
else
{
InvokeCompletion(string.Empty, EOperationStatus.Succeed);
}
InvokeCompletion();
}
#endif
}

View File

@ -6,16 +6,15 @@ using System;
namespace YooAsset
{
internal abstract class ProviderBase : IComparable<ProviderBase>
internal abstract class ProviderBase : AsyncOperationBase
{
public enum EStatus
protected enum ESteps
{
None = 0,
CheckBundle,
Loading,
Checking,
Succeed,
Failed,
Done,
}
/// <summary>
@ -23,11 +22,6 @@ namespace YooAsset
/// </summary>
public string ProviderGUID { private set; get; }
/// <summary>
/// 资源加载的优先级
/// </summary>
public uint ProviderPriority { private set; get; }
/// <summary>
/// 所属资源系统
/// </summary>
@ -63,22 +57,6 @@ namespace YooAsset
/// </summary>
public string RawFilePath { protected set; get; }
/// <summary>
/// 当前的加载状态
/// </summary>
public EStatus Status { protected set; get; } = EStatus.None;
/// <summary>
/// 最近的错误信息
/// </summary>
public string LastError { protected set; get; } = string.Empty;
/// <summary>
/// 加载进度
/// </summary>
public float Progress { protected set; get; } = 0f;
/// <summary>
/// 引用计数
/// </summary>
@ -89,18 +67,8 @@ namespace YooAsset
/// </summary>
public bool IsDestroyed { private set; get; } = false;
/// <summary>
/// 是否完毕(成功或失败)
/// </summary>
public bool IsDone
{
get
{
return Status == EStatus.Succeed || Status == EStatus.Failed;
}
}
protected ESteps _steps = ESteps.None;
protected BundleLoaderBase OwnerBundle { private set; get; }
protected DependAssetBundles DependBundles { private set; get; }
protected bool IsWaitForAsyncComplete { private set; get; } = false;
@ -108,11 +76,10 @@ namespace YooAsset
private readonly List<HandleBase> _handles = new List<HandleBase>();
public ProviderBase(ResourceManager manager, string providerGUID, uint providerPriority, AssetInfo assetInfo)
public ProviderBase(ResourceManager manager, string providerGUID, AssetInfo assetInfo)
{
ResourceMgr = manager;
ProviderGUID = providerGUID;
ProviderPriority = providerPriority;
MainAssetInfo = assetInfo;
// 创建资源包加载器
@ -128,11 +95,6 @@ namespace YooAsset
}
}
/// <summary>
/// 轮询更新方法
/// </summary>
public abstract void Update();
/// <summary>
/// 销毁资源提供者
/// </summary>
@ -140,6 +102,13 @@ namespace YooAsset
{
IsDestroyed = true;
// 检测是否为正常销毁
if (IsDone == false)
{
Error = "User abort !";
Status = EOperationStatus.Failed;
}
// 释放资源包加载器
if (OwnerBundle != null)
{
@ -159,7 +128,7 @@ namespace YooAsset
public bool CanDestroy()
{
// 注意:在进行资源加载过程时不可以销毁
if (Status == EStatus.Loading || Status == EStatus.Checking)
if (_steps == ESteps.Loading || _steps == ESteps.Checking)
return false;
return RefCount <= 0;
@ -226,7 +195,7 @@ namespace YooAsset
IsWaitForAsyncComplete = true;
// 注意:主动轮询更新完成同步加载
Update();
InternalOnUpdate();
// 验证结果
if (IsDone == false)
@ -244,7 +213,7 @@ namespace YooAsset
// 注意:主动轮询更新完成同步加载
// 说明:如果资源包未准备完毕也可以放心销毁。
Update();
InternalOnUpdate();
}
/// <summary>
@ -255,44 +224,21 @@ namespace YooAsset
if (OwnerBundle.IsDestroyed)
throw new System.Exception("Should never get here !");
Status = EStatus.Failed;
LastError = $"The bundle {OwnerBundle.MainBundleInfo.Bundle.BundleName} has been destroyed by unity bugs !";
YooLogger.Error(LastError);
InvokeCompletion();
string error = $"The bundle {OwnerBundle.MainBundleInfo.Bundle.BundleName} has been destroyed by unity bugs !";
YooLogger.Error(error);
InvokeCompletion(Error, EOperationStatus.Failed);
}
/// <summary>
/// 异步操作任务
/// 结束流程
/// </summary>
public Task Task
{
get
{
if (_taskCompletionSource == null)
{
_taskCompletionSource = new TaskCompletionSource<object>();
if (IsDone)
_taskCompletionSource.SetResult(null);
}
return _taskCompletionSource.Task;
}
}
#region 排序接口实现
public int CompareTo(ProviderBase other)
{
return other.ProviderPriority.CompareTo(this.ProviderPriority);
}
#endregion
#region 异步编程相关
private TaskCompletionSource<object> _taskCompletionSource;
protected void InvokeCompletion()
protected void InvokeCompletion(string error, EOperationStatus status)
{
DebugEndRecording();
// 进度百分百完成
Progress = 1f;
_steps = ESteps.Done;
Error = error;
Status = status;
// 注意:创建临时列表是为了防止外部逻辑在回调函数内创建或者释放资源句柄。
// 注意:回调方法如果发生异常,会阻断列表里的后续回调方法!
@ -304,11 +250,7 @@ namespace YooAsset
hande.InvokeCallback();
}
}
if (_taskCompletionSource != null)
_taskCompletionSource.TrySetResult(null);
}
#endregion
#region 调试信息相关
/// <summary>

View File

@ -9,32 +9,17 @@ namespace YooAsset
{
internal class ResourceManager
{
private readonly Dictionary<string, BundleLoaderBase> _loaderDic = new Dictionary<string, BundleLoaderBase>(5000);
private readonly List<BundleLoaderBase> _loaderList = new List<BundleLoaderBase>(5000);
private readonly Dictionary<string, ProviderBase> _providerDic = new Dictionary<string, ProviderBase>(5000);
private readonly List<ProviderBase> _providerList = new List<ProviderBase>(5000);
// 全局场景句柄集合
private readonly static Dictionary<string, SceneHandle> _sceneHandles = new Dictionary<string, SceneHandle>(100);
private static long _sceneCreateCount = 0;
private readonly Dictionary<string, ProviderBase> _providerDic = new Dictionary<string, ProviderBase>(5000);
private readonly Dictionary<string, BundleLoaderBase> _loaderDic = new Dictionary<string, BundleLoaderBase>(5000);
private readonly List<BundleLoaderBase> _loaderList = new List<BundleLoaderBase>(5000);
private bool _simulationOnEditor;
private bool _autoDestroyAssetProvider;
private long _loadingMaxTimeSlice;
private IBundleQuery _bundleQuery;
private bool _isUnloadSafe = true;
// 计时器相关
private Stopwatch _watch;
private long _frameTime;
private bool IsBusy
{
get
{
return _watch.ElapsedMilliseconds - _frameTime >= _loadingMaxTimeSlice;
}
}
/// <summary>
/// 所属包裹
@ -50,13 +35,11 @@ namespace YooAsset
/// <summary>
/// 初始化
/// </summary>
public void Initialize(bool simulationOnEditor, bool autoDestroyAssetProvider, long loadingMaxTimeSlice, IBundleQuery bundleServices)
public void Initialize(bool simulationOnEditor, bool autoDestroyAssetProvider, IBundleQuery bundleServices)
{
_simulationOnEditor = simulationOnEditor;
_autoDestroyAssetProvider = autoDestroyAssetProvider;
_loadingMaxTimeSlice = loadingMaxTimeSlice;
_bundleQuery = bundleServices;
_watch = Stopwatch.StartNew();
}
/// <summary>
@ -64,9 +47,6 @@ namespace YooAsset
/// </summary>
public void Update()
{
_frameTime = _watch.ElapsedMilliseconds;
// 更新加载器
foreach (var loader in _loaderList)
{
loader.Update();
@ -74,17 +54,6 @@ namespace YooAsset
if (_autoDestroyAssetProvider)
loader.TryDestroyProviders();
}
// 更新资源提供者
// 注意:循环更新的时候,可能会扩展列表
_isUnloadSafe = false;
for (int i = 0; i < _providerList.Count; i++)
{
if (IsBusy)
break;
_providerList[i].Update();
}
_isUnloadSafe = true;
}
/// <summary>
@ -92,12 +61,6 @@ namespace YooAsset
/// </summary>
public void UnloadUnusedAssets()
{
if (_isUnloadSafe == false)
{
YooLogger.Warning("Can not unload unused assets when processing resource loading !");
return;
}
// 注意:资源包之间可能存在多层深层嵌套,需要多次循环释放。
int loopCount = 10;
for (int i = 0; i < loopCount; i++)
@ -191,13 +154,13 @@ namespace YooAsset
}
// 释放所有资源句柄
foreach (var provider in _providerList)
foreach (var provider in _providerDic.Values)
{
provider.ReleaseAllHandles();
}
// 强制销毁资源提供者
foreach (var provider in _providerList)
foreach (var provider in _providerDic.Values)
{
provider.ForceDestroyComplete();
provider.Destroy();
@ -211,7 +174,6 @@ namespace YooAsset
}
// 清空数据
_providerList.Clear();
_providerDic.Clear();
_loaderList.Clear();
_loaderDic.Clear();
@ -248,18 +210,15 @@ namespace YooAsset
ProviderBase provider;
{
if (_simulationOnEditor)
provider = new DatabaseSceneProvider(this, providerGUID, priority, assetInfo, sceneMode, suspendLoad);
provider = new DatabaseSceneProvider(this, providerGUID, assetInfo, sceneMode, suspendLoad);
else
provider = new BundledSceneProvider(this, providerGUID, priority, assetInfo, sceneMode, suspendLoad);
provider = new BundledSceneProvider(this, providerGUID, assetInfo, sceneMode, suspendLoad);
provider.InitSpawnDebugInfo();
_providerList.Add(provider);
_providerDic.Add(providerGUID, provider);
if (priority > 0)
{
_providerList.Sort();
}
OperationSystem.StartOperation(PackageName, provider);
}
provider.Priority = priority;
var handle = provider.CreateHandle<SceneHandle>();
handle.PackageName = PackageName;
_sceneHandles.Add(providerGUID, handle);
@ -284,17 +243,15 @@ namespace YooAsset
if (provider == null)
{
if (_simulationOnEditor)
provider = new DatabaseAssetProvider(this, providerGUID, priority, assetInfo);
provider = new DatabaseAssetProvider(this, providerGUID, assetInfo);
else
provider = new BundledAssetProvider(this, providerGUID, priority, assetInfo);
provider = new BundledAssetProvider(this, providerGUID, assetInfo);
provider.InitSpawnDebugInfo();
_providerList.Add(provider);
_providerDic.Add(providerGUID, provider);
if (priority > 0)
{
_providerList.Sort();
}
OperationSystem.StartOperation(PackageName, provider);
}
provider.Priority = priority;
return provider.CreateHandle<AssetHandle>();
}
@ -316,17 +273,15 @@ namespace YooAsset
if (provider == null)
{
if (_simulationOnEditor)
provider = new DatabaseSubAssetsProvider(this, providerGUID, priority, assetInfo);
provider = new DatabaseSubAssetsProvider(this, providerGUID, assetInfo);
else
provider = new BundledSubAssetsProvider(this, providerGUID, priority, assetInfo);
provider = new BundledSubAssetsProvider(this, providerGUID, assetInfo);
provider.InitSpawnDebugInfo();
_providerList.Add(provider);
_providerDic.Add(providerGUID, provider);
if (priority > 0)
{
_providerList.Sort();
}
OperationSystem.StartOperation(PackageName, provider);
}
provider.Priority = priority;
return provider.CreateHandle<SubAssetsHandle>();
}
@ -348,17 +303,15 @@ namespace YooAsset
if (provider == null)
{
if (_simulationOnEditor)
provider = new DatabaseAllAssetsProvider(this, providerGUID, priority, assetInfo);
provider = new DatabaseAllAssetsProvider(this, providerGUID, assetInfo);
else
provider = new BundledAllAssetsProvider(this, providerGUID, priority, assetInfo);
provider = new BundledAllAssetsProvider(this, providerGUID, assetInfo);
provider.InitSpawnDebugInfo();
_providerList.Add(provider);
_providerDic.Add(providerGUID, provider);
if (priority > 0)
{
_providerList.Sort();
}
OperationSystem.StartOperation(PackageName, provider);
}
provider.Priority = priority;
return provider.CreateHandle<AllAssetsHandle>();
}
@ -380,17 +333,15 @@ namespace YooAsset
if (provider == null)
{
if (_simulationOnEditor)
provider = new DatabaseRawFileProvider(this, providerGUID, priority, assetInfo);
provider = new DatabaseRawFileProvider(this, providerGUID, assetInfo);
else
provider = new BundledRawFileProvider(this, providerGUID, priority, assetInfo);
provider = new BundledRawFileProvider(this, providerGUID, assetInfo);
provider.InitSpawnDebugInfo();
_providerList.Add(provider);
_providerDic.Add(providerGUID, provider);
if (priority > 0)
{
_providerList.Sort();
}
OperationSystem.StartOperation(PackageName, provider);
}
provider.Priority = priority;
return provider.CreateHandle<RawFileHandle>();
}
@ -463,7 +414,6 @@ namespace YooAsset
{
foreach (var provider in removeList)
{
_providerList.Remove(provider);
_providerDic.Remove(provider.ProviderGUID);
}
}
@ -522,8 +472,8 @@ namespace YooAsset
#region 调试信息
internal List<DebugProviderInfo> GetDebugReportInfos()
{
List<DebugProviderInfo> result = new List<DebugProviderInfo>(_providerList.Count);
foreach (var provider in _providerList)
List<DebugProviderInfo> result = new List<DebugProviderInfo>(_providerDic.Count);
foreach (var provider in _providerDic.Values)
{
DebugProviderInfo providerInfo = new DebugProviderInfo();
providerInfo.AssetPath = provider.MainAssetInfo.AssetPath;

View File

@ -146,7 +146,7 @@ namespace YooAsset
var editorSimulateModeImpl = new EditorSimulateModeImpl(PackageName);
_bundleQuery = editorSimulateModeImpl;
_playModeImpl = editorSimulateModeImpl;
_resourceMgr.Initialize(true, parameters.AutoDestroyAssetProvider, parameters.LoadingMaxTimeSlice, _bundleQuery);
_resourceMgr.Initialize(true, parameters.AutoDestroyAssetProvider, _bundleQuery);
var initializeParameters = parameters as EditorSimulateModeParameters;
initializeOperation = editorSimulateModeImpl.InitializeAsync(assist, initializeParameters.SimulateManifestFilePath);
@ -156,7 +156,7 @@ namespace YooAsset
var offlinePlayModeImpl = new OfflinePlayModeImpl(PackageName);
_bundleQuery = offlinePlayModeImpl;
_playModeImpl = offlinePlayModeImpl;
_resourceMgr.Initialize(false, parameters.AutoDestroyAssetProvider, parameters.LoadingMaxTimeSlice, _bundleQuery);
_resourceMgr.Initialize(false, parameters.AutoDestroyAssetProvider, _bundleQuery);
var initializeParameters = parameters as OfflinePlayModeParameters;
initializeOperation = offlinePlayModeImpl.InitializeAsync(assist);
@ -166,7 +166,7 @@ namespace YooAsset
var hostPlayModeImpl = new HostPlayModeImpl(PackageName);
_bundleQuery = hostPlayModeImpl;
_playModeImpl = hostPlayModeImpl;
_resourceMgr.Initialize(false, parameters.AutoDestroyAssetProvider, parameters.LoadingMaxTimeSlice, _bundleQuery);
_resourceMgr.Initialize(false, parameters.AutoDestroyAssetProvider, _bundleQuery);
var initializeParameters = parameters as HostPlayModeParameters;
initializeOperation = hostPlayModeImpl.InitializeAsync(assist,
@ -179,7 +179,7 @@ namespace YooAsset
var webPlayModeImpl = new WebPlayModeImpl(PackageName);
_bundleQuery = webPlayModeImpl;
_playModeImpl = webPlayModeImpl;
_resourceMgr.Initialize(false, parameters.AutoDestroyAssetProvider, parameters.LoadingMaxTimeSlice, _bundleQuery);
_resourceMgr.Initialize(false, parameters.AutoDestroyAssetProvider, _bundleQuery);
var initializeParameters = parameters as WebPlayModeParameters;
initializeOperation = webPlayModeImpl.InitializeAsync(assist,
@ -266,13 +266,6 @@ namespace YooAsset
}
#endif
}
// 检测参数范围
if (parameters.LoadingMaxTimeSlice < 10)
{
parameters.LoadingMaxTimeSlice = 10;
YooLogger.Warning($"{nameof(parameters.LoadingMaxTimeSlice)} minimum value is 10 milliseconds.");
}
}
private void InitializeOperation_Completed(AsyncOperationBase op)
{
@ -566,6 +559,7 @@ namespace YooAsset
/// 异步加载原生文件
/// </summary>
/// <param name="assetInfo">资源信息</param>
/// <param name="priority">加载的优先级</param>
public RawFileHandle LoadRawFileAsync(AssetInfo assetInfo, uint priority = 0)
{
DebugCheckInitialize();
@ -576,6 +570,7 @@ namespace YooAsset
/// 异步加载原生文件
/// </summary>
/// <param name="location">资源的定位地址</param>
/// <param name="priority">加载的优先级</param>
public RawFileHandle LoadRawFileAsync(string location, uint priority = 0)
{
DebugCheckInitialize();
@ -601,8 +596,8 @@ namespace YooAsset
/// <param name="location">场景的定位地址</param>
/// <param name="sceneMode">场景加载模式</param>
/// <param name="suspendLoad">场景加载到90%自动挂起</param>
/// <param name="priority">优先级</param>
public SceneHandle LoadSceneAsync(string location, LoadSceneMode sceneMode = LoadSceneMode.Single, bool suspendLoad = false, uint priority = 100)
/// <param name="priority">加载的优先级</param>
public SceneHandle LoadSceneAsync(string location, LoadSceneMode sceneMode = LoadSceneMode.Single, bool suspendLoad = false, uint priority = 0)
{
DebugCheckInitialize();
AssetInfo assetInfo = ConvertLocationToAssetInfo(location, null);
@ -616,8 +611,8 @@ namespace YooAsset
/// <param name="assetInfo">场景的资源信息</param>
/// <param name="sceneMode">场景加载模式</param>
/// <param name="suspendLoad">场景加载到90%自动挂起</param>
/// <param name="priority">优先级</param>
public SceneHandle LoadSceneAsync(AssetInfo assetInfo, LoadSceneMode sceneMode = LoadSceneMode.Single, bool suspendLoad = false, uint priority = 100)
/// <param name="priority">加载的优先级</param>
public SceneHandle LoadSceneAsync(AssetInfo assetInfo, LoadSceneMode sceneMode = LoadSceneMode.Single, bool suspendLoad = false, uint priority = 0)
{
DebugCheckInitialize();
var handle = _resourceMgr.LoadSceneAsync(assetInfo, sceneMode, suspendLoad, priority);
@ -677,6 +672,7 @@ namespace YooAsset
/// 异步加载资源对象
/// </summary>
/// <param name="assetInfo">资源信息</param>
/// <param name="priority">加载的优先级</param>
public AssetHandle LoadAssetAsync(AssetInfo assetInfo, uint priority = 0)
{
DebugCheckInitialize();
@ -688,6 +684,7 @@ namespace YooAsset
/// </summary>
/// <typeparam name="TObject">资源类型</typeparam>
/// <param name="location">资源的定位地址</param>
/// <param name="priority">加载的优先级</param>
public AssetHandle LoadAssetAsync<TObject>(string location, uint priority = 0) where TObject : UnityEngine.Object
{
DebugCheckInitialize();
@ -700,6 +697,7 @@ namespace YooAsset
/// </summary>
/// <param name="location">资源的定位地址</param>
/// <param name="type">资源类型</param>
/// <param name="priority">加载的优先级</param>
public AssetHandle LoadAssetAsync(string location, System.Type type, uint priority = 0)
{
DebugCheckInitialize();
@ -711,6 +709,7 @@ namespace YooAsset
/// 异步加载资源对象
/// </summary>
/// <param name="location">资源的定位地址</param>
/// <param name="priority">加载的优先级</param>
public AssetHandle LoadAssetAsync(string location, uint priority = 0)
{
DebugCheckInitialize();
@ -783,6 +782,7 @@ namespace YooAsset
/// 异步加载子资源对象
/// </summary>
/// <param name="assetInfo">资源信息</param>
/// <param name="priority">加载的优先级</param>
public SubAssetsHandle LoadSubAssetsAsync(AssetInfo assetInfo, uint priority = 0)
{
DebugCheckInitialize();
@ -794,6 +794,7 @@ namespace YooAsset
/// </summary>
/// <typeparam name="TObject">资源类型</typeparam>
/// <param name="location">资源的定位地址</param>
/// <param name="priority">加载的优先级</param>
public SubAssetsHandle LoadSubAssetsAsync<TObject>(string location, uint priority = 0) where TObject : UnityEngine.Object
{
DebugCheckInitialize();
@ -806,6 +807,7 @@ namespace YooAsset
/// </summary>
/// <param name="location">资源的定位地址</param>
/// <param name="type">子对象类型</param>
/// <param name="priority">加载的优先级</param>
public SubAssetsHandle LoadSubAssetsAsync(string location, System.Type type, uint priority = 0)
{
DebugCheckInitialize();
@ -817,6 +819,7 @@ namespace YooAsset
/// 异步加载子资源对象
/// </summary>
/// <param name="location">资源的定位地址</param>
/// <param name="priority">加载的优先级</param>
public SubAssetsHandle LoadSubAssetsAsync(string location, uint priority = 0)
{
DebugCheckInitialize();
@ -889,6 +892,7 @@ namespace YooAsset
/// 异步加载资源包内所有资源对象
/// </summary>
/// <param name="assetInfo">资源信息</param>
/// <param name="priority">加载的优先级</param>
public AllAssetsHandle LoadAllAssetsAsync(AssetInfo assetInfo, uint priority = 0)
{
DebugCheckInitialize();
@ -900,6 +904,7 @@ namespace YooAsset
/// </summary>
/// <typeparam name="TObject">资源类型</typeparam>
/// <param name="location">资源的定位地址</param>
/// <param name="priority">加载的优先级</param>
public AllAssetsHandle LoadAllAssetsAsync<TObject>(string location, uint priority = 0) where TObject : UnityEngine.Object
{
DebugCheckInitialize();
@ -912,6 +917,7 @@ namespace YooAsset
/// </summary>
/// <param name="location">资源的定位地址</param>
/// <param name="type">子对象类型</param>
/// <param name="priority">加载的优先级</param>
public AllAssetsHandle LoadAllAssetsAsync(string location, System.Type type, uint priority = 0)
{
DebugCheckInitialize();
@ -923,6 +929,7 @@ namespace YooAsset
/// 异步加载资源包内所有资源对象
/// </summary>
/// <param name="location">资源的定位地址</param>
/// <param name="priority">加载的优先级</param>
public AllAssetsHandle LoadAllAssetsAsync(string location, uint priority = 0)
{
DebugCheckInitialize();