mirror of https://github.com/tuyoogame/YooAsset
parent
7e20c39fb3
commit
8d2779b446
|
@ -14,7 +14,7 @@ namespace YooAsset
|
||||||
Download,
|
Download,
|
||||||
CheckDownload,
|
CheckDownload,
|
||||||
LoadFile,
|
LoadFile,
|
||||||
CheckFile,
|
CheckLoadFile,
|
||||||
Done,
|
Done,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -23,7 +23,7 @@ namespace YooAsset
|
||||||
private bool _isWaitForAsyncComplete = false;
|
private bool _isWaitForAsyncComplete = false;
|
||||||
private bool _isShowWaitForAsyncError = false;
|
private bool _isShowWaitForAsyncError = false;
|
||||||
private DownloaderBase _downloader;
|
private DownloaderBase _downloader;
|
||||||
private AssetBundleCreateRequest _cacheRequest;
|
private AssetBundleCreateRequest _createRequest;
|
||||||
|
|
||||||
|
|
||||||
public AssetBundleFileLoader(BundleInfo bundleInfo) : base(bundleInfo)
|
public AssetBundleFileLoader(BundleInfo bundleInfo) : base(bundleInfo)
|
||||||
|
@ -115,34 +115,34 @@ namespace YooAsset
|
||||||
if (_isWaitForAsyncComplete)
|
if (_isWaitForAsyncComplete)
|
||||||
CacheBundle = AssetBundle.LoadFromFile(_fileLoadPath, 0, offset);
|
CacheBundle = AssetBundle.LoadFromFile(_fileLoadPath, 0, offset);
|
||||||
else
|
else
|
||||||
_cacheRequest = AssetBundle.LoadFromFileAsync(_fileLoadPath, 0, offset);
|
_createRequest = AssetBundle.LoadFromFileAsync(_fileLoadPath, 0, offset);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (_isWaitForAsyncComplete)
|
if (_isWaitForAsyncComplete)
|
||||||
CacheBundle = AssetBundle.LoadFromFile(_fileLoadPath);
|
CacheBundle = AssetBundle.LoadFromFile(_fileLoadPath);
|
||||||
else
|
else
|
||||||
_cacheRequest = AssetBundle.LoadFromFileAsync(_fileLoadPath);
|
_createRequest = AssetBundle.LoadFromFileAsync(_fileLoadPath);
|
||||||
}
|
}
|
||||||
_steps = ESteps.CheckFile;
|
_steps = ESteps.CheckLoadFile;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 4. 检测AssetBundle加载结果
|
// 4. 检测AssetBundle加载结果
|
||||||
if (_steps == ESteps.CheckFile)
|
if (_steps == ESteps.CheckLoadFile)
|
||||||
{
|
{
|
||||||
if (_cacheRequest != null)
|
if (_createRequest != null)
|
||||||
{
|
{
|
||||||
if (_isWaitForAsyncComplete)
|
if (_isWaitForAsyncComplete)
|
||||||
{
|
{
|
||||||
// 强制挂起主线程(注意:该操作会很耗时)
|
// 强制挂起主线程(注意:该操作会很耗时)
|
||||||
YooLogger.Warning("Suspend the main thread to load unity bundle.");
|
YooLogger.Warning("Suspend the main thread to load unity bundle.");
|
||||||
CacheBundle = _cacheRequest.assetBundle;
|
CacheBundle = _createRequest.assetBundle;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (_cacheRequest.isDone == false)
|
if (_createRequest.isDone == false)
|
||||||
return;
|
return;
|
||||||
CacheBundle = _cacheRequest.assetBundle;
|
CacheBundle = _createRequest.assetBundle;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
using System;
|
using System;
|
||||||
|
using System.IO;
|
||||||
using System.Collections;
|
using System.Collections;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
@ -11,19 +12,25 @@ namespace YooAsset
|
||||||
private enum ESteps
|
private enum ESteps
|
||||||
{
|
{
|
||||||
None = 0,
|
None = 0,
|
||||||
LoadFile,
|
Download,
|
||||||
CheckFile,
|
CheckDownload,
|
||||||
TryLoad,
|
LoadCacheFile,
|
||||||
|
CheckLoadCacheFile,
|
||||||
|
LoadWebFile,
|
||||||
|
CheckLoadWebFile,
|
||||||
|
TryLoadWebFile,
|
||||||
Done,
|
Done,
|
||||||
}
|
}
|
||||||
|
|
||||||
private ESteps _steps = ESteps.None;
|
private ESteps _steps = ESteps.None;
|
||||||
private float _tryTimer = 0;
|
private float _tryTimer = 0;
|
||||||
private string _webURL;
|
private string _fileLoadPath;
|
||||||
private bool _isShowWaitForAsyncError = false;
|
private bool _isShowWaitForAsyncError = false;
|
||||||
|
private DownloaderBase _downloader;
|
||||||
private UnityWebRequest _webRequest;
|
private UnityWebRequest _webRequest;
|
||||||
|
private AssetBundleCreateRequest _createRequest;
|
||||||
|
|
||||||
|
|
||||||
public AssetBundleWebLoader(BundleInfo bundleInfo) : base(bundleInfo)
|
public AssetBundleWebLoader(BundleInfo bundleInfo) : base(bundleInfo)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
@ -40,18 +47,18 @@ namespace YooAsset
|
||||||
{
|
{
|
||||||
if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromRemote)
|
if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromRemote)
|
||||||
{
|
{
|
||||||
_steps = ESteps.LoadFile;
|
_steps = ESteps.Download;
|
||||||
_webURL = MainBundleInfo.RemoteMainURL;
|
_fileLoadPath = MainBundleInfo.Bundle.CachedFilePath;
|
||||||
}
|
|
||||||
else if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromCache)
|
|
||||||
{
|
|
||||||
_steps = ESteps.LoadFile;
|
|
||||||
_webURL = MainBundleInfo.Bundle.CachedFilePath;
|
|
||||||
}
|
}
|
||||||
else if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromStreaming)
|
else if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromStreaming)
|
||||||
{
|
{
|
||||||
_steps = ESteps.LoadFile;
|
_steps = ESteps.LoadWebFile;
|
||||||
_webURL = MainBundleInfo.Bundle.StreamingFilePath;
|
_fileLoadPath = MainBundleInfo.Bundle.StreamingFilePath;
|
||||||
|
}
|
||||||
|
else if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromCache)
|
||||||
|
{
|
||||||
|
_steps = ESteps.LoadCacheFile;
|
||||||
|
_fileLoadPath = MainBundleInfo.Bundle.CachedFilePath;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -59,16 +66,112 @@ namespace YooAsset
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 1. 从服务器或缓存中获取AssetBundle文件
|
// 1. 从服务器下载
|
||||||
if (_steps == ESteps.LoadFile)
|
if (_steps == ESteps.Download)
|
||||||
{
|
{
|
||||||
_webRequest = UnityWebRequestAssetBundle.GetAssetBundle(_webURL, Hash128.Parse(MainBundleInfo.Bundle.FileHash));
|
int failedTryAgain = int.MaxValue;
|
||||||
_webRequest.SendWebRequest();
|
_downloader = DownloadSystem.BeginDownload(MainBundleInfo, failedTryAgain);
|
||||||
_steps = ESteps.CheckFile;
|
_steps = ESteps.CheckDownload;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. 检测获取的AssetBundle文件
|
// 2. 检测服务器下载结果
|
||||||
if (_steps == ESteps.CheckFile)
|
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)
|
if (_webRequest.isDone == false)
|
||||||
return;
|
return;
|
||||||
|
@ -79,8 +182,8 @@ namespace YooAsset
|
||||||
if (_webRequest.isNetworkError || _webRequest.isHttpError)
|
if (_webRequest.isNetworkError || _webRequest.isHttpError)
|
||||||
#endif
|
#endif
|
||||||
{
|
{
|
||||||
YooLogger.Warning($"Failed to get asset bundle form web : {_webURL} Error : {_webRequest.error}");
|
YooLogger.Warning($"Failed to get asset bundle from web : {_fileLoadPath} Error : {_webRequest.error}");
|
||||||
_steps = ESteps.TryLoad;
|
_steps = ESteps.TryLoadWebFile;
|
||||||
_tryTimer = 0;
|
_tryTimer = 0;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -101,15 +204,15 @@ namespace YooAsset
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3. 如果获取失败,重新尝试
|
// 7. 如果获取失败,重新尝试
|
||||||
if (_steps == ESteps.TryLoad)
|
if (_steps == ESteps.TryLoadWebFile)
|
||||||
{
|
{
|
||||||
_tryTimer += Time.unscaledDeltaTime;
|
_tryTimer += Time.unscaledDeltaTime;
|
||||||
if (_tryTimer > 1f)
|
if (_tryTimer > 1f)
|
||||||
{
|
{
|
||||||
_webRequest.Dispose();
|
_webRequest.Dispose();
|
||||||
_webRequest = null;
|
_webRequest = null;
|
||||||
_steps = ESteps.LoadFile;
|
_steps = ESteps.LoadWebFile;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -57,7 +57,7 @@ namespace YooAsset
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 获取网络资源加载路径
|
/// 获取WWW加载本地资源的路径
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static string ConvertToWWWPath(string path)
|
public static string ConvertToWWWPath(string path)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue