update asset system

pull/82/head
hevinci 2023-03-08 12:10:07 +08:00
parent ac0112199d
commit 7b41fd82a4
6 changed files with 177 additions and 12 deletions

View File

@ -387,7 +387,7 @@ namespace YooAsset
// 新增下载需求 // 新增下载需求
#if UNITY_WEBGL #if UNITY_WEBGL
if (bundleInfo.Bundle.IsRawFile) if (bundleInfo.Bundle.IsRawFile)
loader = new RawBundleFileLoader(this, bundleInfo); loader = new RawBundleWebLoader(this, bundleInfo);
else else
loader = new AssetBundleWebLoader(this, bundleInfo); loader = new AssetBundleWebLoader(this, bundleInfo);
#else #else

View File

@ -300,7 +300,7 @@ namespace YooAsset
if (_isShowWaitForAsyncError == false) if (_isShowWaitForAsyncError == false)
{ {
_isShowWaitForAsyncError = true; _isShowWaitForAsyncError = true;
YooLogger.Error($"WaitForAsyncComplete failed ! Try load bundle : {MainBundleInfo.Bundle.BundleName} from remote with sync load method !"); YooLogger.Error($"{nameof(WaitForAsyncComplete)} failed ! Try load bundle : {MainBundleInfo.Bundle.BundleName} from remote with sync load method !");
} }
break; break;
} }

View File

@ -7,6 +7,9 @@ using UnityEngine.Networking;
namespace YooAsset namespace YooAsset
{ {
/// <summary>
/// WebGL平台加载器
/// </summary>
internal sealed class AssetBundleWebLoader : BundleLoaderBase internal sealed class AssetBundleWebLoader : BundleLoaderBase
{ {
private enum ESteps private enum ESteps
@ -24,7 +27,6 @@ namespace YooAsset
private ESteps _steps = ESteps.None; private ESteps _steps = ESteps.None;
private float _tryTimer = 0; private float _tryTimer = 0;
private bool _isShowWaitForAsyncError = false;
private DownloaderBase _downloader; private DownloaderBase _downloader;
private UnityWebRequest _webRequest; private UnityWebRequest _webRequest;
private AssetBundleCreateRequest _createRequest; private AssetBundleCreateRequest _createRequest;
@ -225,10 +227,11 @@ namespace YooAsset
/// </summary> /// </summary>
public override void WaitForAsyncComplete() public override void WaitForAsyncComplete()
{ {
if (_isShowWaitForAsyncError == false) if (IsDone() == false)
{ {
_isShowWaitForAsyncError = true; Status = EStatus.Failed;
YooLogger.Error($"WebGL platform not support {nameof(WaitForAsyncComplete)} ! Use the async load method instead of the sync load method !"); LastError = $"{nameof(WaitForAsyncComplete)} failed ! WebGL platform not support sync load method !";
YooLogger.Error(LastError);
} }
} }
} }

View File

@ -16,7 +16,6 @@ namespace YooAsset
} }
private ESteps _steps = ESteps.None; private ESteps _steps = ESteps.None;
private bool _isShowWaitForAsyncError = false;
private DownloaderBase _unpacker; private DownloaderBase _unpacker;
private DownloaderBase _downloader; private DownloaderBase _downloader;
@ -42,7 +41,7 @@ namespace YooAsset
} }
else if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromStreaming) else if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromStreaming)
{ {
#if UNITY_ANDROID || UNITY_WEBGL #if UNITY_ANDROID
_steps = ESteps.Unpack; _steps = ESteps.Unpack;
FileLoadPath = MainBundleInfo.Bundle.CachedDataFilePath; FileLoadPath = MainBundleInfo.Bundle.CachedDataFilePath;
#else #else
@ -156,14 +155,15 @@ namespace YooAsset
} }
// 保险机制 // 保险机制
// 注意:如果需要从WEB端下载资源,可能会触发保险机制! // 注意:如果需要从端下载资源,可能会触发保险机制!
frame--; frame--;
if (frame == 0) if (frame == 0)
{ {
if (_isShowWaitForAsyncError == false) if (IsDone() == false)
{ {
_isShowWaitForAsyncError = true; Status = EStatus.Failed;
YooLogger.Error($"WaitForAsyncComplete failed ! Try load bundle : {MainBundleInfo.Bundle.BundleName} from remote with sync load method !"); LastError = $"WaitForAsyncComplete failed ! Try load bundle : {MainBundleInfo.Bundle.BundleName} from remote with sync load method !";
YooLogger.Error(LastError);
} }
break; break;
} }

