YooAssets.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. /// <param name="logger">自定义日志处理</param>
  17. public static void Initialize(ILogger logger = null)
  18. {
  19. if (_isInitialize)
  20. {
  21. UnityEngine.Debug.LogWarning($"{nameof(YooAssets)} is initialized !");
  22. return;
  23. }
  24. if (_isInitialize == false)
  25. {
  26. YooLogger.Logger = logger;
  27. // 创建驱动器
  28. _isInitialize = true;
  29. _driver = new UnityEngine.GameObject($"[{nameof(YooAssets)}]");
  30. _driver.AddComponent<YooAssetsDriver>();
  31. UnityEngine.Object.DontDestroyOnLoad(_driver);
  32. YooLogger.Log($"{nameof(YooAssets)} initialize !");
  33. #if DEBUG
  34. // 添加远程调试脚本
  35. _driver.AddComponent<RemoteDebuggerInRuntime>();
  36. #endif
  37. OperationSystem.Initialize();
  38. DownloadSystem.Initialize();
  39. }
  40. }
  41. /// <summary>
  42. /// 销毁资源系统
  43. /// </summary>
  44. public static void Destroy()
  45. {
  46. if (_isInitialize)
  47. {
  48. OperationSystem.DestroyAll();
  49. DownloadSystem.DestroyAll();
  50. CacheSystem.ClearAll();
  51. foreach (var package in _packages)
  52. {
  53. package.DestroyPackage();
  54. }
  55. _packages.Clear();
  56. _isInitialize = false;
  57. if (_driver != null)
  58. GameObject.Destroy(_driver);
  59. YooLogger.Log($"{nameof(YooAssets)} destroy all !");
  60. }
  61. }
  62. /// <summary>
  63. /// 更新资源系统
  64. /// </summary>
  65. internal static void Update()
  66. {
  67. if (_isInitialize)
  68. {
  69. OperationSystem.Update();
  70. DownloadSystem.Update();
  71. for (int i = 0; i < _packages.Count; i++)
  72. {
  73. _packages[i].UpdatePackage();
  74. }
  75. }
  76. }
  77. /// <summary>
  78. /// 创建资源包
  79. /// </summary>
  80. /// <param name="packageName">资源包名称</param>
  81. public static ResourcePackage CreatePackage(string packageName)
  82. {
  83. if (_isInitialize == false)
  84. throw new Exception($"{nameof(YooAssets)} not initialize !");
  85. if (string.IsNullOrEmpty(packageName))
  86. throw new Exception("Package name is null or empty !");
  87. if (HasPackage(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. var package = TryGetPackage(packageName);
  101. if (package == null)
  102. YooLogger.Error($"Not found resource package : {packageName}");
  103. return package;
  104. }
  105. /// <summary>
  106. /// 尝试获取资源包
  107. /// </summary>
  108. /// <param name="packageName">资源包名称</param>
  109. public static ResourcePackage TryGetPackage(string packageName)
  110. {
  111. if (_isInitialize == false)
  112. throw new Exception($"{nameof(YooAssets)} not initialize !");
  113. if (string.IsNullOrEmpty(packageName))
  114. throw new Exception("Package name is null or empty !");
  115. foreach (var package in _packages)
  116. {
  117. if (package.PackageName == packageName)
  118. return package;
  119. }
  120. return null;
  121. }
  122. /// <summary>
  123. /// 销毁资源包
  124. /// </summary>
  125. /// <param name="packageName">资源包名称</param>
  126. public static void DestroyPackage(string packageName)
  127. {
  128. ResourcePackage package = GetPackage(packageName);
  129. if (package == null)
  130. return;
  131. YooLogger.Log($"Destroy resource package : {packageName}");
  132. _packages.Remove(package);
  133. package.DestroyPackage();
  134. // 清空缓存
  135. CacheSystem.ClearPackage(packageName);
  136. }
  137. /// <summary>
  138. /// 检测资源包是否存在
  139. /// </summary>
  140. /// <param name="packageName">资源包名称</param>
  141. public static bool HasPackage(string packageName)
  142. {
  143. if (_isInitialize == false)
  144. throw new Exception($"{nameof(YooAssets)} not initialize !");
  145. foreach (var package in _packages)
  146. {
  147. if (package.PackageName == packageName)
  148. return true;
  149. }
  150. return false;
  151. }
  152. /// <summary>
  153. /// 开启一个异步操作
  154. /// </summary>
  155. /// <param name="operation">异步操作对象</param>
  156. public static void StartOperation(GameAsyncOperation operation)
  157. {
  158. OperationSystem.StartOperation(operation);
  159. }
  160. #region 系统参数
  161. /// <summary>
  162. /// 设置下载系统参数,启用断点续传功能文件的最小字节数
  163. /// </summary>
  164. public static void SetDownloadSystemBreakpointResumeFileSize(int fileBytes)
  165. {
  166. DownloadSystem.BreakpointResumeFileSize = fileBytes;
  167. }
  168. /// <summary>
  169. /// 设置下载系统参数,下载失败后清理文件的HTTP错误码
  170. /// </summary>
  171. public static void SetDownloadSystemClearFileResponseCode(List<long> codes)
  172. {
  173. DownloadSystem.ClearFileResponseCodes = codes;
  174. }
  175. /// <summary>
  176. /// 设置下载系统参数,自定义的证书认证实例
  177. /// </summary>
  178. public static void SetDownloadSystemCertificateHandler(UnityEngine.Networking.CertificateHandler instance)
  179. {
  180. DownloadSystem.CertificateHandlerInstance = instance;
  181. }
  182. /// <summary>
  183. /// 设置下载系统参数,自定义下载请求
  184. /// </summary>
  185. public static void SetDownloadSystemUnityWebRequest(DownloadRequestDelegate requestDelegate)
  186. {
  187. DownloadSystem.RequestDelegate = requestDelegate;
  188. }
  189. /// <summary>
  190. /// 设置下载系统参数,网络重定向次数(Unity引擎默认值32)
  191. /// 注意:不支持设置为负值
  192. /// </summary>
  193. public static void SetDownloadSystemRedirectLimit(int redirectLimit)
  194. {
  195. if (redirectLimit < 0)
  196. {
  197. YooLogger.Warning($"Invalid param value : {redirectLimit}");
  198. return;
  199. }
  200. DownloadSystem.RedirectLimit = redirectLimit;
  201. }
  202. /// <summary>
  203. /// 设置异步系统参数,每帧执行消耗的最大时间切片(单位:毫秒)
  204. /// </summary>
  205. public static void SetOperationSystemMaxTimeSlice(long milliseconds)
  206. {
  207. if (milliseconds < 10)
  208. {
  209. milliseconds = 10;
  210. YooLogger.Warning($"MaxTimeSlice minimum value is 10 milliseconds.");
  211. }
  212. OperationSystem.MaxTimeSlice = milliseconds;
  213. }
  214. /// <summary>
  215. /// 设置缓存系统参数,已经缓存文件的校验等级
  216. /// </summary>
  217. public static void SetCacheSystemCachedFileVerifyLevel(EVerifyLevel verifyLevel)
  218. {
  219. CacheSystem.InitVerifyLevel = verifyLevel;
  220. }
  221. /// <summary>
  222. /// 设置缓存系统参数,禁用缓存在WebGL平台
  223. /// </summary>
  224. public static void SetCacheSystemDisableCacheOnWebGL()
  225. {
  226. CacheSystem.DisableUnityCacheOnWebGL = true;
  227. }
  228. #endregion
  229. #region 调试信息
  230. internal static DebugReport GetDebugReport()
  231. {
  232. DebugReport report = new DebugReport();
  233. report.FrameCount = Time.frameCount;
  234. foreach (var package in _packages)
  235. {
  236. var packageData = package.GetDebugPackageData();
  237. report.PackageDatas.Add(packageData);
  238. }
  239. return report;
  240. }
  241. #endregion
  242. }
  243. }