YooAssets.cs 7.1 KB

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