YooAssets.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. using System;
  2. using System.Diagnostics;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using UnityEngine;
  7. namespace YooAsset
  8. {
  9. public static partial class YooAssets
  10. {
  11. #if UNITY_EDITOR
  12. [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
  13. private static void OnRuntimeInitialize()
  14. {
  15. _isInitialize = false;
  16. _packages.Clear();
  17. _defaultPackage = null;
  18. }
  19. #endif
  20. private static bool _isInitialize = false;
  21. private static GameObject _driver = null;
  22. private static readonly List<ResourcePackage> _packages = new List<ResourcePackage>();
  23. /// <summary>
  24. /// 是否已经初始化
  25. /// </summary>
  26. public static bool Initialized
  27. {
  28. get { return _isInitialize; }
  29. }
  30. /// <summary>
  31. /// 初始化资源系统
  32. /// </summary>
  33. /// <param name="logger">自定义日志处理</param>
  34. public static void Initialize(ILogger logger = null)
  35. {
  36. if (_isInitialize)
  37. {
  38. UnityEngine.Debug.LogWarning($"{nameof(YooAssets)} is initialized !");
  39. return;
  40. }
  41. if (_isInitialize == false)
  42. {
  43. YooLogger.Logger = logger;
  44. // 创建驱动器
  45. _isInitialize = true;
  46. _driver = new UnityEngine.GameObject($"[{nameof(YooAssets)}]");
  47. _driver.AddComponent<YooAssetsDriver>();
  48. UnityEngine.Object.DontDestroyOnLoad(_driver);
  49. YooLogger.Log($"{nameof(YooAssets)} initialize !");
  50. #if DEBUG
  51. // 添加远程调试脚本
  52. _driver.AddComponent<RemoteDebuggerInRuntime>();
  53. #endif
  54. OperationSystem.Initialize();
  55. }
  56. }
  57. /// <summary>
  58. /// 更新资源系统
  59. /// </summary>
  60. internal static void Update()
  61. {
  62. if (_isInitialize)
  63. {
  64. OperationSystem.Update();
  65. }
  66. }
  67. /// <summary>
  68. /// 应用程序退出处理
  69. /// </summary>
  70. internal static void OnApplicationQuit()
  71. {
  72. // 说明:在编辑器下确保播放被停止时IO类操作被终止。
  73. foreach (var package in _packages)
  74. {
  75. OperationSystem.ClearPackageOperation(package.PackageName);
  76. }
  77. OperationSystem.DestroyAll();
  78. }
  79. /// <summary>
  80. /// 创建资源包裹
  81. /// </summary>
  82. /// <param name="packageName">包裹名称</param>
  83. public static ResourcePackage CreatePackage(string packageName)
  84. {
  85. CheckException(packageName);
  86. if (ContainsPackage(packageName))
  87. throw new System.Exception($"Package {packageName} already existed !");
  88. YooLogger.Log($"Create resource package : {packageName}");
  89. ResourcePackage package = new ResourcePackage(packageName);
  90. _packages.Add(package);
  91. return package;
  92. }
  93. /// <summary>
  94. /// 获取资源包裹
  95. /// </summary>
  96. /// <param name="packageName">包裹名称</param>
  97. public static ResourcePackage GetPackage(string packageName)
  98. {
  99. CheckException(packageName);
  100. var package = GetPackageInternal(packageName);
  101. if (package == null)
  102. YooLogger.Error($"Can 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. CheckException(packageName);
  112. return GetPackageInternal(packageName);
  113. }
  114. /// <summary>
  115. /// 获取所有资源包裹
  116. /// </summary>
  117. public static List<ResourcePackage> GetAllPackages()
  118. {
  119. return _packages.ToList();
  120. }
  121. /// <summary>
  122. /// 移除资源包裹
  123. /// </summary>
  124. /// <param name="packageName">包裹名称</param>
  125. public static bool RemovePackage(string packageName)
  126. {
  127. CheckException(packageName);
  128. ResourcePackage package = GetPackageInternal(packageName);
  129. if (package == null)
  130. return false;
  131. return RemovePackage(package);
  132. }
  133. /// <summary>
  134. /// 移除资源包裹
  135. /// </summary>
  136. /// <param name="package">包裹实例对象</param>
  137. public static bool RemovePackage(ResourcePackage package)
  138. {
  139. CheckException(package);
  140. string packageName = package.PackageName;
  141. if (package.InitializeStatus != EOperationStatus.None)
  142. {
  143. YooLogger.Error($"The resource package {packageName} has not been destroyed, please call the method {nameof(ResourcePackage.DestroyAsync)} to destroy!");
  144. return false;
  145. }
  146. YooLogger.Log($"Remove resource package : {packageName}");
  147. _packages.Remove(package);
  148. return true;
  149. }
  150. /// <summary>
  151. /// 检测资源包裹是否存在
  152. /// </summary>
  153. /// <param name="packageName">包裹名称</param>
  154. public static bool ContainsPackage(string packageName)
  155. {
  156. CheckException(packageName);
  157. var package = GetPackageInternal(packageName);
  158. return package != null;
  159. }
  160. /// <summary>
  161. /// 开启一个异步操作
  162. /// </summary>
  163. /// <param name="operation">异步操作对象</param>
  164. public static void StartOperation(GameAsyncOperation operation)
  165. {
  166. // 注意:游戏业务逻辑的包裹填写为空
  167. OperationSystem.StartOperation(string.Empty, operation);
  168. }
  169. private static ResourcePackage GetPackageInternal(string packageName)
  170. {
  171. foreach (var package in _packages)
  172. {
  173. if (package.PackageName == packageName)
  174. return package;
  175. }
  176. return null;
  177. }
  178. private static void CheckException(string packageName)
  179. {
  180. if (_isInitialize == false)
  181. throw new Exception($"{nameof(YooAssets)} not initialize !");
  182. if (string.IsNullOrEmpty(packageName))
  183. throw new Exception("Package name is null or empty !");
  184. }
  185. private static void CheckException(ResourcePackage package)
  186. {
  187. if (_isInitialize == false)
  188. throw new Exception($"{nameof(YooAssets)} not initialize !");
  189. if (package == null)
  190. throw new Exception("Package instance is null !");
  191. }
  192. #region 系统参数
  193. /// <summary>
  194. /// 设置下载系统参数,自定义下载请求
  195. /// </summary>
  196. public static void SetDownloadSystemUnityWebRequest(UnityWebRequestDelegate createDelegate)
  197. {
  198. DownloadSystemHelper.UnityWebRequestCreater = createDelegate;
  199. }
  200. /// <summary>
  201. /// 设置异步系统参数,每帧执行消耗的最大时间切片(单位:毫秒)
  202. /// </summary>
  203. public static void SetOperationSystemMaxTimeSlice(long milliseconds)
  204. {
  205. if (milliseconds < 10)
  206. {
  207. milliseconds = 10;
  208. YooLogger.Warning($"MaxTimeSlice minimum value is 10 milliseconds.");
  209. }
  210. OperationSystem.MaxTimeSlice = milliseconds;
  211. }
  212. #endregion
  213. #region 调试信息
  214. internal static DebugReport GetDebugReport()
  215. {
  216. DebugReport report = new DebugReport();
  217. report.FrameCount = Time.frameCount;
  218. foreach (var package in _packages)
  219. {
  220. var packageData = package.GetDebugPackageData();
  221. report.PackageDatas.Add(packageData);
  222. }
  223. return report;
  224. }
  225. #endregion
  226. }
  227. }