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
{
2023-12-21 19:10:46 +08:00
public static partial class YooAssets
{
private static bool _isInitialize = false ;
private static GameObject _driver = null ;
private static readonly List < ResourcePackage > _packages = new List < ResourcePackage > ( ) ;
/// <summary>
/// 是否已经初始化
/// </summary>
public static bool Initialized
{
get { return _isInitialize ; }
}
/// <summary>
/// 初始化资源系统
/// </summary>
/// <param name="logger">自定义日志处理</param>
public static void Initialize ( ILogger logger = null )
{
if ( _isInitialize )
{
UnityEngine . Debug . LogWarning ( $"{nameof(YooAssets)} is initialized !" ) ;
return ;
}
if ( _isInitialize = = false )
{
YooLogger . Logger = logger ;
// 创建驱动器
_isInitialize = true ;
_driver = new UnityEngine . GameObject ( $"[{nameof(YooAssets)}]" ) ;
_driver . AddComponent < YooAssetsDriver > ( ) ;
UnityEngine . Object . DontDestroyOnLoad ( _driver ) ;
YooLogger . Log ( $"{nameof(YooAssets)} initialize !" ) ;
2022-09-28 11:55:12 +08:00
#if DEBUG
2023-12-21 19:10:46 +08:00
// 添加远程调试脚本
_driver . AddComponent < RemoteDebuggerInRuntime > ( ) ;
2022-09-28 11:55:12 +08:00
#endif
2022-04-04 22:43:47 +08:00
2023-12-21 19:10:46 +08:00
OperationSystem . Initialize ( ) ;
}
}
/// <summary>
2024-07-04 20:36:26 +08:00
/// 更新资源系统
2023-12-21 19:10:46 +08:00
/// </summary>
2024-07-04 20:36:26 +08:00
internal static void Update ( )
2023-12-21 19:10:46 +08:00
{
if ( _isInitialize )
{
2024-07-04 20:36:26 +08:00
OperationSystem . Update ( ) ;
2023-12-21 19:10:46 +08:00
2024-07-04 20:36:26 +08:00
for ( int i = 0 ; i < _packages . Count ; i + + )
2023-12-21 19:10:46 +08:00
{
2024-07-04 20:36:26 +08:00
_packages [ i ] . UpdatePackage ( ) ;
2023-12-21 19:10:46 +08:00
}
}
}
/// <summary>
2024-07-04 20:36:26 +08:00
/// 应用程序退出处理
2023-12-21 19:10:46 +08:00
/// </summary>
2024-07-04 20:36:26 +08:00
internal static void OnApplicationQuit ( )
2023-12-21 19:10:46 +08:00
{
2024-07-04 20:36:26 +08:00
// 说明: 在编辑器下确保播放被停止时IO类操作被终止。
foreach ( var package in _packages )
2023-12-21 19:10:46 +08:00
{
2024-07-04 20:36:26 +08:00
OperationSystem . ClearPackageOperation ( package . PackageName ) ;
2023-12-21 19:10:46 +08:00
}
2024-07-04 20:36:26 +08:00
OperationSystem . DestroyAll ( ) ;
2023-12-21 19:10:46 +08:00
}
/// <summary>
/// 创建资源包
/// </summary>
/// <param name="packageName">资源包名称</param>
public static ResourcePackage CreatePackage ( string packageName )
{
CheckException ( packageName ) ;
if ( ContainsPackage ( packageName ) )
throw new Exception ( $"Package {packageName} already existed !" ) ;
YooLogger . Log ( $"Create resource package : {packageName}" ) ;
ResourcePackage package = new ResourcePackage ( packageName ) ;
_packages . Add ( package ) ;
return package ;
}
/// <summary>
/// 获取资源包
/// </summary>
/// <param name="packageName">资源包名称</param>
public static ResourcePackage GetPackage ( string packageName )
{
CheckException ( packageName ) ;
var package = GetPackageInternal ( packageName ) ;
if ( package = = null )
2024-07-07 00:52:17 +08:00
YooLogger . Error ( $"Can not found resource package : {packageName}" ) ;
2023-12-21 19:10:46 +08:00
return package ;
}
/// <summary>
/// 尝试获取资源包
/// </summary>
/// <param name="packageName">资源包名称</param>
public static ResourcePackage TryGetPackage ( string packageName )
{
CheckException ( packageName ) ;
return GetPackageInternal ( packageName ) ;
}
/// <summary>
2024-07-04 20:36:26 +08:00
/// 移除资源包
2023-12-21 19:10:46 +08:00
/// </summary>
/// <param name="packageName">资源包名称</param>
2024-07-04 20:36:26 +08:00
public static bool RemovePackage ( string packageName )
2023-12-21 19:10:46 +08:00
{
CheckException ( packageName ) ;
ResourcePackage package = GetPackageInternal ( packageName ) ;
if ( package = = null )
2024-07-04 20:36:26 +08:00
return false ;
if ( package . InitializeStatus ! = EOperationStatus . None )
{
YooLogger . Error ( $"The resource package {packageName} has not been destroyed, please call the method {nameof(ResourcePackage.DestroyAsync)} to destroy!" ) ;
return false ;
}
2023-12-21 19:10:46 +08:00
2024-07-04 20:36:26 +08:00
YooLogger . Log ( $"Remove resource package : {packageName}" ) ;
2023-12-21 19:10:46 +08:00
_packages . Remove ( package ) ;
2024-07-04 20:36:26 +08:00
return true ;
2023-12-21 19:10:46 +08:00
}
/// <summary>
/// 检测资源包是否存在
/// </summary>
/// <param name="packageName">资源包名称</param>
public static bool ContainsPackage ( string packageName )
{
CheckException ( packageName ) ;
var package = GetPackageInternal ( packageName ) ;
return package ! = null ;
}
/// <summary>
/// 开启一个异步操作
/// </summary>
/// <param name="operation">异步操作对象</param>
public static void StartOperation ( GameAsyncOperation operation )
{
// 注意:游戏业务逻辑的包裹填写为空
OperationSystem . StartOperation ( string . Empty , operation ) ;
}
private static ResourcePackage GetPackageInternal ( string packageName )
{
foreach ( var package in _packages )
{
if ( package . PackageName = = packageName )
return package ;
}
return null ;
}
private static void CheckException ( 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 !" ) ;
}
#region 系 统 参 数
/// <summary>
/// 设置下载系统参数,自定义下载请求
/// </summary>
2024-07-04 20:36:26 +08:00
public static void SetDownloadSystemUnityWebRequest ( UnityWebRequestDelegate createDelegate )
2023-12-21 19:10:46 +08:00
{
2024-07-04 20:36:26 +08:00
DownloadSystemHelper . UnityWebRequestCreater = createDelegate ;
2023-12-21 19:10:46 +08:00
}
/// <summary>
/// 设置异步系统参数,每帧执行消耗的最大时间切片(单位:毫秒)
/// </summary>
public static void SetOperationSystemMaxTimeSlice ( long milliseconds )
{
if ( milliseconds < 10 )
{
milliseconds = 10 ;
YooLogger . Warning ( $"MaxTimeSlice minimum value is 10 milliseconds." ) ;
}
OperationSystem . MaxTimeSlice = milliseconds ;
}
#endregion
#region 调 试 信 息
internal static DebugReport GetDebugReport ( )
{
DebugReport report = new DebugReport ( ) ;
report . FrameCount = Time . frameCount ;
foreach ( var package in _packages )
{
var packageData = package . GetDebugPackageData ( ) ;
report . PackageDatas . Add ( packageData ) ;
}
return report ;
}
#endregion
}
2022-03-01 10:44:12 +08:00
}