Update AssetSystem

HostPlayMode支持WEBGL
pull/40/head
hevinci 2022-09-02 16:28:19 +08:00
parent 7e20c39fb3
commit 8d2779b446
3 changed files with 140 additions and 37 deletions

View File

@ -14,7 +14,7 @@ namespace YooAsset
Download,
CheckDownload,
LoadFile,
CheckFile,
CheckLoadFile,
Done,
}
@ -23,7 +23,7 @@ namespace YooAsset
private bool _isWaitForAsyncComplete = false;
private bool _isShowWaitForAsyncError = false;
private DownloaderBase _downloader;
private AssetBundleCreateRequest _cacheRequest;
private AssetBundleCreateRequest _createRequest;
public AssetBundleFileLoader(BundleInfo bundleInfo) : base(bundleInfo)
@ -115,34 +115,34 @@ namespace YooAsset
if (_isWaitForAsyncComplete)
CacheBundle = AssetBundle.LoadFromFile(_fileLoadPath, 0, offset);
else
_cacheRequest = AssetBundle.LoadFromFileAsync(_fileLoadPath, 0, offset);
_createRequest = AssetBundle.LoadFromFileAsync(_fileLoadPath, 0, offset);
}
else
{
if (_isWaitForAsyncComplete)
CacheBundle = AssetBundle.LoadFromFile(_fileLoadPath);
else
_cacheRequest = AssetBundle.LoadFromFileAsync(_fileLoadPath);
_createRequest = AssetBundle.LoadFromFileAsync(_fileLoadPath);
}
_steps = ESteps.CheckFile;
_steps = ESteps.CheckLoadFile;
}
// 4. 检测AssetBundle加载结果
if (_steps == ESteps.CheckFile)
if (_steps == ESteps.CheckLoadFile)
{
if (_cacheRequest != null)
if (_createRequest != null)
{
if (_isWaitForAsyncComplete)
{
// 强制挂起主线程(注意:该操作会很耗时)
YooLogger.Warning("Suspend the main thread to load unity bundle.");
CacheBundle = _cacheRequest.assetBundle;
CacheBundle = _createRequest.assetBundle;
}
else
{
if (_cacheRequest.isDone == false)
if (_createRequest.isDone == false)
return;
CacheBundle = _cacheRequest.assetBundle;
CacheBundle = _createRequest.assetBundle;
}
}

View File

@ -1,4 +1,5 @@
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
@ -11,17 +12,23 @@ namespace YooAsset
private enum ESteps
{
None = 0,
LoadFile,
CheckFile,
TryLoad,
Download,
CheckDownload,
LoadCacheFile,
CheckLoadCacheFile,
LoadWebFile,
CheckLoadWebFile,
TryLoadWebFile,
Done,
}
private ESteps _steps = ESteps.None;
private float _tryTimer = 0;
private string _webURL;
private string _fileLoadPath;
private bool _isShowWaitForAsyncError = false;
private DownloaderBase _downloader;
private UnityWebRequest _webRequest;
private AssetBundleCreateRequest _createRequest;
public AssetBundleWebLoader(BundleInfo bundleInfo) : base(bundleInfo)
@ -40,18 +47,18 @@ namespace YooAsset
{
if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromRemote)
{
_steps = ESteps.LoadFile;
_webURL = MainBundleInfo.RemoteMainURL;
}
else if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromCache)
{
_steps = ESteps.LoadFile;
_webURL = MainBundleInfo.Bundle.CachedFilePath;
_steps = ESteps.Download;
_fileLoadPath = MainBundleInfo.Bundle.CachedFilePath;
}
else if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromStreaming)
{
_steps = ESteps.LoadFile;
_webURL = MainBundleInfo.Bundle.StreamingFilePath;
_steps = ESteps.LoadWebFile;
_fileLoadPath = MainBundleInfo.Bundle.StreamingFilePath;
}
else if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromCache)
{
_steps = ESteps.LoadCacheFile;
_fileLoadPath = MainBundleInfo.Bundle.CachedFilePath;
}
else
{
@ -59,16 +66,112 @@ namespace YooAsset
}
}
// 1. 从服务器或缓存中获取AssetBundle文件
if (_steps == ESteps.LoadFile)
// 1. 从服务器下载
if (_steps == ESteps.Download)
{
_webRequest = UnityWebRequestAssetBundle.GetAssetBundle(_webURL, Hash128.Parse(MainBundleInfo.Bundle.FileHash));
_webRequest.SendWebRequest();
_steps = ESteps.CheckFile;
int failedTryAgain = int.MaxValue;
_downloader = DownloadSystem.BeginDownload(MainBundleInfo, failedTryAgain);
_steps = ESteps.CheckDownload;
}
// 2. 检测获取的AssetBundle文件
if (_steps == ESteps.CheckFile)
// 2. 检测服务器下载结果
if (_steps == ESteps.CheckDownload)
{
if (_downloader.IsDone() == false)
return;
if (_downloader.HasError())
{
_steps = ESteps.Done;
Status = EStatus.Failed;
LastError = _downloader.GetLastError();
}
else
{
_steps = ESteps.LoadCacheFile;
}
}
// 3. 从本地缓存里加载AssetBundle
if (_steps == ESteps.LoadCacheFile)
{
#if UNITY_EDITOR
// 注意Unity2017.4编辑器模式下如果AssetBundle文件不存在会导致编辑器崩溃这里做了预判。
if (System.IO.File.Exists(_fileLoadPath) == false)
{
_steps = ESteps.Done;
Status = EStatus.Failed;
LastError = $"Not found assetBundle file : {_fileLoadPath}";
YooLogger.Error(LastError);
return;
}
#endif
// Load assetBundle file
if (MainBundleInfo.Bundle.IsEncrypted)
{
if (AssetSystem.DecryptionServices == null)
throw new Exception($"{nameof(AssetBundleFileLoader)} need {nameof(IDecryptionServices)} : {MainBundleInfo.Bundle.BundleName}");
DecryptionFileInfo fileInfo = new DecryptionFileInfo();
fileInfo.BundleName = MainBundleInfo.Bundle.BundleName;
fileInfo.FileHash = MainBundleInfo.Bundle.FileHash;
ulong offset = AssetSystem.DecryptionServices.GetFileOffset(fileInfo);
_createRequest = AssetBundle.LoadFromFileAsync(_fileLoadPath, 0, offset);
}
else
{
_createRequest = AssetBundle.LoadFromFileAsync(_fileLoadPath);
}
_steps = ESteps.CheckLoadCacheFile;
}
// 4. 检测AssetBundle加载结果
if (_steps == ESteps.CheckLoadCacheFile)
{
if (_createRequest.isDone == false)
return;
CacheBundle = _createRequest.assetBundle;
if (CacheBundle == null)
{
_steps = ESteps.Done;
Status = EStatus.Failed;
LastError = $"Failed to load AssetBundle file : {MainBundleInfo.Bundle.BundleName}";
YooLogger.Error(LastError);
// 注意当缓存文件的校验等级为Low的时候并不能保证缓存文件的完整性。
// 在AssetBundle文件加载失败的情况下我们需要重新验证文件的完整性
if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromCache)
{
string cacheLoadPath = MainBundleInfo.Bundle.CachedFilePath;
if (CacheSystem.VerifyBundle(MainBundleInfo.Bundle, EVerifyLevel.High) != EVerifyResult.Succeed)
{
if (File.Exists(cacheLoadPath))
{
YooLogger.Error($"Delete the invalid cache file : {cacheLoadPath}");
File.Delete(cacheLoadPath);
}
}
}
}
else
{
_steps = ESteps.Done;
Status = EStatus.Succeed;
}
}
// 5. 从WEB网站获取AssetBundle文件
if (_steps == ESteps.LoadWebFile)
{
_webRequest = UnityWebRequestAssetBundle.GetAssetBundle(_fileLoadPath, Hash128.Parse(MainBundleInfo.Bundle.FileHash));
_webRequest.SendWebRequest();
_steps = ESteps.CheckLoadWebFile;
}
// 6. 检测AssetBundle加载结果
if (_steps == ESteps.CheckLoadWebFile)
{
if (_webRequest.isDone == false)
return;
@ -79,8 +182,8 @@ namespace YooAsset
if (_webRequest.isNetworkError || _webRequest.isHttpError)
#endif
{
YooLogger.Warning($"Failed to get asset bundle form web : {_webURL} Error : {_webRequest.error}");
_steps = ESteps.TryLoad;
YooLogger.Warning($"Failed to get asset bundle from web : {_fileLoadPath} Error : {_webRequest.error}");
_steps = ESteps.TryLoadWebFile;
_tryTimer = 0;
}
else
@ -101,15 +204,15 @@ namespace YooAsset
}
}
// 3. 如果获取失败,重新尝试
if (_steps == ESteps.TryLoad)
// 7. 如果获取失败,重新尝试
if (_steps == ESteps.TryLoadWebFile)
{
_tryTimer += Time.unscaledDeltaTime;
if (_tryTimer > 1f)
{
_webRequest.Dispose();
_webRequest = null;
_steps = ESteps.LoadFile;
_steps = ESteps.LoadWebFile;
}
}
}

View File

@ -57,7 +57,7 @@ namespace YooAsset
}
/// <summary>
/// 获取网络资源加载路径
/// 获取WWW加载本地资源的路径
/// </summary>
public static string ConvertToWWWPath(string path)
{