Files
YooAsset/Assets/YooAsset/Runtime/YooAssets.cs

258 lines
6.3 KiB
C#
Raw Normal View History

2022-03-01 10:44:12 +08:00
using System;
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
{
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;
private static GameObject _driver = null;
2022-10-14 11:49:32 +08:00
private static readonly List<AssetsPackage> _packages = new List<AssetsPackage>();
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)
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-09-28 11:55:12 +08:00
_isInitialize = true;
_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
// 添加远程调试脚本
_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>
/// 销毁资源系统
/// </summary>
public static void Destroy()
{
if (_isInitialize)
{
OperationSystem.DestroyAll();
DownloadSystem.DestroyAll();
CacheSystem.ClearAll();
foreach (var package in _packages)
2022-09-29 18:40:43 +08:00
{
package.DestroyPackage();
2022-09-29 18:40:43 +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-07-20 10:57:56 +08:00
/// <summary>
/// 更新资源系统
2022-07-20 10:57:56 +08:00
/// </summary>
internal static void Update()
2022-07-20 10:57:56 +08:00
{
2022-09-29 18:40:43 +08:00
if (_isInitialize)
{
OperationSystem.Update();
DownloadSystem.Update();
2022-03-24 00:20:37 +08:00
for (int i = 0; i < _packages.Count; i++)
2022-09-29 18:40:43 +08:00
{
_packages[i].UpdatePackage();
2022-09-29 18:40:43 +08:00
}
}
2022-03-01 10:44:12 +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)
throw new Exception($"{nameof(YooAssets)} not initialize !");
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-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
}
/// <summary>
/// 获取资源包
/// </summary>
/// <param name="packageName">资源包名称</param>
2022-10-14 11:49:32 +08:00
public static AssetsPackage GetAssetsPackage(string packageName)
{
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)
{
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;
}
/// <summary>
2022-09-29 18:40:43 +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)
{
if (_isInitialize == false)
throw new Exception($"{nameof(YooAssets)} not initialize !");
2022-09-29 18:40:43 +08:00
foreach (var package in _packages)
{
2022-09-29 18:40:43 +08:00
if (package.PackageName == packageName)
return true;
}
2022-09-29 18:40:43 +08:00
return false;
}
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-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;
}
/// <summary>
/// 设置下载系统参数下载失败后清理文件的HTTP错误码
/// </summary>
public static void SetDownloadSystemClearFileResponseCode(List<long> codes)
{
DownloadSystem.ClearFileResponseCodes = codes;
}
/// <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;
}
/// <summary>
/// 设置异步系统参数,每帧执行消耗的最大时间切片(单位:毫秒)
/// </summary>
2022-09-29 18:40:43 +08:00
public static void SetOperationSystemMaxTimeSlice(long milliseconds)
{
2022-09-29 18:40:43 +08:00
if (milliseconds < 30)
{
2022-09-29 18:40:43 +08:00
milliseconds = 30;
YooLogger.Warning($"MaxTimeSlice minimum value is 30 milliseconds.");
}
2022-09-29 18:40:43 +08:00
OperationSystem.MaxTimeSlice = milliseconds;
}
/// <summary>
/// 设置缓存系统参数,已经缓存文件的校验等级
/// </summary>
2022-09-29 18:40:43 +08:00
public static void SetCacheSystemCachedFileVerifyLevel(EVerifyLevel verifyLevel)
{
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;
}
/// <summary>
/// 获取沙盒的根路径
/// </summary>
public static string GetSandboxRoot()
{
return PathHelper.GetPersistentRootPath();
}
2022-03-01 10:44:12 +08:00
/// <summary>
/// 清空沙盒目录
/// </summary>
public static void ClearSandbox()
{
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-09-29 18:40:43 +08:00
foreach (var package in _packages)
{
2022-10-22 15:38:51 +08:00
var packageData = package.GetDebugPackageData();
report.PackageDatas.Add(packageData);
}
2022-09-29 18:40:43 +08:00
return report;
}
#endregion
2022-03-01 10:44:12 +08:00
}
}