YooAssets.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. using System;
  2. using System.Diagnostics;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. namespace YooAsset
  7. {
  8. public static partial class YooAssets
  9. {
  10. private static bool _isInitialize = false;
  11. private static GameObject _driver = null;
  12. private static readonly List<ResourcePackage> _packages = new List<ResourcePackage>();
  13. /// <summary>
  14. /// 是否已经初始化
  15. /// </summary>
  16. public static bool Initialized
  17. {
  18. get { return _isInitialize; }
  19. }
  20. /// <summary>
  21. /// 初始化资源系统
  22. /// </summary>
  23. /// <param name="logger">自定义日志处理</param>
  24. public static void Initialize(ILogger logger = null)
  25. {
  26. if (_isInitialize)
  27. {
  28. UnityEngine.Debug.LogWarning($"{nameof(YooAssets)} is initialized !");
  29. return;
  30. }
  31. if (_isInitialize == false)
  32. {
  33. YooLogger.Logger = logger;
  34. // 创建驱动器
  35. _isInitialize = true;
  36. _driver = new UnityEngine.GameObject($"[{nameof(YooAssets)}]");
  37. _driver.AddComponent<YooAssetsDriver>();
  38. UnityEngine.Object.DontDestroyOnLoad(_driver);
  39. YooLogger.Log($"{nameof(YooAssets)} initialize !");
  40. #if DEBUG
  41. // 添加远程调试脚本
  42. _driver.AddComponent<RemoteDebuggerInRuntime>();
  43. #endif
  44. OperationSystem.Initialize();
  45. }
  46. }
  47. /// <summary>
  48. /// 销毁资源系统
  49. /// </summary>
  50. public static void Destroy()
  51. {
  52. if (_isInitialize)
  53. {
  54. OperationSystem.DestroyAll();
  55. foreach (var package in _packages)
  56. {
  57. package.DestroyPackage();
  58. }
  59. _packages.Clear();
  60. _isInitialize = false;
  61. if (_driver != null)
  62. GameObject.Destroy(_driver);
  63. YooLogger.Log($"{nameof(YooAssets)} destroy all !");
  64. }
  65. }
  66. /// <summary>
  67. /// 更新资源系统
  68. /// </summary>
  69. internal static void Update()
  70. {
  71. if (_isInitialize)
  72. {
  73. OperationSystem.Update();
  74. for (int i = 0; i < _packages.Count; i++)
  75. {
  76. _packages[i].UpdatePackage();
  77. }
  78. }
  79. }
  80. /// <summary>
  81. /// 创建资源包
  82. /// </summary>
  83. /// <param name="packageName">资源包名称</param>
  84. public static ResourcePackage CreatePackage(string packageName)
  85. {
  86. CheckException(packageName);
  87. if (ContainsPackage(packageName))
  88. throw new Exception($"Package {packageName} already existed !");
  89. YooLogger.Log($"Create resource package : {packageName}");
  90. ResourcePackage package = new ResourcePackage(packageName);
  91. _packages.Add(package);
  92. return package;
  93. }
  94. /// <summary>
  95. /// 获取资源包
  96. /// </summary>
  97. /// <param name="packageName">资源包名称</param>
  98. public static ResourcePackage GetPackage(string packageName)
  99. {
  100. CheckException(packageName);
  101. var package = GetPackageInternal(packageName);
  102. if (package == null)
  103. YooLogger.Error($"Not found resource package : {packageName}");
  104. return package;
  105. }
  106. /// <summary>
  107. /// 尝试获取资源包
  108. /// </summary>
  109. /// <param name="packageName">资源包名称</param>
  110. public static ResourcePackage TryGetPackage(string packageName)
  111. {
  112. CheckException(packageName);
  113. return GetPackageInternal(packageName);
  114. }
  115. /// <summary>
  116. /// 销毁资源包
  117. /// </summary>
  118. /// <param name="packageName">资源包名称</param>
  119. public static void DestroyPackage(string packageName)
  120. {
  121. CheckException(packageName);
  122. ResourcePackage package = GetPackageInternal(packageName);
  123. if (package == null)
  124. return;
  125. YooLogger.Log($"Destroy resource package : {packageName}");
  126. _packages.Remove(package);
  127. package.DestroyPackage();
  128. }
  129. /// <summary>
  130. /// 检测资源包是否存在
  131. /// </summary>
  132. /// <param name="packageName">资源包名称</param>
  133. public static bool ContainsPackage(string packageName)
  134. {
  135. CheckException(packageName);
  136. var package = GetPackageInternal(packageName);
  137. return package != null;
  138. }
  139. /// <summary>
  140. /// 开启一个异步操作
  141. /// </summary>
  142. /// <param name="operation">异步操作对象</param>
  143. public static void StartOperation(GameAsyncOperation operation)
  144. {
  145. // 注意:游戏业务逻辑的包裹填写为空
  146. OperationSystem.StartOperation(string.Empty, operation);
  147. }
  148. private static ResourcePackage GetPackageInternal(string packageName)
  149. {
  150. foreach (var package in _packages)
  151. {
  152. if (package.PackageName == packageName)
  153. return package;
  154. }
  155. return null;
  156. }
  157. private static void CheckException(string packageName)
  158. {
  159. if (_isInitialize == false)
  160. throw new Exception($"{nameof(YooAssets)} not initialize !");
  161. if (string.IsNullOrEmpty(packageName))
  162. throw new Exception("Package name is null or empty !");
  163. }
  164. #region 系统参数
  165. /// <summary>
  166. /// 设置下载系统参数,下载失败后清理文件的HTTP错误码
  167. /// </summary>
  168. public static void SetDownloadSystemClearFileResponseCode(List<long> codes)
  169. {
  170. DownloadHelper.ClearFileResponseCodes = codes;
  171. }
  172. /// <summary>
  173. /// 设置下载系统参数,自定义下载请求
  174. /// </summary>
  175. public static void SetDownloadSystemUnityWebRequest(DownloadRequestDelegate requestDelegate)
  176. {
  177. DownloadHelper.RequestDelegate = requestDelegate;
  178. }
  179. /// <summary>
  180. /// 设置异步系统参数,每帧执行消耗的最大时间切片(单位:毫秒)
  181. /// </summary>
  182. public static void SetOperationSystemMaxTimeSlice(long milliseconds)
  183. {
  184. if (milliseconds < 10)
  185. {
  186. milliseconds = 10;
  187. YooLogger.Warning($"MaxTimeSlice minimum value is 10 milliseconds.");
  188. }
  189. OperationSystem.MaxTimeSlice = milliseconds;
  190. }
  191. /// <summary>
  192. /// 设置缓存系统参数,禁用缓存在WebGL平台
  193. /// </summary>
  194. public static void SetCacheSystemDisableCacheOnWebGL()
  195. {
  196. CacheHelper.DisableUnityCacheOnWebGL = true;
  197. }
  198. #endregion
  199. #region 调试信息
  200. internal static DebugReport GetDebugReport()
  201. {
  202. DebugReport report = new DebugReport();
  203. report.FrameCount = Time.frameCount;
  204. foreach (var package in _packages)
  205. {
  206. var packageData = package.GetDebugPackageData();
  207. report.PackageDatas.Add(packageData);
  208. }
  209. return report;
  210. }
  211. #endregion
  212. }
  213. }