2022-03-01 10:44:12 +08:00
|
|
|
|
using System;
|
2022-05-05 11:44:03 +08:00
|
|
|
|
using System.Diagnostics;
|
2022-03-01 10:44:12 +08:00
|
|
|
|
using System.Collections;
|
|
|
|
|
|
using System.Collections.Generic;
|
2022-09-29 18:40:43 +08:00
|
|
|
|
using UnityEngine;
|
2022-03-01 10:44:12 +08:00
|
|
|
|
|
|
|
|
|
|
namespace YooAsset
|
|
|
|
|
|
{
|
2022-08-12 17:34:12 +08:00
|
|
|
|
public static partial class YooAssets
|
2022-03-01 10:44:12 +08:00
|
|
|
|
{
|
2022-03-03 18:07:58 +08:00
|
|
|
|
private static bool _isInitialize = false;
|
2022-11-26 16:43:05 +08:00
|
|
|
|
private static GameObject _driver = null;
|
2022-10-14 11:49:32 +08:00
|
|
|
|
private static readonly List<AssetsPackage> _packages = new List<AssetsPackage>();
|
2022-06-20 15:38:55 +08:00
|
|
|
|
|
2022-03-01 10:44:12 +08:00
|
|
|
|
/// <summary>
|
2022-10-08 12:09:34 +08:00
|
|
|
|
/// 初始化资源系统
|
2022-03-01 10:44:12 +08:00
|
|
|
|
/// </summary>
|
2022-09-29 18:40:43 +08:00
|
|
|
|
public static void Initialize()
|
2022-03-01 10:44:12 +08:00
|
|
|
|
{
|
2022-09-28 11:55:12 +08:00
|
|
|
|
if (_isInitialize)
|
2022-10-12 15:01:39 +08:00
|
|
|
|
throw new Exception($"{nameof(YooAssets)} is initialized !");
|
2022-03-01 10:44:12 +08:00
|
|
|
|
|
2022-09-28 11:55:12 +08:00
|
|
|
|
if (_isInitialize == false)
|
|
|
|
|
|
{
|
2022-10-12 15:01:39 +08:00
|
|
|
|
// 创建驱动器
|
2022-09-28 11:55:12 +08:00
|
|
|
|
_isInitialize = true;
|
2022-11-26 16:43:05 +08:00
|
|
|
|
_driver = new UnityEngine.GameObject($"[{nameof(YooAssets)}]");
|
|
|
|
|
|
_driver.AddComponent<YooAssetsDriver>();
|
|
|
|
|
|
UnityEngine.Object.DontDestroyOnLoad(_driver);
|
2022-11-26 17:39:35 +08:00
|
|
|
|
YooLogger.Log($"{nameof(YooAssets)} initialize !");
|
2022-09-28 11:55:12 +08:00
|
|
|
|
|
|
|
|
|
|
#if DEBUG
|
2022-10-12 15:01:39 +08:00
|
|
|
|
// 添加远程调试脚本
|
2022-11-26 16:43:05 +08:00
|
|
|
|
_driver.AddComponent<RemoteDebuggerInRuntime>();
|
2022-09-28 11:55:12 +08:00
|
|
|
|
#endif
|
2022-04-04 22:43:47 +08:00
|
|
|
|
|
2022-09-29 18:40:43 +08:00
|
|
|
|
// 初始化异步系统
|
|
|
|
|
|
OperationSystem.Initialize();
|
2022-03-01 10:44:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2022-11-26 16:43:05 +08:00
|
|
|
|
/// 销毁资源系统
|
2022-04-12 19:15:44 +08:00
|
|
|
|
/// </summary>
|
2022-11-26 16:43:05 +08:00
|
|
|
|
public static void Destroy()
|
2022-05-03 12:23:08 +08:00
|
|
|
|
{
|
2022-05-11 16:48:17 +08:00
|
|
|
|
if (_isInitialize)
|
|
|
|
|
|
{
|
2022-11-26 16:43:05 +08:00
|
|
|
|
OperationSystem.DestroyAll();
|
|
|
|
|
|
DownloadSystem.DestroyAll();
|
|
|
|
|
|
CacheSystem.ClearAll();
|
2022-07-05 20:07:32 +08:00
|
|
|
|
|
2022-11-26 16:43:05 +08:00
|
|
|
|
foreach (var package in _packages)
|
2022-09-29 18:40:43 +08:00
|
|
|
|
{
|
2022-11-26 16:43:05 +08:00
|
|
|
|
package.DestroyPackage();
|
2022-09-29 18:40:43 +08:00
|
|
|
|
}
|
2022-11-26 16:43:05 +08:00
|
|
|
|
_packages.Clear();
|
|
|
|
|
|
|
|
|
|
|
|
_isInitialize = false;
|
2022-11-26 17:39:35 +08:00
|
|
|
|
if (_driver != null)
|
|
|
|
|
|
GameObject.Destroy(_driver);
|
|
|
|
|
|
YooLogger.Log($"{nameof(YooAssets)} destroy all !");
|
2022-05-06 23:15:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-07-20 10:57:56 +08:00
|
|
|
|
/// <summary>
|
2022-11-26 16:43:05 +08:00
|
|
|
|
/// 更新资源系统
|
2022-07-20 10:57:56 +08:00
|
|
|
|
/// </summary>
|
2022-11-26 16:43:05 +08:00
|
|
|
|
internal static void Update()
|
2022-07-20 10:57:56 +08:00
|
|
|
|
{
|
2022-09-29 18:40:43 +08:00
|
|
|
|
if (_isInitialize)
|
2022-09-05 15:31:26 +08:00
|
|
|
|
{
|
2022-11-26 16:43:05 +08:00
|
|
|
|
OperationSystem.Update();
|
|
|
|
|
|
DownloadSystem.Update();
|
2022-03-24 00:20:37 +08:00
|
|
|
|
|
2022-11-26 16:43:05 +08:00
|
|
|
|
for (int i = 0; i < _packages.Count; i++)
|
2022-09-29 18:40:43 +08:00
|
|
|
|
{
|
2022-11-26 16:43:05 +08:00
|
|
|
|
_packages[i].UpdatePackage();
|
2022-09-29 18:40:43 +08:00
|
|
|
|
}
|
2022-09-05 15:31:26 +08:00
|
|
|
|
}
|
2022-03-01 10:44:12 +08:00
|
|
|
|
}
|
2022-05-06 23:15:03 +08:00
|
|
|
|
|
2022-03-01 10:44:12 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2022-09-29 18:40:43 +08:00
|
|
|
|
/// 创建资源包
|
2022-03-01 10:44:12 +08:00
|
|
|
|
/// </summary>
|
2022-09-29 18:40:43 +08:00
|
|
|
|
/// <param name="packageName">资源包名称</param>
|
2022-10-14 11:49:32 +08:00
|
|
|
|
public static AssetsPackage CreateAssetsPackage(string packageName)
|
2022-03-01 10:44:12 +08:00
|
|
|
|
{
|
2022-09-29 18:40:43 +08:00
|
|
|
|
if (_isInitialize == false)
|
2022-10-12 15:01:39 +08:00
|
|
|
|
throw new Exception($"{nameof(YooAssets)} not initialize !");
|
2022-05-01 15:26:48 +08:00
|
|
|
|
|
2022-09-29 18:40:43 +08:00
|
|
|
|
if (string.IsNullOrEmpty(packageName))
|
2022-10-14 11:49:32 +08:00
|
|
|
|
throw new Exception("Package name is null or empty !");
|
2022-03-01 10:44:12 +08:00
|
|
|
|
|
2022-10-14 11:49:32 +08:00
|
|
|
|
if (HasAssetsPackage(packageName))
|
2022-09-29 18:40:43 +08:00
|
|
|
|
throw new Exception($"Package {packageName} already existed !");
|
2022-05-03 12:23:08 +08:00
|
|
|
|
|
2022-10-14 11:49:32 +08:00
|
|
|
|
AssetsPackage assetsPackage = new AssetsPackage(packageName);
|
|
|
|
|
|
_packages.Add(assetsPackage);
|
|
|
|
|
|
return assetsPackage;
|
2022-03-01 10:44:12 +08:00
|
|
|
|
}
|
2022-05-03 12:23:08 +08:00
|
|
|
|
|
2022-10-12 15:01:39 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取资源包
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="packageName">资源包名称</param>
|
2022-10-14 11:49:32 +08:00
|
|
|
|
public static AssetsPackage GetAssetsPackage(string packageName)
|
2022-11-29 11:06:24 +08:00
|
|
|
|
{
|
|
|
|
|
|
var package = TryGetAssetsPackage(packageName);
|
|
|
|
|
|
if (package == null)
|
|
|
|
|
|
YooLogger.Warning($"Not found assets package : {packageName}");
|
|
|
|
|
|
return package;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 尝试获取资源包
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="packageName">资源包名称</param>
|
|
|
|
|
|
public static AssetsPackage TryGetAssetsPackage(string packageName)
|
2022-10-12 15:01:39 +08:00
|
|
|
|
{
|
|
|
|
|
|
if (_isInitialize == false)
|
|
|
|
|
|
throw new Exception($"{nameof(YooAssets)} not initialize !");
|
|
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(packageName))
|
|
|
|
|
|
throw new Exception("Package name is null or empty !");
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var package in _packages)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (package.PackageName == packageName)
|
|
|
|
|
|
return package;
|
|
|
|
|
|
}
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-05-03 12:23:08 +08:00
|
|
|
|
/// <summary>
|
2022-09-29 18:40:43 +08:00
|
|
|
|
/// 检测资源包是否存在
|
2022-05-03 12:23:08 +08:00
|
|
|
|
/// </summary>
|
2022-09-29 18:40:43 +08:00
|
|
|
|
/// <param name="packageName">资源包名称</param>
|
2022-10-14 11:49:32 +08:00
|
|
|
|
public static bool HasAssetsPackage(string packageName)
|
2022-05-03 12:23:08 +08:00
|
|
|
|
{
|
2022-10-12 15:01:39 +08:00
|
|
|
|
if (_isInitialize == false)
|
|
|
|
|
|
throw new Exception($"{nameof(YooAssets)} not initialize !");
|
|
|
|
|
|
|
2022-09-29 18:40:43 +08:00
|
|
|
|
foreach (var package in _packages)
|
2022-05-03 12:23:08 +08:00
|
|
|
|
{
|
2022-09-29 18:40:43 +08:00
|
|
|
|
if (package.PackageName == packageName)
|
|
|
|
|
|
return true;
|
2022-05-03 12:23:08 +08:00
|
|
|
|
}
|
2022-09-29 18:40:43 +08:00
|
|
|
|
return false;
|
2022-05-03 12:23:08 +08:00
|
|
|
|
}
|
2022-10-22 15:38:51 +08:00
|
|
|
|
|
2022-03-07 20:13:39 +08:00
|
|
|
|
/// <summary>
|
2022-09-29 18:40:43 +08:00
|
|
|
|
/// 开启一个异步操作
|
2022-03-07 20:13:39 +08:00
|
|
|
|
/// </summary>
|
2022-09-29 18:40:43 +08:00
|
|
|
|
/// <param name="operation">异步操作对象</param>
|
|
|
|
|
|
public static void StartOperation(GameAsyncOperation operation)
|
2022-03-07 20:13:39 +08:00
|
|
|
|
{
|
2022-09-29 18:40:43 +08:00
|
|
|
|
OperationSystem.StartOperation(operation);
|
2022-03-07 20:13:39 +08:00
|
|
|
|
}
|
2022-03-24 00:20:37 +08:00
|
|
|
|
|
2022-09-29 18:40:43 +08:00
|
|
|
|
#region 系统参数
|
2022-03-07 20:13:39 +08:00
|
|
|
|
/// <summary>
|
2022-10-19 18:18:32 +08:00
|
|
|
|
/// 设置下载系统参数,启用断点续传功能文件的最小字节数
|
2022-03-07 20:13:39 +08:00
|
|
|
|
/// </summary>
|
2022-09-29 18:40:43 +08:00
|
|
|
|
public static void SetDownloadSystemBreakpointResumeFileSize(int fileBytes)
|
2022-03-07 20:13:39 +08:00
|
|
|
|
{
|
2022-09-29 18:40:43 +08:00
|
|
|
|
DownloadSystem.BreakpointResumeFileSize = fileBytes;
|
2022-05-01 20:33:50 +08:00
|
|
|
|
}
|
2022-05-11 16:48:17 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2022-10-19 18:18:32 +08:00
|
|
|
|
/// 设置下载系统参数,下载失败后清理文件的HTTP错误码
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static void SetDownloadSystemClearFileResponseCode(List<long> codes)
|
|
|
|
|
|
{
|
|
|
|
|
|
DownloadSystem.ClearFileResponseCodes = codes;
|
|
|
|
|
|
}
|
2022-10-21 15:18:16 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 设置下载系统参数,自定义的证书认证实例
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static void SetDownloadSystemCertificateHandler(UnityEngine.Networking.CertificateHandler instance)
|
|
|
|
|
|
{
|
|
|
|
|
|
DownloadSystem.CertificateHandlerInstance = instance;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-02-24 19:45:05 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 设置下载系统参数,自定义下载请求
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static void SetDownloadSystemUnityWebRequest(DownloadRequestDelegate requestDelegate)
|
|
|
|
|
|
{
|
|
|
|
|
|
DownloadSystem.RequestDelegate = requestDelegate;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-10-19 18:18:32 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 设置异步系统参数,每帧执行消耗的最大时间切片(单位:毫秒)
|
2022-05-11 16:48:17 +08:00
|
|
|
|
/// </summary>
|
2022-09-29 18:40:43 +08:00
|
|
|
|
public static void SetOperationSystemMaxTimeSlice(long milliseconds)
|
2022-05-11 16:48:17 +08:00
|
|
|
|
{
|
2022-09-29 18:40:43 +08:00
|
|
|
|
if (milliseconds < 30)
|
2022-05-11 16:48:17 +08:00
|
|
|
|
{
|
2022-09-29 18:40:43 +08:00
|
|
|
|
milliseconds = 30;
|
|
|
|
|
|
YooLogger.Warning($"MaxTimeSlice minimum value is 30 milliseconds.");
|
2022-05-11 16:48:17 +08:00
|
|
|
|
}
|
2022-09-29 18:40:43 +08:00
|
|
|
|
OperationSystem.MaxTimeSlice = milliseconds;
|
2022-05-11 16:48:17 +08:00
|
|
|
|
}
|
2022-05-01 20:33:50 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2022-10-19 18:18:32 +08:00
|
|
|
|
/// 设置缓存系统参数,已经缓存文件的校验等级
|
2022-05-01 20:33:50 +08:00
|
|
|
|
/// </summary>
|
2022-09-29 18:40:43 +08:00
|
|
|
|
public static void SetCacheSystemCachedFileVerifyLevel(EVerifyLevel verifyLevel)
|
2022-05-01 20:33:50 +08:00
|
|
|
|
{
|
2022-09-29 18:40:43 +08:00
|
|
|
|
CacheSystem.InitVerifyLevel = verifyLevel;
|
2022-03-07 20:13:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
2022-03-01 10:44:12 +08:00
|
|
|
|
#region 沙盒相关
|
2022-10-18 10:13:04 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取内置文件夹名称
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static string GetStreamingAssetBuildinFolderName()
|
|
|
|
|
|
{
|
|
|
|
|
|
return YooAssetSettings.StreamingAssetsBuildinFolder;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-05-02 15:03:36 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取沙盒的根路径
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static string GetSandboxRoot()
|
|
|
|
|
|
{
|
2022-12-07 17:52:32 +08:00
|
|
|
|
return PathHelper.GetPersistentRootPath();
|
2022-05-02 15:03:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-01 10:44:12 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 清空沙盒目录
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static void ClearSandbox()
|
|
|
|
|
|
{
|
2022-10-31 16:45:21 +08:00
|
|
|
|
PersistentHelper.DeleteSandbox();
|
2022-03-01 10:44:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
2022-09-29 18:40:43 +08:00
|
|
|
|
#region 调试信息
|
|
|
|
|
|
internal static DebugReport GetDebugReport()
|
2022-03-07 14:37:50 +08:00
|
|
|
|
{
|
2022-09-29 18:40:43 +08:00
|
|
|
|
DebugReport report = new DebugReport();
|
|
|
|
|
|
report.FrameCount = Time.frameCount;
|
2022-05-05 21:12:44 +08:00
|
|
|
|
|
2022-09-29 18:40:43 +08:00
|
|
|
|
foreach (var package in _packages)
|
2022-05-02 23:15:09 +08:00
|
|
|
|
{
|
2022-10-22 15:38:51 +08:00
|
|
|
|
var packageData = package.GetDebugPackageData();
|
|
|
|
|
|
report.PackageDatas.Add(packageData);
|
2022-05-02 23:15:09 +08:00
|
|
|
|
}
|
2022-09-29 18:40:43 +08:00
|
|
|
|
return report;
|
2022-05-11 16:48:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
#endregion
|
2022-03-01 10:44:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|