YooAssets.cs 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178
  1. using System;
  2. using System.Diagnostics;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine.SceneManagement;
  6. namespace YooAsset
  7. {
  8. public static partial class YooAssets
  9. {
  10. /// <summary>
  11. /// 运行模式
  12. /// </summary>
  13. public enum EPlayMode
  14. {
  15. /// <summary>
  16. /// 编辑器下的模拟模式
  17. /// 注意:在初始化的时候自动构建真机模拟环境。
  18. /// </summary>
  19. EditorSimulateMode,
  20. /// <summary>
  21. /// 离线运行模式
  22. /// </summary>
  23. OfflinePlayMode,
  24. /// <summary>
  25. /// 联机运行模式
  26. /// </summary>
  27. HostPlayMode,
  28. }
  29. /// <summary>
  30. /// 初始化参数
  31. /// </summary>
  32. public abstract class InitializeParameters
  33. {
  34. /// <summary>
  35. /// 资源定位地址大小写不敏感
  36. /// </summary>
  37. public bool LocationToLower = false;
  38. /// <summary>
  39. /// 资源定位服务接口
  40. /// </summary>
  41. public ILocationServices LocationServices = null;
  42. /// <summary>
  43. /// 文件解密服务接口
  44. /// </summary>
  45. public IDecryptionServices DecryptionServices = null;
  46. /// <summary>
  47. /// 资源加载的最大数量
  48. /// </summary>
  49. public int AssetLoadingMaxNumber = int.MaxValue;
  50. /// <summary>
  51. /// 异步操作系统每帧允许运行的最大时间切片(单位:毫秒)
  52. /// </summary>
  53. public long OperationSystemMaxTimeSlice = long.MaxValue;
  54. }
  55. /// <summary>
  56. /// 编辑器下模拟运行模式的初始化参数
  57. /// </summary>
  58. public class EditorSimulateModeParameters : InitializeParameters
  59. {
  60. /// <summary>
  61. /// 用于模拟运行的资源清单路径
  62. /// 注意:如果路径为空,会自动重新构建补丁清单。
  63. /// </summary>
  64. public string SimulatePatchManifestPath;
  65. }
  66. /// <summary>
  67. /// 离线运行模式的初始化参数
  68. /// </summary>
  69. public class OfflinePlayModeParameters : InitializeParameters
  70. {
  71. }
  72. /// <summary>
  73. /// 联机运行模式的初始化参数
  74. /// </summary>
  75. public class HostPlayModeParameters : InitializeParameters
  76. {
  77. /// <summary>
  78. /// 默认的资源服务器下载地址
  79. /// </summary>
  80. public string DefaultHostServer;
  81. /// <summary>
  82. /// 备用的资源服务器下载地址
  83. /// </summary>
  84. public string FallbackHostServer;
  85. /// <summary>
  86. /// 当缓存池被污染的时候清理缓存池
  87. /// </summary>
  88. public bool ClearCacheWhenDirty = false;
  89. #if UNITY_WEBGL
  90. /// <summary>
  91. /// WEBGL模式不支持多线程下载
  92. /// </summary>
  93. internal int BreakpointResumeFileSize = int.MaxValue;
  94. #else
  95. /// <summary>
  96. /// 启用断点续传功能的文件大小
  97. /// </summary>
  98. public int BreakpointResumeFileSize = int.MaxValue;
  99. #endif
  100. /// <summary>
  101. /// 下载文件校验等级
  102. /// </summary>
  103. public EVerifyLevel VerifyLevel = EVerifyLevel.High;
  104. }
  105. private static bool _isInitialize = false;
  106. private static string _initializeError = string.Empty;
  107. private static EOperationStatus _initializeStatus = EOperationStatus.None;
  108. private static EPlayMode _playMode;
  109. private static IBundleServices _bundleServices;
  110. private static ILocationServices _locationServices;
  111. private static EditorSimulateModeImpl _editorSimulateModeImpl;
  112. private static OfflinePlayModeImpl _offlinePlayModeImpl;
  113. private static HostPlayModeImpl _hostPlayModeImpl;
  114. /// <summary>
  115. /// 是否已经初始化
  116. /// </summary>
  117. public static bool IsInitialized
  118. {
  119. get { return _isInitialize; }
  120. }
  121. /// <summary>
  122. /// 异步初始化
  123. /// </summary>
  124. public static InitializationOperation InitializeAsync(InitializeParameters parameters)
  125. {
  126. if (parameters == null)
  127. throw new Exception($"YooAsset create parameters is null.");
  128. if (parameters.LocationServices == null)
  129. throw new Exception($"{nameof(IBundleServices)} is null.");
  130. else
  131. _locationServices = parameters.LocationServices;
  132. #if !UNITY_EDITOR
  133. if (parameters is EditorSimulateModeParameters)
  134. throw new Exception($"Editor simulate mode only support unity editor.");
  135. #endif
  136. // 创建驱动器
  137. if (_isInitialize == false)
  138. {
  139. _isInitialize = true;
  140. UnityEngine.GameObject driverGo = new UnityEngine.GameObject("[YooAsset]");
  141. driverGo.AddComponent<YooAssetDriver>();
  142. UnityEngine.Object.DontDestroyOnLoad(driverGo);
  143. #if DEBUG
  144. driverGo.AddComponent<RemoteDebuggerInRuntime>();
  145. #endif
  146. }
  147. else
  148. {
  149. throw new Exception("YooAsset is initialized yet.");
  150. }
  151. // 检测参数范围
  152. if (parameters.AssetLoadingMaxNumber < 1)
  153. {
  154. parameters.AssetLoadingMaxNumber = 1;
  155. YooLogger.Warning($"{nameof(parameters.AssetLoadingMaxNumber)} minimum value is 1");
  156. }
  157. if (parameters.OperationSystemMaxTimeSlice < 30)
  158. {
  159. parameters.OperationSystemMaxTimeSlice = 30;
  160. YooLogger.Warning($"{nameof(parameters.OperationSystemMaxTimeSlice)} minimum value is 30 milliseconds");
  161. }
  162. // 鉴定运行模式
  163. if (parameters is EditorSimulateModeParameters)
  164. _playMode = EPlayMode.EditorSimulateMode;
  165. else if (parameters is OfflinePlayModeParameters)
  166. _playMode = EPlayMode.OfflinePlayMode;
  167. else if (parameters is HostPlayModeParameters)
  168. _playMode = EPlayMode.HostPlayMode;
  169. else
  170. throw new NotImplementedException();
  171. // 初始化异步操作系统
  172. OperationSystem.Initialize(parameters.OperationSystemMaxTimeSlice);
  173. // 初始化下载系统
  174. if (_playMode == EPlayMode.HostPlayMode)
  175. {
  176. var hostPlayModeParameters = parameters as HostPlayModeParameters;
  177. CacheSystem.Initialize(hostPlayModeParameters.VerifyLevel);
  178. DownloadSystem.Initialize(hostPlayModeParameters.BreakpointResumeFileSize);
  179. }
  180. else
  181. {
  182. CacheSystem.Initialize(EVerifyLevel.Low);
  183. DownloadSystem.Initialize(int.MaxValue);
  184. }
  185. // 初始化资源系统
  186. InitializationOperation initializeOperation;
  187. if (_playMode == EPlayMode.EditorSimulateMode)
  188. {
  189. _editorSimulateModeImpl = new EditorSimulateModeImpl();
  190. _bundleServices = _editorSimulateModeImpl;
  191. AssetSystem.Initialize(true, parameters.AssetLoadingMaxNumber, parameters.DecryptionServices, _bundleServices);
  192. var editorSimulateModeParameters = parameters as EditorSimulateModeParameters;
  193. initializeOperation = _editorSimulateModeImpl.InitializeAsync(
  194. editorSimulateModeParameters.LocationToLower,
  195. editorSimulateModeParameters.SimulatePatchManifestPath);
  196. }
  197. else if (_playMode == EPlayMode.OfflinePlayMode)
  198. {
  199. _offlinePlayModeImpl = new OfflinePlayModeImpl();
  200. _bundleServices = _offlinePlayModeImpl;
  201. AssetSystem.Initialize(false, parameters.AssetLoadingMaxNumber, parameters.DecryptionServices, _bundleServices);
  202. initializeOperation = _offlinePlayModeImpl.InitializeAsync(parameters.LocationToLower);
  203. }
  204. else if (_playMode == EPlayMode.HostPlayMode)
  205. {
  206. _hostPlayModeImpl = new HostPlayModeImpl();
  207. _bundleServices = _hostPlayModeImpl;
  208. AssetSystem.Initialize(false, parameters.AssetLoadingMaxNumber, parameters.DecryptionServices, _bundleServices);
  209. var hostPlayModeParameters = parameters as HostPlayModeParameters;
  210. initializeOperation = _hostPlayModeImpl.InitializeAsync(
  211. hostPlayModeParameters.LocationToLower,
  212. hostPlayModeParameters.ClearCacheWhenDirty,
  213. hostPlayModeParameters.DefaultHostServer,
  214. hostPlayModeParameters.FallbackHostServer);
  215. }
  216. else
  217. {
  218. throw new NotImplementedException();
  219. }
  220. // 监听初始化结果
  221. initializeOperation.Completed += InitializeOperation_Completed;
  222. return initializeOperation;
  223. }
  224. private static void InitializeOperation_Completed(AsyncOperationBase op)
  225. {
  226. _initializeStatus = op.Status;
  227. _initializeError = op.Error;
  228. }
  229. /// <summary>
  230. /// 向网络端请求静态资源版本
  231. /// </summary>
  232. /// <param name="timeout">超时时间(默认值:60秒)</param>
  233. public static UpdateStaticVersionOperation UpdateStaticVersionAsync(int timeout = 60)
  234. {
  235. DebugCheckInitialize();
  236. if (_playMode == EPlayMode.EditorSimulateMode)
  237. {
  238. var operation = new EditorPlayModeUpdateStaticVersionOperation();
  239. OperationSystem.StartOperation(operation);
  240. return operation;
  241. }
  242. else if (_playMode == EPlayMode.OfflinePlayMode)
  243. {
  244. var operation = new OfflinePlayModeUpdateStaticVersionOperation();
  245. OperationSystem.StartOperation(operation);
  246. return operation;
  247. }
  248. else if (_playMode == EPlayMode.HostPlayMode)
  249. {
  250. return _hostPlayModeImpl.UpdateStaticVersionAsync(timeout);
  251. }
  252. else
  253. {
  254. throw new NotImplementedException();
  255. }
  256. }
  257. /// <summary>
  258. /// 向网络端请求并更新补丁清单
  259. /// </summary>
  260. /// <param name="resourceVersion">更新的资源版本</param>
  261. /// <param name="timeout">超时时间(默认值:60秒)</param>
  262. public static UpdateManifestOperation UpdateManifestAsync(int resourceVersion, int timeout = 60)
  263. {
  264. DebugCheckInitialize();
  265. DebugCheckUpdateManifest();
  266. if (_playMode == EPlayMode.EditorSimulateMode)
  267. {
  268. var operation = new EditorPlayModeUpdateManifestOperation();
  269. OperationSystem.StartOperation(operation);
  270. return operation;
  271. }
  272. else if (_playMode == EPlayMode.OfflinePlayMode)
  273. {
  274. var operation = new OfflinePlayModeUpdateManifestOperation();
  275. OperationSystem.StartOperation(operation);
  276. return operation;
  277. }
  278. else if (_playMode == EPlayMode.HostPlayMode)
  279. {
  280. return _hostPlayModeImpl.UpdatePatchManifestAsync(resourceVersion, timeout);
  281. }
  282. else
  283. {
  284. throw new NotImplementedException();
  285. }
  286. }
  287. /// <summary>
  288. /// 弱联网情况下加载补丁清单
  289. /// 注意:当指定版本内容验证失败后会返回失败。
  290. /// </summary>
  291. /// <param name="resourceVersion">指定的资源版本</param>
  292. public static UpdateManifestOperation WeaklyUpdateManifestAsync(int resourceVersion)
  293. {
  294. DebugCheckInitialize();
  295. if (_playMode == EPlayMode.EditorSimulateMode)
  296. {
  297. var operation = new EditorPlayModeUpdateManifestOperation();
  298. OperationSystem.StartOperation(operation);
  299. return operation;
  300. }
  301. else if (_playMode == EPlayMode.OfflinePlayMode)
  302. {
  303. var operation = new OfflinePlayModeUpdateManifestOperation();
  304. OperationSystem.StartOperation(operation);
  305. return operation;
  306. }
  307. else if (_playMode == EPlayMode.HostPlayMode)
  308. {
  309. return _hostPlayModeImpl.WeaklyUpdatePatchManifestAsync(resourceVersion);
  310. }
  311. else
  312. {
  313. throw new NotImplementedException();
  314. }
  315. }
  316. /// <summary>
  317. /// 开启一个异步操作
  318. /// </summary>
  319. /// <param name="operation">异步操作对象</param>
  320. public static void StartOperation(GameAsyncOperation operation)
  321. {
  322. OperationSystem.StartOperation(operation);
  323. }
  324. /// <summary>
  325. /// 获取资源版本号
  326. /// </summary>
  327. public static int GetResourceVersion()
  328. {
  329. DebugCheckInitialize();
  330. if (_playMode == EPlayMode.EditorSimulateMode)
  331. {
  332. return _editorSimulateModeImpl.GetResourceVersion();
  333. }
  334. else if (_playMode == EPlayMode.OfflinePlayMode)
  335. {
  336. return _offlinePlayModeImpl.GetResourceVersion();
  337. }
  338. else if (_playMode == EPlayMode.HostPlayMode)
  339. {
  340. return _hostPlayModeImpl.GetResourceVersion();
  341. }
  342. else
  343. {
  344. throw new NotImplementedException();
  345. }
  346. }
  347. /// <summary>
  348. /// 资源回收(卸载引用计数为零的资源)
  349. /// </summary>
  350. public static void UnloadUnusedAssets()
  351. {
  352. if (_isInitialize)
  353. {
  354. AssetSystem.Update();
  355. AssetSystem.UnloadUnusedAssets();
  356. }
  357. }
  358. /// <summary>
  359. /// 强制回收所有资源
  360. /// </summary>
  361. public static void ForceUnloadAllAssets()
  362. {
  363. if (_isInitialize)
  364. {
  365. AssetSystem.ForceUnloadAllAssets();
  366. }
  367. }
  368. #region 资源信息
  369. /// <summary>
  370. /// 是否需要从远端更新下载
  371. /// </summary>
  372. /// <param name="location">资源的定位地址</param>
  373. public static bool IsNeedDownloadFromRemote(string location)
  374. {
  375. DebugCheckInitialize();
  376. AssetInfo assetInfo = ConvertLocationToAssetInfo(location, null);
  377. if (assetInfo.IsInvalid)
  378. return false;
  379. BundleInfo bundleInfo = _bundleServices.GetBundleInfo(assetInfo);
  380. if (bundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromRemote)
  381. return true;
  382. else
  383. return false;
  384. }
  385. /// <summary>
  386. /// 是否需要从远端更新下载
  387. /// </summary>
  388. /// <param name="location">资源的定位地址</param>
  389. public static bool IsNeedDownloadFromRemote(AssetInfo assetInfo)
  390. {
  391. DebugCheckInitialize();
  392. if (assetInfo.IsInvalid)
  393. {
  394. YooLogger.Warning(assetInfo.Error);
  395. return false;
  396. }
  397. BundleInfo bundleInfo = _bundleServices.GetBundleInfo(assetInfo);
  398. if (bundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromRemote)
  399. return true;
  400. else
  401. return false;
  402. }
  403. /// <summary>
  404. /// 获取资源信息列表
  405. /// </summary>
  406. /// <param name="tag">资源标签</param>
  407. public static AssetInfo[] GetAssetInfos(string tag)
  408. {
  409. DebugCheckInitialize();
  410. string[] tags = new string[] { tag };
  411. return _bundleServices.GetAssetInfos(tags);
  412. }
  413. /// <summary>
  414. /// 获取资源信息列表
  415. /// </summary>
  416. /// <param name="tags">资源标签列表</param>
  417. public static AssetInfo[] GetAssetInfos(string[] tags)
  418. {
  419. DebugCheckInitialize();
  420. return _bundleServices.GetAssetInfos(tags);
  421. }
  422. /// <summary>
  423. /// 获取资源信息
  424. /// </summary>
  425. /// <param name="location">资源的定位地址</param>
  426. public static AssetInfo GetAssetInfo(string location)
  427. {
  428. DebugCheckInitialize();
  429. AssetInfo assetInfo = ConvertLocationToAssetInfo(location, null);
  430. return assetInfo;
  431. }
  432. /// <summary>
  433. /// 获取资源路径
  434. /// </summary>
  435. /// <param name="location">资源的定位地址</param>
  436. /// <returns>如果location地址无效,则返回空字符串</returns>
  437. public static string GetAssetPath(string location)
  438. {
  439. DebugCheckInitialize();
  440. return _locationServices.ConvertLocationToAssetPath(location);
  441. }
  442. #endregion
  443. #region 原生文件
  444. /// <summary>
  445. /// 异步获取原生文件
  446. /// </summary>
  447. /// <param name="location">资源的定位地址</param>
  448. /// <param name="copyPath">拷贝路径</param>
  449. public static RawFileOperation GetRawFileAsync(string location, string copyPath = null)
  450. {
  451. DebugCheckInitialize();
  452. AssetInfo assetInfo = ConvertLocationToAssetInfo(location, null);
  453. return GetRawFileInternal(assetInfo, copyPath);
  454. }
  455. /// <summary>
  456. /// 异步获取原生文件
  457. /// </summary>
  458. /// <param name="assetInfo">资源信息</param>
  459. /// <param name="copyPath">拷贝路径</param>
  460. public static RawFileOperation GetRawFileAsync(AssetInfo assetInfo, string copyPath = null)
  461. {
  462. DebugCheckInitialize();
  463. if (assetInfo.IsInvalid)
  464. YooLogger.Warning(assetInfo.Error);
  465. return GetRawFileInternal(assetInfo, copyPath);
  466. }
  467. private static RawFileOperation GetRawFileInternal(AssetInfo assetInfo, string copyPath)
  468. {
  469. if (assetInfo.IsInvalid)
  470. {
  471. RawFileOperation operation = new CompletedRawFileOperation(assetInfo.Error, copyPath);
  472. OperationSystem.StartOperation(operation);
  473. return operation;
  474. }
  475. BundleInfo bundleInfo = _bundleServices.GetBundleInfo(assetInfo);
  476. #if UNITY_EDITOR
  477. if (bundleInfo.Bundle.IsRawFile == false)
  478. {
  479. string error = $"Cannot load asset bundle file using {nameof(GetRawFileAsync)} method !";
  480. YooLogger.Error(error);
  481. RawFileOperation operation = new CompletedRawFileOperation(error, copyPath);
  482. OperationSystem.StartOperation(operation);
  483. return operation;
  484. }
  485. #endif
  486. if (_playMode == EPlayMode.EditorSimulateMode)
  487. {
  488. RawFileOperation operation = new EditorPlayModeRawFileOperation(bundleInfo, copyPath);
  489. OperationSystem.StartOperation(operation);
  490. return operation;
  491. }
  492. else if (_playMode == EPlayMode.OfflinePlayMode)
  493. {
  494. RawFileOperation operation = new OfflinePlayModeRawFileOperation(bundleInfo, copyPath);
  495. OperationSystem.StartOperation(operation);
  496. return operation;
  497. }
  498. else if (_playMode == EPlayMode.HostPlayMode)
  499. {
  500. RawFileOperation operation = new HostPlayModeRawFileOperation(bundleInfo, copyPath);
  501. OperationSystem.StartOperation(operation);
  502. return operation;
  503. }
  504. else
  505. {
  506. throw new NotImplementedException();
  507. }
  508. }
  509. #endregion
  510. #region 场景加载
  511. /// <summary>
  512. /// 异步加载场景
  513. /// </summary>
  514. /// <param name="location">场景的定位地址</param>
  515. /// <param name="sceneMode">场景加载模式</param>
  516. /// <param name="activateOnLoad">加载完毕时是否主动激活</param>
  517. /// <param name="priority">优先级</param>
  518. public static SceneOperationHandle LoadSceneAsync(string location, LoadSceneMode sceneMode = LoadSceneMode.Single, bool activateOnLoad = true, int priority = 100)
  519. {
  520. DebugCheckInitialize();
  521. AssetInfo assetInfo = ConvertLocationToAssetInfo(location, null);
  522. var handle = AssetSystem.LoadSceneAsync(assetInfo, sceneMode, activateOnLoad, priority);
  523. return handle;
  524. }
  525. /// <summary>
  526. /// 异步加载场景
  527. /// </summary>
  528. /// <param name="assetInfo">场景的资源信息</param>
  529. /// <param name="sceneMode">场景加载模式</param>
  530. /// <param name="activateOnLoad">加载完毕时是否主动激活</param>
  531. /// <param name="priority">优先级</param>
  532. public static SceneOperationHandle LoadSceneAsync(AssetInfo assetInfo, LoadSceneMode sceneMode = LoadSceneMode.Single, bool activateOnLoad = true, int priority = 100)
  533. {
  534. DebugCheckInitialize();
  535. if (assetInfo.IsInvalid)
  536. YooLogger.Warning(assetInfo.Error);
  537. var handle = AssetSystem.LoadSceneAsync(assetInfo, sceneMode, activateOnLoad, priority);
  538. return handle;
  539. }
  540. #endregion
  541. #region 资源加载
  542. /// <summary>
  543. /// 同步加载资源对象
  544. /// </summary>
  545. /// <param name="assetInfo">资源信息</param>
  546. public static AssetOperationHandle LoadAssetSync(AssetInfo assetInfo)
  547. {
  548. DebugCheckInitialize();
  549. if (assetInfo.IsInvalid)
  550. YooLogger.Warning(assetInfo.Error);
  551. return LoadAssetInternal(assetInfo, true);
  552. }
  553. /// <summary>
  554. /// 同步加载资源对象
  555. /// </summary>
  556. /// <typeparam name="TObject">资源类型</typeparam>
  557. /// <param name="location">资源的定位地址</param>
  558. public static AssetOperationHandle LoadAssetSync<TObject>(string location) where TObject : UnityEngine.Object
  559. {
  560. DebugCheckInitialize();
  561. AssetInfo assetInfo = ConvertLocationToAssetInfo(location, typeof(TObject));
  562. return LoadAssetInternal(assetInfo, true);
  563. }
  564. /// <summary>
  565. /// 同步加载资源对象
  566. /// </summary>
  567. /// <param name="location">资源的定位地址</param>
  568. /// <param name="type">资源类型</param>
  569. public static AssetOperationHandle LoadAssetSync(string location, System.Type type)
  570. {
  571. DebugCheckInitialize();
  572. AssetInfo assetInfo = ConvertLocationToAssetInfo(location, type);
  573. return LoadAssetInternal(assetInfo, true);
  574. }
  575. /// <summary>
  576. /// 异步加载资源对象
  577. /// </summary>
  578. /// <param name="assetInfo">资源信息</param>
  579. public static AssetOperationHandle LoadAssetAsync(AssetInfo assetInfo)
  580. {
  581. DebugCheckInitialize();
  582. if (assetInfo.IsInvalid)
  583. YooLogger.Warning(assetInfo.Error);
  584. return LoadAssetInternal(assetInfo, false);
  585. }
  586. /// <summary>
  587. /// 异步加载资源对象
  588. /// </summary>
  589. /// <typeparam name="TObject">资源类型</typeparam>
  590. /// <param name="location">资源的定位地址</param>
  591. public static AssetOperationHandle LoadAssetAsync<TObject>(string location) where TObject : UnityEngine.Object
  592. {
  593. DebugCheckInitialize();
  594. AssetInfo assetInfo = ConvertLocationToAssetInfo(location, typeof(TObject));
  595. return LoadAssetInternal(assetInfo, false);
  596. }
  597. /// <summary>
  598. /// 异步加载资源对象
  599. /// </summary>
  600. /// <param name="location">资源的定位地址</param>
  601. /// <param name="type">资源类型</param>
  602. public static AssetOperationHandle LoadAssetAsync(string location, System.Type type)
  603. {
  604. DebugCheckInitialize();
  605. AssetInfo assetInfo = ConvertLocationToAssetInfo(location, type);
  606. return LoadAssetInternal(assetInfo, false);
  607. }
  608. private static AssetOperationHandle LoadAssetInternal(AssetInfo assetInfo, bool waitForAsyncComplete)
  609. {
  610. #if UNITY_EDITOR
  611. BundleInfo bundleInfo = _bundleServices.GetBundleInfo(assetInfo);
  612. if (bundleInfo.Bundle.IsRawFile)
  613. {
  614. string error = $"Cannot load raw file using LoadAsset method !";
  615. YooLogger.Error(error);
  616. CompletedProvider completedProvider = new CompletedProvider(assetInfo);
  617. completedProvider.SetCompleted(error);
  618. return completedProvider.CreateHandle<AssetOperationHandle>();
  619. }
  620. #endif
  621. var handle = AssetSystem.LoadAssetAsync(assetInfo);
  622. if (waitForAsyncComplete)
  623. handle.WaitForAsyncComplete();
  624. return handle;
  625. }
  626. #endregion
  627. #region 资源加载
  628. /// <summary>
  629. /// 同步加载子资源对象
  630. /// </summary>
  631. /// <param name="assetInfo">资源信息</param>
  632. public static SubAssetsOperationHandle LoadSubAssetsSync(AssetInfo assetInfo)
  633. {
  634. DebugCheckInitialize();
  635. if (assetInfo.IsInvalid)
  636. YooLogger.Warning(assetInfo.Error);
  637. return LoadSubAssetsInternal(assetInfo, true);
  638. }
  639. /// <summary>
  640. /// 同步加载子资源对象
  641. /// </summary>
  642. /// <typeparam name="TObject">资源类型</typeparam>
  643. /// <param name="location">资源的定位地址</param>
  644. public static SubAssetsOperationHandle LoadSubAssetsSync<TObject>(string location) where TObject : UnityEngine.Object
  645. {
  646. DebugCheckInitialize();
  647. AssetInfo assetInfo = ConvertLocationToAssetInfo(location, typeof(TObject));
  648. return LoadSubAssetsInternal(assetInfo, true);
  649. }
  650. /// <summary>
  651. /// 同步加载子资源对象
  652. /// </summary>
  653. /// <param name="location">资源的定位地址</param>
  654. /// <param name="type">子对象类型</param>
  655. public static SubAssetsOperationHandle LoadSubAssetsSync(string location, System.Type type)
  656. {
  657. DebugCheckInitialize();
  658. AssetInfo assetInfo = ConvertLocationToAssetInfo(location, type);
  659. return LoadSubAssetsInternal(assetInfo, true);
  660. }
  661. /// <summary>
  662. /// 异步加载子资源对象
  663. /// </summary>
  664. /// <param name="assetInfo">资源信息</param>
  665. public static SubAssetsOperationHandle LoadSubAssetsAsync(AssetInfo assetInfo)
  666. {
  667. DebugCheckInitialize();
  668. if (assetInfo.IsInvalid)
  669. YooLogger.Warning(assetInfo.Error);
  670. return LoadSubAssetsInternal(assetInfo, false);
  671. }
  672. /// <summary>
  673. /// 异步加载子资源对象
  674. /// </summary>
  675. /// <typeparam name="TObject">资源类型</typeparam>
  676. /// <param name="location">资源的定位地址</param>
  677. public static SubAssetsOperationHandle LoadSubAssetsAsync<TObject>(string location) where TObject : UnityEngine.Object
  678. {
  679. DebugCheckInitialize();
  680. AssetInfo assetInfo = ConvertLocationToAssetInfo(location, typeof(TObject));
  681. return LoadSubAssetsInternal(assetInfo, false);
  682. }
  683. /// <summary>
  684. /// 异步加载子资源对象
  685. /// </summary>
  686. /// <param name="location">资源的定位地址</param>
  687. /// <param name="type">子对象类型</param>
  688. public static SubAssetsOperationHandle LoadSubAssetsAsync(string location, System.Type type)
  689. {
  690. DebugCheckInitialize();
  691. AssetInfo assetInfo = ConvertLocationToAssetInfo(location, type);
  692. return LoadSubAssetsInternal(assetInfo, false);
  693. }
  694. private static SubAssetsOperationHandle LoadSubAssetsInternal(AssetInfo assetInfo, bool waitForAsyncComplete)
  695. {
  696. #if UNITY_EDITOR
  697. BundleInfo bundleInfo = _bundleServices.GetBundleInfo(assetInfo);
  698. if (bundleInfo.Bundle.IsRawFile)
  699. {
  700. string error = $"Cannot load raw file using LoadSubAssets method !";
  701. YooLogger.Error(error);
  702. CompletedProvider completedProvider = new CompletedProvider(assetInfo);
  703. completedProvider.SetCompleted(error);
  704. return completedProvider.CreateHandle<SubAssetsOperationHandle>();
  705. }
  706. #endif
  707. var handle = AssetSystem.LoadSubAssetsAsync(assetInfo);
  708. if (waitForAsyncComplete)
  709. handle.WaitForAsyncComplete();
  710. return handle;
  711. }
  712. #endregion
  713. #region 资源下载
  714. /// <summary>
  715. /// 创建补丁下载器,用于下载更新资源标签指定的资源包文件
  716. /// </summary>
  717. /// <param name="tag">资源标签</param>
  718. /// <param name="downloadingMaxNumber">同时下载的最大文件数</param>
  719. /// <param name="failedTryAgain">下载失败的重试次数</param>
  720. public static PatchDownloaderOperation CreatePatchDownloader(string tag, int downloadingMaxNumber, int failedTryAgain)
  721. {
  722. DebugCheckInitialize();
  723. return CreatePatchDownloader(new string[] { tag }, downloadingMaxNumber, failedTryAgain);
  724. }
  725. /// <summary>
  726. /// 创建补丁下载器,用于下载更新资源标签指定的资源包文件
  727. /// </summary>
  728. /// <param name="tags">资源标签列表</param>
  729. /// <param name="downloadingMaxNumber">同时下载的最大文件数</param>
  730. /// <param name="failedTryAgain">下载失败的重试次数</param>
  731. public static PatchDownloaderOperation CreatePatchDownloader(string[] tags, int downloadingMaxNumber, int failedTryAgain)
  732. {
  733. DebugCheckInitialize();
  734. if (_playMode == EPlayMode.EditorSimulateMode || _playMode == EPlayMode.OfflinePlayMode)
  735. {
  736. List<BundleInfo> downloadList = new List<BundleInfo>();
  737. var operation = new PatchDownloaderOperation(downloadList, downloadingMaxNumber, failedTryAgain);
  738. return operation;
  739. }
  740. else if (_playMode == EPlayMode.HostPlayMode)
  741. {
  742. return _hostPlayModeImpl.CreatePatchDownloaderByTags(tags, downloadingMaxNumber, failedTryAgain);
  743. }
  744. else
  745. {
  746. throw new NotImplementedException();
  747. }
  748. }
  749. /// <summary>
  750. /// 创建补丁下载器,用于下载更新当前资源版本所有的资源包文件
  751. /// </summary>
  752. /// <param name="downloadingMaxNumber">同时下载的最大文件数</param>
  753. /// <param name="failedTryAgain">下载失败的重试次数</param>
  754. public static PatchDownloaderOperation CreatePatchDownloader(int downloadingMaxNumber, int failedTryAgain)
  755. {
  756. DebugCheckInitialize();
  757. if (_playMode == EPlayMode.EditorSimulateMode || _playMode == EPlayMode.OfflinePlayMode)
  758. {
  759. List<BundleInfo> downloadList = new List<BundleInfo>();
  760. var operation = new PatchDownloaderOperation(downloadList, downloadingMaxNumber, failedTryAgain);
  761. return operation;
  762. }
  763. else if (_playMode == EPlayMode.HostPlayMode)
  764. {
  765. return _hostPlayModeImpl.CreatePatchDownloaderByAll(downloadingMaxNumber, failedTryAgain);
  766. }
  767. else
  768. {
  769. throw new NotImplementedException();
  770. }
  771. }
  772. /// <summary>
  773. /// 创建补丁下载器,用于下载更新指定的资源列表依赖的资源包文件
  774. /// </summary>
  775. /// <param name="locations">资源定位列表</param>
  776. /// <param name="downloadingMaxNumber">同时下载的最大文件数</param>
  777. /// <param name="failedTryAgain">下载失败的重试次数</param>
  778. public static PatchDownloaderOperation CreateBundleDownloader(string[] locations, int downloadingMaxNumber, int failedTryAgain)
  779. {
  780. DebugCheckInitialize();
  781. if (_playMode == EPlayMode.EditorSimulateMode || _playMode == EPlayMode.OfflinePlayMode)
  782. {
  783. List<BundleInfo> downloadList = new List<BundleInfo>();
  784. var operation = new PatchDownloaderOperation(downloadList, downloadingMaxNumber, failedTryAgain);
  785. return operation;
  786. }
  787. else if (_playMode == EPlayMode.HostPlayMode)
  788. {
  789. List<AssetInfo> assetInfos = new List<AssetInfo>(locations.Length);
  790. foreach (var location in locations)
  791. {
  792. AssetInfo assetInfo = ConvertLocationToAssetInfo(location, null);
  793. assetInfos.Add(assetInfo);
  794. }
  795. return _hostPlayModeImpl.CreatePatchDownloaderByPaths(assetInfos.ToArray(), downloadingMaxNumber, failedTryAgain);
  796. }
  797. else
  798. {
  799. throw new NotImplementedException();
  800. }
  801. }
  802. /// <summary>
  803. /// 创建补丁下载器,用于下载更新指定的资源列表依赖的资源包文件
  804. /// </summary>
  805. /// <param name="assetInfos">资源信息列表</param>
  806. /// <param name="downloadingMaxNumber">同时下载的最大文件数</param>
  807. /// <param name="failedTryAgain">下载失败的重试次数</param>
  808. public static PatchDownloaderOperation CreateBundleDownloader(AssetInfo[] assetInfos, int downloadingMaxNumber, int failedTryAgain)
  809. {
  810. DebugCheckInitialize();
  811. if (_playMode == EPlayMode.EditorSimulateMode || _playMode == EPlayMode.OfflinePlayMode)
  812. {
  813. List<BundleInfo> downloadList = new List<BundleInfo>();
  814. var operation = new PatchDownloaderOperation(downloadList, downloadingMaxNumber, failedTryAgain);
  815. return operation;
  816. }
  817. else if (_playMode == EPlayMode.HostPlayMode)
  818. {
  819. return _hostPlayModeImpl.CreatePatchDownloaderByPaths(assetInfos, downloadingMaxNumber, failedTryAgain);
  820. }
  821. else
  822. {
  823. throw new NotImplementedException();
  824. }
  825. }
  826. #endregion
  827. #region 资源解压
  828. /// <summary>
  829. /// 创建补丁解压器
  830. /// </summary>
  831. /// <param name="tag">资源标签</param>
  832. /// <param name="unpackingMaxNumber">同时解压的最大文件数</param>
  833. /// <param name="failedTryAgain">解压失败的重试次数</param>
  834. public static PatchUnpackerOperation CreatePatchUnpacker(string tag, int unpackingMaxNumber, int failedTryAgain)
  835. {
  836. DebugCheckInitialize();
  837. return CreatePatchUnpacker(new string[] { tag }, unpackingMaxNumber, failedTryAgain);
  838. }
  839. /// <summary>
  840. /// 创建补丁解压器
  841. /// </summary>
  842. /// <param name="tags">资源标签列表</param>
  843. /// <param name="unpackingMaxNumber">同时解压的最大文件数</param>
  844. /// <param name="failedTryAgain">解压失败的重试次数</param>
  845. public static PatchUnpackerOperation CreatePatchUnpacker(string[] tags, int unpackingMaxNumber, int failedTryAgain)
  846. {
  847. DebugCheckInitialize();
  848. if (_playMode == EPlayMode.EditorSimulateMode)
  849. {
  850. List<BundleInfo> downloadList = new List<BundleInfo>();
  851. var operation = new PatchUnpackerOperation(downloadList, unpackingMaxNumber, failedTryAgain);
  852. return operation;
  853. }
  854. else if (_playMode == EPlayMode.OfflinePlayMode)
  855. {
  856. List<BundleInfo> downloadList = new List<BundleInfo>();
  857. var operation = new PatchUnpackerOperation(downloadList, unpackingMaxNumber, failedTryAgain);
  858. return operation;
  859. }
  860. else if (_playMode == EPlayMode.HostPlayMode)
  861. {
  862. return _hostPlayModeImpl.CreatePatchUnpackerByTags(tags, unpackingMaxNumber, failedTryAgain);
  863. }
  864. else
  865. {
  866. throw new NotImplementedException();
  867. }
  868. }
  869. /// <summary>
  870. /// 创建补丁解压器
  871. /// </summary>
  872. /// <param name="unpackingMaxNumber">同时解压的最大文件数</param>
  873. /// <param name="failedTryAgain">解压失败的重试次数</param>
  874. public static PatchUnpackerOperation CreatePatchUnpacker(int unpackingMaxNumber, int failedTryAgain)
  875. {
  876. DebugCheckInitialize();
  877. if (_playMode == EPlayMode.EditorSimulateMode)
  878. {
  879. List<BundleInfo> downloadList = new List<BundleInfo>();
  880. var operation = new PatchUnpackerOperation(downloadList, unpackingMaxNumber, failedTryAgain);
  881. return operation;
  882. }
  883. else if (_playMode == EPlayMode.OfflinePlayMode)
  884. {
  885. List<BundleInfo> downloadList = new List<BundleInfo>();
  886. var operation = new PatchUnpackerOperation(downloadList, unpackingMaxNumber, failedTryAgain);
  887. return operation;
  888. }
  889. else if (_playMode == EPlayMode.HostPlayMode)
  890. {
  891. return _hostPlayModeImpl.CreatePatchUnpackerByAll(unpackingMaxNumber, failedTryAgain);
  892. }
  893. else
  894. {
  895. throw new NotImplementedException();
  896. }
  897. }
  898. #endregion
  899. #region 包裹更新
  900. /// <summary>
  901. /// 创建资源包裹下载器,用于下载更新指定资源版本所有的资源包文件
  902. /// </summary>
  903. /// <param name="resourceVersion">指定更新的资源版本</param>
  904. /// <param name="timeout">超时时间</param>
  905. public static UpdatePackageOperation UpdatePackageAsync(int resourceVersion, int timeout = 60)
  906. {
  907. DebugCheckInitialize();
  908. if (_playMode == EPlayMode.EditorSimulateMode)
  909. {
  910. var operation = new EditorPlayModeUpdatePackageOperation();
  911. OperationSystem.StartOperation(operation);
  912. return operation;
  913. }
  914. else if (_playMode == EPlayMode.OfflinePlayMode)
  915. {
  916. var operation = new OfflinePlayModeUpdatePackageOperation();
  917. OperationSystem.StartOperation(operation);
  918. return operation;
  919. }
  920. else if (_playMode == EPlayMode.HostPlayMode)
  921. {
  922. return _hostPlayModeImpl.UpdatePackageAsync(resourceVersion, timeout);
  923. }
  924. else
  925. {
  926. throw new NotImplementedException();
  927. }
  928. }
  929. #endregion
  930. #region 沙盒相关
  931. /// <summary>
  932. /// 获取沙盒的根路径
  933. /// </summary>
  934. public static string GetSandboxRoot()
  935. {
  936. return PathHelper.MakePersistentRootPath();
  937. }
  938. /// <summary>
  939. /// 清空沙盒目录
  940. /// </summary>
  941. public static void ClearSandbox()
  942. {
  943. SandboxHelper.DeleteSandbox();
  944. }
  945. /// <summary>
  946. /// 清空所有的缓存文件
  947. /// </summary>
  948. public static void ClearAllCacheFiles()
  949. {
  950. SandboxHelper.DeleteCacheFolder();
  951. }
  952. /// <summary>
  953. /// 清空未被使用的缓存文件
  954. /// </summary>
  955. public static ClearUnusedCacheFilesOperation ClearUnusedCacheFiles()
  956. {
  957. DebugCheckInitialize();
  958. if (_playMode == EPlayMode.EditorSimulateMode)
  959. {
  960. var operation = new EditorPlayModeClearUnusedCacheFilesOperation();
  961. OperationSystem.StartOperation(operation);
  962. return operation;
  963. }
  964. else if (_playMode == EPlayMode.OfflinePlayMode)
  965. {
  966. var operation = new OfflinePlayModeClearUnusedCacheFilesOperation();
  967. OperationSystem.StartOperation(operation);
  968. return operation;
  969. }
  970. else if (_playMode == EPlayMode.HostPlayMode)
  971. {
  972. var operation = new HostPlayModeClearUnusedCacheFilesOperation(_hostPlayModeImpl);
  973. OperationSystem.StartOperation(operation);
  974. return operation;
  975. }
  976. else
  977. {
  978. throw new NotImplementedException();
  979. }
  980. }
  981. #endregion
  982. #region 内部方法
  983. internal static void InternalDestroy()
  984. {
  985. if (_isInitialize)
  986. {
  987. _isInitialize = false;
  988. _initializeError = string.Empty;
  989. _initializeStatus = EOperationStatus.None;
  990. _bundleServices = null;
  991. _locationServices = null;
  992. _editorSimulateModeImpl = null;
  993. _offlinePlayModeImpl = null;
  994. _hostPlayModeImpl = null;
  995. OperationSystem.DestroyAll();
  996. DownloadSystem.DestroyAll();
  997. CacheSystem.DestroyAll();
  998. AssetSystem.DestroyAll();
  999. YooLogger.Log("YooAssets destroy all !");
  1000. }
  1001. }
  1002. internal static void InternalUpdate()
  1003. {
  1004. OperationSystem.Update();
  1005. DownloadSystem.Update();
  1006. AssetSystem.Update();
  1007. }
  1008. /// <summary>
  1009. /// 资源定位地址转换为资源完整路径
  1010. /// </summary>
  1011. internal static string MappingToAssetPath(string location)
  1012. {
  1013. return _bundleServices.MappingToAssetPath(location);
  1014. }
  1015. #endregion
  1016. #region 调试方法
  1017. [Conditional("DEBUG")]
  1018. private static void DebugCheckInitialize()
  1019. {
  1020. if (_initializeStatus == EOperationStatus.None)
  1021. throw new Exception("YooAssets initialize not completed !");
  1022. else if (_initializeStatus == EOperationStatus.Failed)
  1023. throw new Exception($"YooAssets initialize failed : {_initializeError}");
  1024. }
  1025. [Conditional("DEBUG")]
  1026. private static void DebugCheckLocation(string location)
  1027. {
  1028. if (string.IsNullOrEmpty(location) == false)
  1029. {
  1030. // 检查路径末尾是否有空格
  1031. int index = location.LastIndexOf(" ");
  1032. if (index != -1)
  1033. {
  1034. if (location.Length == index + 1)
  1035. YooLogger.Warning($"Found blank character in location : \"{location}\"");
  1036. }
  1037. if (location.IndexOfAny(System.IO.Path.GetInvalidPathChars()) >= 0)
  1038. YooLogger.Warning($"Found illegal character in location : \"{location}\"");
  1039. }
  1040. }
  1041. [Conditional("DEBUG")]
  1042. private static void DebugCheckUpdateManifest()
  1043. {
  1044. var loadedBundleInfos = AssetSystem.GetLoadedBundleInfos();
  1045. if (loadedBundleInfos.Count > 0)
  1046. {
  1047. YooLogger.Warning($"Found loaded bundle before update manifest ! Recommended to call the {nameof(ForceUnloadAllAssets)} method to release loaded bundle !");
  1048. }
  1049. }
  1050. #endregion
  1051. #region 私有方法
  1052. /// <summary>
  1053. /// 资源定位地址转换为资源信息类,失败时内部会发出错误日志。
  1054. /// </summary>
  1055. /// <returns>如果转换失败会返回一个无效的资源信息类</returns>
  1056. private static AssetInfo ConvertLocationToAssetInfo(string location, System.Type assetType)
  1057. {
  1058. DebugCheckLocation(location);
  1059. string assetPath = _locationServices.ConvertLocationToAssetPath(location);
  1060. PatchAsset patchAsset = _bundleServices.TryGetPatchAsset(assetPath);
  1061. if (patchAsset != null)
  1062. {
  1063. AssetInfo assetInfo = new AssetInfo(patchAsset, assetType);
  1064. return assetInfo;
  1065. }
  1066. else
  1067. {
  1068. string error;
  1069. if (string.IsNullOrEmpty(location))
  1070. error = $"The location is null or empty !";
  1071. else
  1072. error = $"The location is invalid : {location}";
  1073. YooLogger.Error(error);
  1074. AssetInfo assetInfo = new AssetInfo(error);
  1075. return assetInfo;
  1076. }
  1077. }
  1078. #endregion
  1079. }
  1080. }