Files
YooAsset/Assets/YooAsset/Runtime/AssetSystem/Provider/BundledAssetProvider.cs

123 lines
3.2 KiB
C#
Raw Normal View History

2022-03-01 10:44:12 +08:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace YooAsset
{
internal sealed class BundledAssetProvider : ProviderBase
2022-03-01 10:44:12 +08:00
{
private AssetBundleRequest _cacheRequest;
2022-09-29 18:40:43 +08:00
public BundledAssetProvider(AssetSystemImpl impl, string providerGUID, AssetInfo assetInfo) : base(impl, providerGUID, assetInfo)
2022-03-01 10:44:12 +08:00
{
}
public override void Update()
{
DebugBeginRecording();
2022-10-22 17:46:46 +08:00
2022-03-01 10:44:12 +08:00
if (IsDone)
return;
2022-03-09 23:57:04 +08:00
if (Status == EStatus.None)
2022-03-01 10:44:12 +08:00
{
2022-03-09 23:57:04 +08:00
Status = EStatus.CheckBundle;
2022-03-01 10:44:12 +08:00
}
// 1. 检测资源包
2022-03-09 23:57:04 +08:00
if (Status == EStatus.CheckBundle)
2022-03-01 10:44:12 +08:00
{
if (IsWaitForAsyncComplete)
{
2023-06-27 10:28:16 +08:00
DependBundles.WaitForAsyncComplete();
2022-03-01 10:44:12 +08:00
OwnerBundle.WaitForAsyncComplete();
}
2023-06-27 10:28:16 +08:00
if (DependBundles.IsDone() == false)
2022-03-01 10:44:12 +08:00
return;
if (OwnerBundle.IsDone() == false)
return;
2023-06-27 10:28:16 +08:00
if (DependBundles.IsSucceed() == false)
2022-03-01 10:44:12 +08:00
{
Status = EStatus.Failed;
2023-06-27 10:28:16 +08:00
LastError = DependBundles.GetLastError();
2022-03-01 10:44:12 +08:00
InvokeCompletion();
return;
2022-03-01 10:44:12 +08:00
}
if (OwnerBundle.Status != BundleLoaderBase.EStatus.Succeed)
2022-03-01 10:44:12 +08:00
{
Status = EStatus.Failed;
LastError = OwnerBundle.LastError;
InvokeCompletion();
return;
2022-03-01 10:44:12 +08:00
}
2022-08-12 14:30:23 +08:00
if (OwnerBundle.CacheBundle == null)
{
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 !";
2022-10-24 15:39:04 +08:00
YooLogger.Error(LastError);
2022-08-12 14:30:23 +08:00
InvokeCompletion();
return;
}
Status = EStatus.Loading;
2022-03-01 10:44:12 +08:00
}
// 2. 加载资源对象
2022-03-09 23:57:04 +08:00
if (Status == EStatus.Loading)
2022-03-01 10:44:12 +08:00
{
if (IsWaitForAsyncComplete)
{
if (MainAssetInfo.AssetType == null)
AssetObject = OwnerBundle.CacheBundle.LoadAsset(MainAssetInfo.AssetPath);
2022-03-01 10:44:12 +08:00
else
AssetObject = OwnerBundle.CacheBundle.LoadAsset(MainAssetInfo.AssetPath, MainAssetInfo.AssetType);
2022-03-01 10:44:12 +08:00
}
else
{
if (MainAssetInfo.AssetType == null)
_cacheRequest = OwnerBundle.CacheBundle.LoadAssetAsync(MainAssetInfo.AssetPath);
2022-03-01 10:44:12 +08:00
else
_cacheRequest = OwnerBundle.CacheBundle.LoadAssetAsync(MainAssetInfo.AssetPath, MainAssetInfo.AssetType);
2022-03-01 10:44:12 +08:00
}
2022-03-09 23:57:04 +08:00
Status = EStatus.Checking;
2022-03-01 10:44:12 +08:00
}
// 3. 检测加载结果
2022-03-09 23:57:04 +08:00
if (Status == EStatus.Checking)
2022-03-01 10:44:12 +08:00
{
if (_cacheRequest != null)
{
if (IsWaitForAsyncComplete)
{
// 强制挂起主线程(注意:该操作会很耗时)
2022-03-09 21:53:01 +08:00
YooLogger.Warning("Suspend the main thread to load unity asset.");
2022-03-01 10:44:12 +08:00
AssetObject = _cacheRequest.asset;
}
else
{
2022-12-13 11:46:39 +08:00
Progress = _cacheRequest.progress;
2022-03-01 10:44:12 +08:00
if (_cacheRequest.isDone == false)
return;
AssetObject = _cacheRequest.asset;
}
}
Status = AssetObject == null ? EStatus.Failed : EStatus.Succeed;
if (Status == EStatus.Failed)
{
2022-08-12 14:30:23 +08:00
if (MainAssetInfo.AssetType == null)
LastError = $"Failed to load asset : {MainAssetInfo.AssetPath} AssetType : null AssetBundle : {OwnerBundle.MainBundleInfo.Bundle.BundleName}";
2022-05-09 21:18:15 +08:00
else
LastError = $"Failed to load asset : {MainAssetInfo.AssetPath} AssetType : {MainAssetInfo.AssetType} AssetBundle : {OwnerBundle.MainBundleInfo.Bundle.BundleName}";
YooLogger.Error(LastError);
}
2022-03-01 10:44:12 +08:00
InvokeCompletion();
}
}
}
}