update web file system

pull/326/head
何冠峰 2024-07-04 21:59:24 +08:00
parent d43eb821b9
commit 9420f8561f
4 changed files with 49 additions and 21 deletions

View File

@ -57,7 +57,7 @@ namespace YooAsset
// 注意:在连续时间段内无新增下载数据及判定为超时 // 注意:在连续时间段内无新增下载数据及判定为超时
if (_isAbort == false) if (_isAbort == false)
{ {
if (_latestDownloadBytes != _webRequest.downloadedBytes) if ( _latestDownloadBytes != _webRequest.downloadedBytes)
{ {
_latestDownloadBytes = _webRequest.downloadedBytes; _latestDownloadBytes = _webRequest.downloadedBytes;
_latestDownloadRealtime = Time.realtimeSinceStartup; _latestDownloadRealtime = Time.realtimeSinceStartup;

View File

@ -61,12 +61,20 @@ namespace YooAsset
} }
#region 自定义参数 #region 自定义参数
public bool AllowCrossAccess { private set; get; } = false;
/// <summary> /// <summary>
/// 自定义参数:远程服务接口 /// 自定义参数:远程服务接口
/// </summary> /// </summary>
public IRemoteServices RemoteServices { private set; get; } = null; public IRemoteServices RemoteServices { private set; get; } = null;
/// <summary>
/// 禁用Unity的网络缓存
/// </summary>
public bool DisableUnityWebCache { private set; get; } = false;
/// <summary>
/// 允许跨域访问和下载
/// </summary>
public bool AllowCrossAccess { private set; get; } = false;
#endregion #endregion
@ -124,14 +132,18 @@ namespace YooAsset
public virtual void SetParameter(string name, object value) public virtual void SetParameter(string name, object value)
{ {
if (name == "ALLOW_CROSS_ACCESS") if (name == "REMOTE_SERVICES")
{
AllowCrossAccess = (bool)value;
}
else if (name == "REMOTE_SERVICES")
{ {
RemoteServices = (IRemoteServices)value; RemoteServices = (IRemoteServices)value;
} }
else if (name == "DISABLE_UNITY_WEB_CACHE")
{
DisableUnityWebCache = (bool)value;
}
else if (name == "ALLOW_CROSS_ACCESS")
{
AllowCrossAccess = (bool)value;
}
else else
{ {
YooLogger.Warning($"Invalid parameter : {name}"); YooLogger.Warning($"Invalid parameter : {name}");

View File

@ -110,20 +110,34 @@ namespace YooAsset
private void CreateWebRequest() private void CreateWebRequest()
{ {
uint unityCRC = Bundle.UnityCRC;
Hash128 fileHash = Hash128.Parse(Bundle.FileHash);
// 注意:优先从浏览器缓存里获取文件
// The file hash defining the version of the asset bundle.
_webRequest = DownloadSystemHelper.NewUnityWebRequestGet(_requestURL); _webRequest = DownloadSystemHelper.NewUnityWebRequestGet(_requestURL);
_downloadhandler = new DownloadHandlerAssetBundle(_requestURL, fileHash, unityCRC); _webRequest.downloadHandler = CreateDownloadHandler();
#if UNITY_2020_3_OR_NEWER
_downloadhandler.autoLoadAssetBundle = false;
#endif
_webRequest.downloadHandler = _downloadhandler;
_webRequest.disposeDownloadHandlerOnDispose = true; _webRequest.disposeDownloadHandlerOnDispose = true;
_webRequest.SendWebRequest(); _webRequest.SendWebRequest();
} }
private DownloadHandlerAssetBundle CreateDownloadHandler()
{
if (_fileSystem.DisableUnityWebCache)
{
var downloadhandler = new DownloadHandlerAssetBundle(_requestURL, 0);
#if UNITY_2020_3_OR_NEWER
downloadhandler.autoLoadAssetBundle = false;
#endif
return downloadhandler;
}
else
{
// 注意:优先从浏览器缓存里获取文件
// The file hash defining the version of the asset bundle.
uint unityCRC = Bundle.UnityCRC;
Hash128 fileHash = Hash128.Parse(Bundle.FileHash);
var downloadhandler = new DownloadHandlerAssetBundle(_requestURL, fileHash, unityCRC);
#if UNITY_2020_3_OR_NEWER
downloadhandler.autoLoadAssetBundle = false;
#endif
return downloadhandler;
}
}
private void DisposeWebRequest() private void DisposeWebRequest()
{ {
if (_webRequest != null) if (_webRequest != null)

View File

@ -160,10 +160,11 @@ namespace YooAsset
/// <summary> /// <summary>
/// 创建默认的Web文件系统参数 /// 创建默认的Web文件系统参数
/// </summary> /// </summary>
public static FileSystemParameters CreateDefaultWebFileSystemParameters() public static FileSystemParameters CreateDefaultWebFileSystemParameters(bool disableUnityWebCache = false)
{ {
string fileSystemClass = typeof(DefaultWebFileSystem).FullName; string fileSystemClass = typeof(DefaultWebFileSystem).FullName;
var fileSystemParams = new FileSystemParameters(fileSystemClass, null); var fileSystemParams = new FileSystemParameters(fileSystemClass, null);
fileSystemParams.AddParameter("DISABLE_UNITY_WEB_CACHE", disableUnityWebCache);
fileSystemParams.AddParameter("ALLOW_CROSS_ACCESS", false); fileSystemParams.AddParameter("ALLOW_CROSS_ACCESS", false);
return fileSystemParams; return fileSystemParams;
} }
@ -172,12 +173,13 @@ namespace YooAsset
/// 创建默认的Web文件系统参数 /// 创建默认的Web文件系统参数
/// </summary> /// </summary>
/// <param name="remoteServices">远端资源地址查询服务类</param> /// <param name="remoteServices">远端资源地址查询服务类</param>
public static FileSystemParameters CreateDefaultWebFileSystemParameters(IRemoteServices remoteServices) public static FileSystemParameters CreateDefaultWebFileSystemParameters(IRemoteServices remoteServices, bool disableUnityWebCache = false)
{ {
string fileSystemClass = typeof(DefaultWebFileSystem).FullName; string fileSystemClass = typeof(DefaultWebFileSystem).FullName;
var fileSystemParams = new FileSystemParameters(fileSystemClass, null); var fileSystemParams = new FileSystemParameters(fileSystemClass, null);
fileSystemParams.AddParameter("ALLOW_CROSS_ACCESS", true);
fileSystemParams.AddParameter("REMOTE_SERVICES", remoteServices); fileSystemParams.AddParameter("REMOTE_SERVICES", remoteServices);
fileSystemParams.AddParameter("DISABLE_UNITY_WEB_CACHE", disableUnityWebCache);
fileSystemParams.AddParameter("ALLOW_CROSS_ACCESS", true);
return fileSystemParams; return fileSystemParams;
} }
} }