View File

@ -0,0 +1,151 @@
using System.IO;
namespace YooAsset
{
/// <summary>
/// WebGL平台加载器
/// </summary>
internal class RawBundleWebLoader : BundleLoaderBase
{
private enum ESteps
{
None,
Download,
CheckDownload,
Website,
CheckWebsite,
CheckFile,
Done,
}
private ESteps _steps = ESteps.None;
private DownloaderBase _website;
private DownloaderBase _downloader;
public RawBundleWebLoader(AssetSystemImpl impl, BundleInfo bundleInfo) : base(impl, bundleInfo)
{
}
/// <summary>
/// 轮询更新
/// </summary>
public override void Update()
{
if (_steps == ESteps.Done)
return;
if (_steps == ESteps.None)
{
if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromRemote)
{
_steps = ESteps.Download;
FileLoadPath = MainBundleInfo.Bundle.CachedDataFilePath;
}
else if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromStreaming)
{
_steps = ESteps.Website;
FileLoadPath = MainBundleInfo.Bundle.CachedDataFilePath;
}
else if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromCache)
{
_steps = ESteps.CheckFile;
FileLoadPath = MainBundleInfo.Bundle.CachedDataFilePath;
}
else
{
throw new System.NotImplementedException(MainBundleInfo.LoadMode.ToString());
}
}
// 1. 下载远端文件
if (_steps == ESteps.Download)
{
int failedTryAgain = int.MaxValue;
_downloader = DownloadSystem.BeginDownload(MainBundleInfo, failedTryAgain);
_steps = ESteps.CheckDownload;
}
// 2. 检测下载结果
if (_steps == ESteps.CheckDownload)
{
DownloadProgress = _downloader.DownloadProgress;
DownloadedBytes = _downloader.DownloadedBytes;
if (_downloader.IsDone() == false)
return;
if (_downloader.HasError())
{
_steps = ESteps.Done;
Status = EStatus.Failed;
LastError = _downloader.GetLastError();
}
else
{
_steps = ESteps.CheckFile;
}
}
// 3. 从站点下载
if (_steps == ESteps.Website)
{
int failedTryAgain = 1;
var bundleInfo = PatchManifestTools.GetUnpackInfo(MainBundleInfo.Bundle);
_website = DownloadSystem.BeginDownload(bundleInfo, failedTryAgain);
_steps = ESteps.CheckWebsite;
}
// 4. 检测站点下载
if (_steps == ESteps.CheckWebsite)
{
DownloadProgress = _website.DownloadProgress;
DownloadedBytes = _website.DownloadedBytes;
if (_website.IsDone() == false)
return;
if (_website.HasError())
{
_steps = ESteps.Done;
Status = EStatus.Failed;
LastError = _website.GetLastError();
}
else
{
_steps = ESteps.CheckFile;
}
}
// 5. 检测结果
if (_steps == ESteps.CheckFile)
{
// 设置下载进度
DownloadProgress = 1f;
DownloadedBytes = (ulong)MainBundleInfo.Bundle.FileSize;
_steps = ESteps.Done;
if (File.Exists(FileLoadPath))
{
Status = EStatus.Succeed;
}
else
{
Status = EStatus.Failed;
LastError = $"Raw file not found : {FileLoadPath}";
}
}
}
/// <summary>
/// 主线程等待异步操作完毕
/// </summary>
public override void WaitForAsyncComplete()
{
if (IsDone() == false)
{
Status = EStatus.Failed;
LastError = $"{nameof(WaitForAsyncComplete)} failed ! WebGL platform not support sync load method !";
YooLogger.Error(LastError);
}
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 1d16b689f73611e44bd01a4cc429a6ac
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: