ResourcePackage.cs 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190
  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 class ResourcePackage
  9. {
  10. private bool _isInitialize = false;
  11. private string _initializeError = string.Empty;
  12. private EOperationStatus _initializeStatus = EOperationStatus.None;
  13. private EPlayMode _playMode;
  14. // 管理器
  15. private CacheManager _cacheMgr;
  16. private PersistentManager _persistentMgr;
  17. private DownloadManager _downloadMgr;
  18. private ResourceManager _resourceMgr;
  19. private ResourceLoader _resourceLoader;
  20. private IBundleQuery _bundleQuery;
  21. private IPlayMode _playModeImpl;
  22. /// <summary>
  23. /// 包裹名
  24. /// </summary>
  25. public readonly string PackageName;
  26. /// <summary>
  27. /// 初始化状态
  28. /// </summary>
  29. public EOperationStatus InitializeStatus
  30. {
  31. get { return _initializeStatus; }
  32. }
  33. private ResourcePackage()
  34. {
  35. }
  36. internal ResourcePackage(string packageName)
  37. {
  38. PackageName = packageName;
  39. }
  40. /// <summary>
  41. /// 更新资源包裹
  42. /// </summary>
  43. internal void UpdatePackage()
  44. {
  45. if (_resourceMgr != null)
  46. _resourceMgr.Update();
  47. if (_downloadMgr != null)
  48. _downloadMgr.Update();
  49. }
  50. /// <summary>
  51. /// 销毁资源包裹
  52. /// </summary>
  53. internal void DestroyPackage()
  54. {
  55. if (_isInitialize)
  56. {
  57. _isInitialize = false;
  58. _initializeError = string.Empty;
  59. _initializeStatus = EOperationStatus.None;
  60. _bundleQuery = null;
  61. _playModeImpl = null;
  62. _persistentMgr = null;
  63. _resourceLoader = null;
  64. if (_resourceMgr != null)
  65. {
  66. _resourceMgr.ForceUnloadAllAssets();
  67. _resourceMgr = null;
  68. }
  69. if (_downloadMgr != null)
  70. {
  71. _downloadMgr.DestroyAll();
  72. _downloadMgr = null;
  73. }
  74. if (_cacheMgr != null)
  75. {
  76. _cacheMgr.ClearAll();
  77. _cacheMgr = null;
  78. }
  79. // 最后清理该包裹的异步任务
  80. // 注意:对于有线程操作的异步任务,需要保证线程安全释放。
  81. OperationSystem.ClearPackageOperation(PackageName);
  82. }
  83. }
  84. /// <summary>
  85. /// 异步初始化
  86. /// </summary>
  87. public InitializationOperation InitializeAsync(InitializeParameters parameters)
  88. {
  89. // 注意:WebGL平台因为网络原因可能会初始化失败!
  90. ResetInitializeAfterFailed();
  91. // 检测初始化参数合法性
  92. CheckInitializeParameters(parameters);
  93. // 创建缓存管理器
  94. _cacheMgr = new CacheManager(PackageName, parameters.CacheBootVerifyLevel);
  95. // 创建持久化管理器
  96. _persistentMgr = new PersistentManager(PackageName);
  97. _persistentMgr.Initialize(parameters.BuildinRootDirectory, parameters.SandboxRootDirectory, parameters.CacheFileAppendExtension);
  98. // 创建下载管理器
  99. _downloadMgr = new DownloadManager(PackageName);
  100. _downloadMgr.Initialize(parameters.BreakpointResumeFileSize);
  101. // 创建资源包加载器
  102. if (_playMode == EPlayMode.HostPlayMode)
  103. {
  104. var initializeParameters = parameters as HostPlayModeParameters;
  105. _resourceLoader = new ResourceLoader();
  106. _resourceLoader.Init(parameters.DecryptionServices, initializeParameters.DeliveryLoadServices);
  107. }
  108. else
  109. {
  110. _resourceLoader = new ResourceLoader();
  111. _resourceLoader.Init(parameters.DecryptionServices, null);
  112. }
  113. // 创建资源协助类
  114. ResourceAssist assist = new ResourceAssist();
  115. assist.Cache = _cacheMgr;
  116. assist.Persistent = _persistentMgr;
  117. assist.Download = _downloadMgr;
  118. assist.Loader = _resourceLoader;
  119. // 创建资源管理器
  120. InitializationOperation initializeOperation;
  121. _resourceMgr = new ResourceManager(PackageName);
  122. if (_playMode == EPlayMode.EditorSimulateMode)
  123. {
  124. var editorSimulateModeImpl = new EditorSimulateModeImpl(PackageName);
  125. _bundleQuery = editorSimulateModeImpl;
  126. _playModeImpl = editorSimulateModeImpl;
  127. _resourceMgr.Initialize(true, parameters.AutoDestroyAssetProvider, _bundleQuery);
  128. var initializeParameters = parameters as EditorSimulateModeParameters;
  129. initializeOperation = editorSimulateModeImpl.InitializeAsync(assist, initializeParameters.SimulateManifestFilePath);
  130. }
  131. else if (_playMode == EPlayMode.OfflinePlayMode)
  132. {
  133. var offlinePlayModeImpl = new OfflinePlayModeImpl(PackageName);
  134. _bundleQuery = offlinePlayModeImpl;
  135. _playModeImpl = offlinePlayModeImpl;
  136. _resourceMgr.Initialize(false, parameters.AutoDestroyAssetProvider, _bundleQuery);
  137. var initializeParameters = parameters as OfflinePlayModeParameters;
  138. initializeOperation = offlinePlayModeImpl.InitializeAsync(assist);
  139. }
  140. else if (_playMode == EPlayMode.HostPlayMode)
  141. {
  142. var hostPlayModeImpl = new HostPlayModeImpl(PackageName);
  143. _bundleQuery = hostPlayModeImpl;
  144. _playModeImpl = hostPlayModeImpl;
  145. _resourceMgr.Initialize(false, parameters.AutoDestroyAssetProvider, _bundleQuery);
  146. var initializeParameters = parameters as HostPlayModeParameters;
  147. initializeOperation = hostPlayModeImpl.InitializeAsync(assist,
  148. initializeParameters.BuildinQueryServices,
  149. initializeParameters.DeliveryQueryServices,
  150. initializeParameters.RemoteServices);
  151. }
  152. else if (_playMode == EPlayMode.WebPlayMode)
  153. {
  154. var webPlayModeImpl = new WebPlayModeImpl(PackageName);
  155. _bundleQuery = webPlayModeImpl;
  156. _playModeImpl = webPlayModeImpl;
  157. _resourceMgr.Initialize(false, parameters.AutoDestroyAssetProvider, _bundleQuery);
  158. var initializeParameters = parameters as WebPlayModeParameters;
  159. initializeOperation = webPlayModeImpl.InitializeAsync(assist,
  160. initializeParameters.BuildinQueryServices,
  161. initializeParameters.RemoteServices);
  162. }
  163. else
  164. {
  165. throw new NotImplementedException();
  166. }
  167. // 监听初始化结果
  168. _isInitialize = true;
  169. initializeOperation.Completed += InitializeOperation_Completed;
  170. return initializeOperation;
  171. }
  172. private void ResetInitializeAfterFailed()
  173. {
  174. if (_isInitialize && _initializeStatus == EOperationStatus.Failed)
  175. {
  176. _isInitialize = false;
  177. _initializeStatus = EOperationStatus.None;
  178. _initializeError = string.Empty;
  179. }
  180. }
  181. private void CheckInitializeParameters(InitializeParameters parameters)
  182. {
  183. if (_isInitialize)
  184. throw new Exception($"{nameof(ResourcePackage)} is initialized yet.");
  185. if (parameters == null)
  186. throw new Exception($"{nameof(ResourcePackage)} create parameters is null.");
  187. #if !UNITY_EDITOR
  188. if (parameters is EditorSimulateModeParameters)
  189. throw new Exception($"Editor simulate mode only support unity editor.");
  190. #endif
  191. if (parameters is EditorSimulateModeParameters)
  192. {
  193. var editorSimulateModeParameters = parameters as EditorSimulateModeParameters;
  194. if (string.IsNullOrEmpty(editorSimulateModeParameters.SimulateManifestFilePath))
  195. throw new Exception($"{nameof(editorSimulateModeParameters.SimulateManifestFilePath)} is null or empty.");
  196. }
  197. if (parameters is HostPlayModeParameters)
  198. {
  199. var hostPlayModeParameters = parameters as HostPlayModeParameters;
  200. if (hostPlayModeParameters.RemoteServices == null)
  201. throw new Exception($"{nameof(IRemoteServices)} is null.");
  202. if (hostPlayModeParameters.BuildinQueryServices == null)
  203. throw new Exception($"{nameof(IBuildinQueryServices)} is null.");
  204. if (hostPlayModeParameters.DeliveryQueryServices != null)
  205. {
  206. if (hostPlayModeParameters.DeliveryLoadServices == null)
  207. throw new Exception($"{nameof(IDeliveryLoadServices)} is null.");
  208. }
  209. }
  210. // 鉴定运行模式
  211. if (parameters is EditorSimulateModeParameters)
  212. _playMode = EPlayMode.EditorSimulateMode;
  213. else if (parameters is OfflinePlayModeParameters)
  214. _playMode = EPlayMode.OfflinePlayMode;
  215. else if (parameters is HostPlayModeParameters)
  216. _playMode = EPlayMode.HostPlayMode;
  217. else if (parameters is WebPlayModeParameters)
  218. _playMode = EPlayMode.WebPlayMode;
  219. else
  220. throw new NotImplementedException();
  221. // 检测运行时平台
  222. if (_playMode != EPlayMode.EditorSimulateMode)
  223. {
  224. #if UNITY_WEBGL
  225. if (_playMode != EPlayMode.WebPlayMode)
  226. {
  227. throw new Exception($"{_playMode} can not support WebGL plateform ! Please use {nameof(EPlayMode.WebPlayMode)}");
  228. }
  229. #else
  230. if (_playMode == EPlayMode.WebPlayMode)
  231. {
  232. throw new Exception($"{nameof(EPlayMode.WebPlayMode)} only support WebGL plateform !");
  233. }
  234. #endif
  235. }
  236. }
  237. private void InitializeOperation_Completed(AsyncOperationBase op)
  238. {
  239. _initializeStatus = op.Status;
  240. _initializeError = op.Error;
  241. }
  242. /// <summary>
  243. /// 向网络端请求最新的资源版本
  244. /// </summary>
  245. /// <param name="appendTimeTicks">在URL末尾添加时间戳</param>
  246. /// <param name="timeout">超时时间(默认值:60秒)</param>
  247. public UpdatePackageVersionOperation UpdatePackageVersionAsync(bool appendTimeTicks = true, int timeout = 60)
  248. {
  249. DebugCheckInitialize(false);
  250. return _playModeImpl.UpdatePackageVersionAsync(appendTimeTicks, timeout);
  251. }
  252. /// <summary>
  253. /// 向网络端请求并更新清单
  254. /// </summary>
  255. /// <param name="packageVersion">更新的包裹版本</param>
  256. /// <param name="autoSaveVersion">更新成功后自动保存版本号,作为下次初始化的版本。</param>
  257. /// <param name="timeout">超时时间(默认值:60秒)</param>
  258. public UpdatePackageManifestOperation UpdatePackageManifestAsync(string packageVersion, bool autoSaveVersion = true, int timeout = 60)
  259. {
  260. DebugCheckInitialize(false);
  261. // 注意:强烈建议在更新之前保持加载器为空!
  262. if (_resourceMgr.HasAnyLoader())
  263. {
  264. YooLogger.Warning($"Found loaded bundle before update manifest ! Recommended to call the {nameof(ForceUnloadAllAssets)} method to release loaded bundle !");
  265. }
  266. return _playModeImpl.UpdatePackageManifestAsync(packageVersion, autoSaveVersion, timeout);
  267. }
  268. /// <summary>
  269. /// 预下载指定版本的包裹资源
  270. /// </summary>
  271. /// <param name="packageVersion">下载的包裹版本</param>
  272. /// <param name="timeout">超时时间(默认值:60秒)</param>
  273. public PreDownloadContentOperation PreDownloadContentAsync(string packageVersion, int timeout = 60)
  274. {
  275. DebugCheckInitialize(false);
  276. return _playModeImpl.PreDownloadContentAsync(packageVersion, timeout);
  277. }
  278. /// <summary>
  279. /// 清理包裹未使用的缓存文件
  280. /// </summary>
  281. public ClearUnusedCacheFilesOperation ClearUnusedCacheFilesAsync()
  282. {
  283. DebugCheckInitialize();
  284. var operation = new ClearUnusedCacheFilesOperation(this, _cacheMgr);
  285. OperationSystem.StartOperation(PackageName, operation);
  286. return operation;
  287. }
  288. /// <summary>
  289. /// 清理包裹本地所有的缓存文件
  290. /// </summary>
  291. public ClearAllCacheFilesOperation ClearAllCacheFilesAsync()
  292. {
  293. DebugCheckInitialize();
  294. var operation = new ClearAllCacheFilesOperation(_cacheMgr);
  295. OperationSystem.StartOperation(PackageName, operation);
  296. return operation;
  297. }
  298. /// <summary>
  299. /// 获取本地包裹的版本信息
  300. /// </summary>
  301. public string GetPackageVersion()
  302. {
  303. DebugCheckInitialize();
  304. return _playModeImpl.ActiveManifest.PackageVersion;
  305. }
  306. #region 资源卸载
  307. /// <summary>
  308. /// 资源回收(卸载引用计数为零的资源)
  309. /// </summary>
  310. public void UnloadUnusedAssets()
  311. {
  312. DebugCheckInitialize();
  313. _resourceMgr.UnloadUnusedAssets();
  314. }
  315. /// <summary>
  316. /// 资源回收(尝试卸载指定的资源)
  317. /// </summary>
  318. public void TryUnloadUnusedAsset(string location)
  319. {
  320. DebugCheckInitialize();
  321. AssetInfo assetInfo = ConvertLocationToAssetInfo(location, null);
  322. _resourceMgr.TryUnloadUnusedAsset(assetInfo);
  323. }
  324. /// <summary>
  325. /// 资源回收(尝试卸载指定的资源)
  326. /// </summary>
  327. public void TryUnloadUnusedAsset(AssetInfo assetInfo)
  328. {
  329. DebugCheckInitialize();
  330. _resourceMgr.TryUnloadUnusedAsset(assetInfo);
  331. }
  332. /// <summary>
  333. /// 强制回收所有资源
  334. /// </summary>
  335. public void ForceUnloadAllAssets()
  336. {
  337. DebugCheckInitialize();
  338. _resourceMgr.ForceUnloadAllAssets();
  339. }
  340. #endregion
  341. #region 沙盒相关
  342. /// <summary>
  343. /// 获取包裹的内置文件根路径
  344. /// </summary>
  345. public string GetPackageBuildinRootDirectory()
  346. {
  347. DebugCheckInitialize();
  348. return _persistentMgr.BuildinRoot;
  349. }
  350. /// <summary>
  351. /// 获取包裹的沙盒文件根路径
  352. /// </summary>
  353. public string GetPackageSandboxRootDirectory()
  354. {
  355. DebugCheckInitialize();
  356. return _persistentMgr.SandboxRoot;
  357. }
  358. /// <summary>
  359. /// 清空包裹的沙盒目录
  360. /// </summary>
  361. public void ClearPackageSandbox()
  362. {
  363. DebugCheckInitialize();
  364. _persistentMgr.DeleteSandboxPackageFolder();
  365. _cacheMgr.ClearAll();
  366. }
  367. #endregion
  368. #region 资源信息
  369. /// <summary>
  370. /// 是否需要从远端更新下载
  371. /// </summary>
  372. /// <param name="location">资源的定位地址</param>
  373. public bool IsNeedDownloadFromRemote(string location)
  374. {
  375. DebugCheckInitialize();
  376. AssetInfo assetInfo = ConvertLocationToAssetInfo(location, null);
  377. return IsNeedDownloadFromRemoteInternal(assetInfo);
  378. }
  379. /// <summary>
  380. /// 是否需要从远端更新下载
  381. /// </summary>
  382. /// <param name="location">资源的定位地址</param>
  383. public bool IsNeedDownloadFromRemote(AssetInfo assetInfo)
  384. {
  385. DebugCheckInitialize();
  386. return IsNeedDownloadFromRemoteInternal(assetInfo);
  387. }
  388. /// <summary>
  389. /// 获取资源信息列表
  390. /// </summary>
  391. /// <param name="tag">资源标签</param>
  392. public AssetInfo[] GetAssetInfos(string tag)
  393. {
  394. DebugCheckInitialize();
  395. string[] tags = new string[] { tag };
  396. return _playModeImpl.ActiveManifest.GetAssetsInfoByTags(tags);
  397. }
  398. /// <summary>
  399. /// 获取资源信息列表
  400. /// </summary>
  401. /// <param name="tags">资源标签列表</param>
  402. public AssetInfo[] GetAssetInfos(string[] tags)
  403. {
  404. DebugCheckInitialize();
  405. return _playModeImpl.ActiveManifest.GetAssetsInfoByTags(tags);
  406. }
  407. /// <summary>
  408. /// 获取资源信息
  409. /// </summary>
  410. /// <param name="location">资源的定位地址</param>
  411. public AssetInfo GetAssetInfo(string location)
  412. {
  413. DebugCheckInitialize();
  414. return ConvertLocationToAssetInfo(location, null);
  415. }
  416. /// <summary>
  417. /// 获取资源信息
  418. /// </summary>
  419. /// <param name="location">资源的定位地址</param>
  420. /// <param name="type">资源类型</param>
  421. public AssetInfo GetAssetInfo(string location, System.Type type)
  422. {
  423. DebugCheckInitialize();
  424. return ConvertLocationToAssetInfo(location, type);
  425. }
  426. /// <summary>
  427. /// 获取资源信息
  428. /// </summary>
  429. /// <param name="assetGUID">资源GUID</param>
  430. public AssetInfo GetAssetInfoByGUID(string assetGUID)
  431. {
  432. DebugCheckInitialize();
  433. return ConvertAssetGUIDToAssetInfo(assetGUID, null);
  434. }
  435. /// <summary>
  436. /// 获取资源信息
  437. /// </summary>
  438. /// <param name="assetGUID">资源GUID</param>
  439. /// <param name="type">资源类型</param>
  440. public AssetInfo GetAssetInfoByGUID(string assetGUID, System.Type type)
  441. {
  442. DebugCheckInitialize();
  443. return ConvertAssetGUIDToAssetInfo(assetGUID, type);
  444. }
  445. /// <summary>
  446. /// 检查资源定位地址是否有效
  447. /// </summary>
  448. /// <param name="location">资源的定位地址</param>
  449. public bool CheckLocationValid(string location)
  450. {
  451. DebugCheckInitialize();
  452. string assetPath = _playModeImpl.ActiveManifest.TryMappingToAssetPath(location);
  453. return string.IsNullOrEmpty(assetPath) == false;
  454. }
  455. private bool IsNeedDownloadFromRemoteInternal(AssetInfo assetInfo)
  456. {
  457. if (assetInfo.IsInvalid)
  458. {
  459. YooLogger.Warning(assetInfo.Error);
  460. return false;
  461. }
  462. BundleInfo bundleInfo = _bundleQuery.GetMainBundleInfo(assetInfo);
  463. if (bundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromRemote)
  464. return true;
  465. BundleInfo[] depends = _bundleQuery.GetDependBundleInfos(assetInfo);
  466. foreach (var depend in depends)
  467. {
  468. if (depend.LoadMode == BundleInfo.ELoadMode.LoadFromRemote)
  469. return true;
  470. }
  471. return false;
  472. }
  473. #endregion
  474. #region 原生文件
  475. /// <summary>
  476. /// 同步加载原生文件
  477. /// </summary>
  478. /// <param name="assetInfo">资源信息</param>
  479. public RawFileHandle LoadRawFileSync(AssetInfo assetInfo)
  480. {
  481. DebugCheckInitialize();
  482. return LoadRawFileInternal(assetInfo, true, 0);
  483. }
  484. /// <summary>
  485. /// 同步加载原生文件
  486. /// </summary>
  487. /// <param name="location">资源的定位地址</param>
  488. public RawFileHandle LoadRawFileSync(string location)
  489. {
  490. DebugCheckInitialize();
  491. AssetInfo assetInfo = ConvertLocationToAssetInfo(location, null);
  492. return LoadRawFileInternal(assetInfo, true, 0);
  493. }
  494. /// <summary>
  495. /// 异步加载原生文件
  496. /// </summary>
  497. /// <param name="assetInfo">资源信息</param>
  498. /// <param name="priority">加载的优先级</param>
  499. public RawFileHandle LoadRawFileAsync(AssetInfo assetInfo, uint priority = 0)
  500. {
  501. DebugCheckInitialize();
  502. return LoadRawFileInternal(assetInfo, false, priority);
  503. }
  504. /// <summary>
  505. /// 异步加载原生文件
  506. /// </summary>
  507. /// <param name="location">资源的定位地址</param>
  508. /// <param name="priority">加载的优先级</param>
  509. public RawFileHandle LoadRawFileAsync(string location, uint priority = 0)
  510. {
  511. DebugCheckInitialize();
  512. AssetInfo assetInfo = ConvertLocationToAssetInfo(location, null);
  513. return LoadRawFileInternal(assetInfo, false, priority);
  514. }
  515. private RawFileHandle LoadRawFileInternal(AssetInfo assetInfo, bool waitForAsyncComplete, uint priority)
  516. {
  517. DebugCheckRawFileLoadMethod(nameof(LoadRawFileAsync));
  518. var handle = _resourceMgr.LoadRawFileAsync(assetInfo, priority);
  519. if (waitForAsyncComplete)
  520. handle.WaitForAsyncComplete();
  521. return handle;
  522. }
  523. #endregion
  524. #region 场景加载
  525. /// <summary>
  526. /// 异步加载场景
  527. /// </summary>
  528. /// <param name="location">场景的定位地址</param>
  529. /// <param name="sceneMode">场景加载模式</param>
  530. /// <param name="suspendLoad">场景加载到90%自动挂起</param>
  531. /// <param name="priority">加载的优先级</param>
  532. public SceneHandle LoadSceneAsync(string location, LoadSceneMode sceneMode = LoadSceneMode.Single, bool suspendLoad = false, uint priority = 0)
  533. {
  534. DebugCheckInitialize();
  535. AssetInfo assetInfo = ConvertLocationToAssetInfo(location, null);
  536. var handle = _resourceMgr.LoadSceneAsync(assetInfo, sceneMode, suspendLoad, priority);
  537. return handle;
  538. }
  539. /// <summary>
  540. /// 异步加载场景
  541. /// </summary>
  542. /// <param name="assetInfo">场景的资源信息</param>
  543. /// <param name="sceneMode">场景加载模式</param>
  544. /// <param name="suspendLoad">场景加载到90%自动挂起</param>
  545. /// <param name="priority">加载的优先级</param>
  546. public SceneHandle LoadSceneAsync(AssetInfo assetInfo, LoadSceneMode sceneMode = LoadSceneMode.Single, bool suspendLoad = false, uint priority = 0)
  547. {
  548. DebugCheckInitialize();
  549. var handle = _resourceMgr.LoadSceneAsync(assetInfo, sceneMode, suspendLoad, priority);
  550. return handle;
  551. }
  552. #endregion
  553. #region 资源加载
  554. /// <summary>
  555. /// 同步加载资源对象
  556. /// </summary>
  557. /// <param name="assetInfo">资源信息</param>
  558. public AssetHandle LoadAssetSync(AssetInfo assetInfo)
  559. {
  560. DebugCheckInitialize();
  561. return LoadAssetInternal(assetInfo, true, 0);
  562. }
  563. /// <summary>
  564. /// 同步加载资源对象
  565. /// </summary>
  566. /// <typeparam name="TObject">资源类型</typeparam>
  567. /// <param name="location">资源的定位地址</param>
  568. public AssetHandle LoadAssetSync<TObject>(string location) where TObject : UnityEngine.Object
  569. {
  570. DebugCheckInitialize();
  571. AssetInfo assetInfo = ConvertLocationToAssetInfo(location, typeof(TObject));
  572. return LoadAssetInternal(assetInfo, true, 0);
  573. }
  574. /// <summary>
  575. /// 同步加载资源对象
  576. /// </summary>
  577. /// <param name="location">资源的定位地址</param>
  578. /// <param name="type">资源类型</param>
  579. public AssetHandle LoadAssetSync(string location, System.Type type)
  580. {
  581. DebugCheckInitialize();
  582. AssetInfo assetInfo = ConvertLocationToAssetInfo(location, type);
  583. return LoadAssetInternal(assetInfo, true, 0);
  584. }
  585. /// <summary>
  586. /// 同步加载资源对象
  587. /// </summary>
  588. /// <param name="location">资源的定位地址</param>
  589. public AssetHandle LoadAssetSync(string location)
  590. {
  591. DebugCheckInitialize();
  592. Type type = typeof(UnityEngine.Object);
  593. AssetInfo assetInfo = ConvertLocationToAssetInfo(location, type);
  594. return LoadAssetInternal(assetInfo, true, 0);
  595. }
  596. /// <summary>
  597. /// 异步加载资源对象
  598. /// </summary>
  599. /// <param name="assetInfo">资源信息</param>
  600. /// <param name="priority">加载的优先级</param>
  601. public AssetHandle LoadAssetAsync(AssetInfo assetInfo, uint priority = 0)
  602. {
  603. DebugCheckInitialize();
  604. return LoadAssetInternal(assetInfo, false, priority);
  605. }
  606. /// <summary>
  607. /// 异步加载资源对象
  608. /// </summary>
  609. /// <typeparam name="TObject">资源类型</typeparam>
  610. /// <param name="location">资源的定位地址</param>
  611. /// <param name="priority">加载的优先级</param>
  612. public AssetHandle LoadAssetAsync<TObject>(string location, uint priority = 0) where TObject : UnityEngine.Object
  613. {
  614. DebugCheckInitialize();
  615. AssetInfo assetInfo = ConvertLocationToAssetInfo(location, typeof(TObject));
  616. return LoadAssetInternal(assetInfo, false, priority);
  617. }
  618. /// <summary>
  619. /// 异步加载资源对象
  620. /// </summary>
  621. /// <param name="location">资源的定位地址</param>
  622. /// <param name="type">资源类型</param>
  623. /// <param name="priority">加载的优先级</param>
  624. public AssetHandle LoadAssetAsync(string location, System.Type type, uint priority = 0)
  625. {
  626. DebugCheckInitialize();
  627. AssetInfo assetInfo = ConvertLocationToAssetInfo(location, type);
  628. return LoadAssetInternal(assetInfo, false, priority);
  629. }
  630. /// <summary>
  631. /// 异步加载资源对象
  632. /// </summary>
  633. /// <param name="location">资源的定位地址</param>
  634. /// <param name="priority">加载的优先级</param>
  635. public AssetHandle LoadAssetAsync(string location, uint priority = 0)
  636. {
  637. DebugCheckInitialize();
  638. Type type = typeof(UnityEngine.Object);
  639. AssetInfo assetInfo = ConvertLocationToAssetInfo(location, type);
  640. return LoadAssetInternal(assetInfo, false, priority);
  641. }
  642. private AssetHandle LoadAssetInternal(AssetInfo assetInfo, bool waitForAsyncComplete, uint priority)
  643. {
  644. DebugCheckAssetLoadMethod(nameof(LoadAssetAsync));
  645. DebugCheckAssetLoadType(assetInfo.AssetType);
  646. var handle = _resourceMgr.LoadAssetAsync(assetInfo, priority);
  647. if (waitForAsyncComplete)
  648. handle.WaitForAsyncComplete();
  649. return handle;
  650. }
  651. #endregion
  652. #region 资源加载
  653. /// <summary>
  654. /// 同步加载子资源对象
  655. /// </summary>
  656. /// <param name="assetInfo">资源信息</param>
  657. public SubAssetsHandle LoadSubAssetsSync(AssetInfo assetInfo)
  658. {
  659. DebugCheckInitialize();
  660. return LoadSubAssetsInternal(assetInfo, true, 0);
  661. }
  662. /// <summary>
  663. /// 同步加载子资源对象
  664. /// </summary>
  665. /// <typeparam name="TObject">资源类型</typeparam>
  666. /// <param name="location">资源的定位地址</param>
  667. public SubAssetsHandle LoadSubAssetsSync<TObject>(string location) where TObject : UnityEngine.Object
  668. {
  669. DebugCheckInitialize();
  670. AssetInfo assetInfo = ConvertLocationToAssetInfo(location, typeof(TObject));
  671. return LoadSubAssetsInternal(assetInfo, true, 0);
  672. }
  673. /// <summary>
  674. /// 同步加载子资源对象
  675. /// </summary>
  676. /// <param name="location">资源的定位地址</param>
  677. /// <param name="type">子对象类型</param>
  678. public SubAssetsHandle LoadSubAssetsSync(string location, System.Type type)
  679. {
  680. DebugCheckInitialize();
  681. AssetInfo assetInfo = ConvertLocationToAssetInfo(location, type);
  682. return LoadSubAssetsInternal(assetInfo, true, 0);
  683. }
  684. /// <summary>
  685. /// 同步加载子资源对象
  686. /// </summary>
  687. /// <param name="location">资源的定位地址</param>
  688. public SubAssetsHandle LoadSubAssetsSync(string location)
  689. {
  690. DebugCheckInitialize();
  691. Type type = typeof(UnityEngine.Object);
  692. AssetInfo assetInfo = ConvertLocationToAssetInfo(location, type);
  693. return LoadSubAssetsInternal(assetInfo, true, 0);
  694. }
  695. /// <summary>
  696. /// 异步加载子资源对象
  697. /// </summary>
  698. /// <param name="assetInfo">资源信息</param>
  699. /// <param name="priority">加载的优先级</param>
  700. public SubAssetsHandle LoadSubAssetsAsync(AssetInfo assetInfo, uint priority = 0)
  701. {
  702. DebugCheckInitialize();
  703. return LoadSubAssetsInternal(assetInfo, false, priority);
  704. }
  705. /// <summary>
  706. /// 异步加载子资源对象
  707. /// </summary>
  708. /// <typeparam name="TObject">资源类型</typeparam>
  709. /// <param name="location">资源的定位地址</param>
  710. /// <param name="priority">加载的优先级</param>
  711. public SubAssetsHandle LoadSubAssetsAsync<TObject>(string location, uint priority = 0) where TObject : UnityEngine.Object
  712. {
  713. DebugCheckInitialize();
  714. AssetInfo assetInfo = ConvertLocationToAssetInfo(location, typeof(TObject));
  715. return LoadSubAssetsInternal(assetInfo, false, priority);
  716. }
  717. /// <summary>
  718. /// 异步加载子资源对象
  719. /// </summary>
  720. /// <param name="location">资源的定位地址</param>
  721. /// <param name="type">子对象类型</param>
  722. /// <param name="priority">加载的优先级</param>
  723. public SubAssetsHandle LoadSubAssetsAsync(string location, System.Type type, uint priority = 0)
  724. {
  725. DebugCheckInitialize();
  726. AssetInfo assetInfo = ConvertLocationToAssetInfo(location, type);
  727. return LoadSubAssetsInternal(assetInfo, false, priority);
  728. }
  729. /// <summary>
  730. /// 异步加载子资源对象
  731. /// </summary>
  732. /// <param name="location">资源的定位地址</param>
  733. /// <param name="priority">加载的优先级</param>
  734. public SubAssetsHandle LoadSubAssetsAsync(string location, uint priority = 0)
  735. {
  736. DebugCheckInitialize();
  737. Type type = typeof(UnityEngine.Object);
  738. AssetInfo assetInfo = ConvertLocationToAssetInfo(location, type);
  739. return LoadSubAssetsInternal(assetInfo, false, priority);
  740. }
  741. private SubAssetsHandle LoadSubAssetsInternal(AssetInfo assetInfo, bool waitForAsyncComplete, uint priority)
  742. {
  743. DebugCheckAssetLoadMethod(nameof(LoadSubAssetsAsync));
  744. DebugCheckAssetLoadType(assetInfo.AssetType);
  745. var handle = _resourceMgr.LoadSubAssetsAsync(assetInfo, priority);
  746. if (waitForAsyncComplete)
  747. handle.WaitForAsyncComplete();
  748. return handle;
  749. }
  750. #endregion
  751. #region 资源加载
  752. /// <summary>
  753. /// 同步加载资源包内所有资源对象
  754. /// </summary>
  755. /// <param name="assetInfo">资源信息</param>
  756. public AllAssetsHandle LoadAllAssetsSync(AssetInfo assetInfo)
  757. {
  758. DebugCheckInitialize();
  759. return LoadAllAssetsInternal(assetInfo, true, 0);
  760. }
  761. /// <summary>
  762. /// 同步加载资源包内所有资源对象
  763. /// </summary>
  764. /// <typeparam name="TObject">资源类型</typeparam>
  765. /// <param name="location">资源的定位地址</param>
  766. public AllAssetsHandle LoadAllAssetsSync<TObject>(string location) where TObject : UnityEngine.Object
  767. {
  768. DebugCheckInitialize();
  769. AssetInfo assetInfo = ConvertLocationToAssetInfo(location, typeof(TObject));
  770. return LoadAllAssetsInternal(assetInfo, true, 0);
  771. }
  772. /// <summary>
  773. /// 同步加载资源包内所有资源对象
  774. /// </summary>
  775. /// <param name="location">资源的定位地址</param>
  776. /// <param name="type">子对象类型</param>
  777. public AllAssetsHandle LoadAllAssetsSync(string location, System.Type type)
  778. {
  779. DebugCheckInitialize();
  780. AssetInfo assetInfo = ConvertLocationToAssetInfo(location, type);
  781. return LoadAllAssetsInternal(assetInfo, true, 0);
  782. }
  783. /// <summary>
  784. /// 同步加载资源包内所有资源对象
  785. /// </summary>
  786. /// <param name="location">资源的定位地址</param>
  787. public AllAssetsHandle LoadAllAssetsSync(string location)
  788. {
  789. DebugCheckInitialize();
  790. Type type = typeof(UnityEngine.Object);
  791. AssetInfo assetInfo = ConvertLocationToAssetInfo(location, type);
  792. return LoadAllAssetsInternal(assetInfo, true, 0);
  793. }
  794. /// <summary>
  795. /// 异步加载资源包内所有资源对象
  796. /// </summary>
  797. /// <param name="assetInfo">资源信息</param>
  798. /// <param name="priority">加载的优先级</param>
  799. public AllAssetsHandle LoadAllAssetsAsync(AssetInfo assetInfo, uint priority = 0)
  800. {
  801. DebugCheckInitialize();
  802. return LoadAllAssetsInternal(assetInfo, false, priority);
  803. }
  804. /// <summary>
  805. /// 异步加载资源包内所有资源对象
  806. /// </summary>
  807. /// <typeparam name="TObject">资源类型</typeparam>
  808. /// <param name="location">资源的定位地址</param>
  809. /// <param name="priority">加载的优先级</param>
  810. public AllAssetsHandle LoadAllAssetsAsync<TObject>(string location, uint priority = 0) where TObject : UnityEngine.Object
  811. {
  812. DebugCheckInitialize();
  813. AssetInfo assetInfo = ConvertLocationToAssetInfo(location, typeof(TObject));
  814. return LoadAllAssetsInternal(assetInfo, false, priority);
  815. }
  816. /// <summary>
  817. /// 异步加载资源包内所有资源对象
  818. /// </summary>
  819. /// <param name="location">资源的定位地址</param>
  820. /// <param name="type">子对象类型</param>
  821. /// <param name="priority">加载的优先级</param>
  822. public AllAssetsHandle LoadAllAssetsAsync(string location, System.Type type, uint priority = 0)
  823. {
  824. DebugCheckInitialize();
  825. AssetInfo assetInfo = ConvertLocationToAssetInfo(location, type);
  826. return LoadAllAssetsInternal(assetInfo, false, priority);
  827. }
  828. /// <summary>
  829. /// 异步加载资源包内所有资源对象
  830. /// </summary>
  831. /// <param name="location">资源的定位地址</param>
  832. /// <param name="priority">加载的优先级</param>
  833. public AllAssetsHandle LoadAllAssetsAsync(string location, uint priority = 0)
  834. {
  835. DebugCheckInitialize();
  836. Type type = typeof(UnityEngine.Object);
  837. AssetInfo assetInfo = ConvertLocationToAssetInfo(location, type);
  838. return LoadAllAssetsInternal(assetInfo, false, priority);
  839. }
  840. private AllAssetsHandle LoadAllAssetsInternal(AssetInfo assetInfo, bool waitForAsyncComplete, uint priority)
  841. {
  842. DebugCheckAssetLoadMethod(nameof(LoadAllAssetsAsync));
  843. DebugCheckAssetLoadType(assetInfo.AssetType);
  844. var handle = _resourceMgr.LoadAllAssetsAsync(assetInfo, priority);
  845. if (waitForAsyncComplete)
  846. handle.WaitForAsyncComplete();
  847. return handle;
  848. }
  849. #endregion
  850. #region 资源下载
  851. /// <summary>
  852. /// 创建资源下载器,用于下载当前资源版本所有的资源包文件
  853. /// </summary>
  854. /// <param name="downloadingMaxNumber">同时下载的最大文件数</param>
  855. /// <param name="failedTryAgain">下载失败的重试次数</param>
  856. /// <param name="timeout">超时时间</param>
  857. public ResourceDownloaderOperation CreateResourceDownloader(int downloadingMaxNumber, int failedTryAgain, int timeout = 60)
  858. {
  859. DebugCheckInitialize();
  860. return _playModeImpl.CreateResourceDownloaderByAll(downloadingMaxNumber, failedTryAgain, timeout);
  861. }
  862. /// <summary>
  863. /// 创建资源下载器,用于下载指定的资源标签关联的资源包文件
  864. /// </summary>
  865. /// <param name="tag">资源标签</param>
  866. /// <param name="downloadingMaxNumber">同时下载的最大文件数</param>
  867. /// <param name="failedTryAgain">下载失败的重试次数</param>
  868. /// <param name="timeout">超时时间</param>
  869. public ResourceDownloaderOperation CreateResourceDownloader(string tag, int downloadingMaxNumber, int failedTryAgain, int timeout = 60)
  870. {
  871. DebugCheckInitialize();
  872. return _playModeImpl.CreateResourceDownloaderByTags(new string[] { tag }, downloadingMaxNumber, failedTryAgain, timeout);
  873. }
  874. /// <summary>
  875. /// 创建资源下载器,用于下载指定的资源标签列表关联的资源包文件
  876. /// </summary>
  877. /// <param name="tags">资源标签列表</param>
  878. /// <param name="downloadingMaxNumber">同时下载的最大文件数</param>
  879. /// <param name="failedTryAgain">下载失败的重试次数</param>
  880. /// <param name="timeout">超时时间</param>
  881. public ResourceDownloaderOperation CreateResourceDownloader(string[] tags, int downloadingMaxNumber, int failedTryAgain, int timeout = 60)
  882. {
  883. DebugCheckInitialize();
  884. return _playModeImpl.CreateResourceDownloaderByTags(tags, downloadingMaxNumber, failedTryAgain, timeout);
  885. }
  886. /// <summary>
  887. /// 创建资源下载器,用于下载指定的资源依赖的资源包文件
  888. /// </summary>
  889. /// <param name="location">资源的定位地址</param>
  890. /// <param name="downloadingMaxNumber">同时下载的最大文件数</param>
  891. /// <param name="failedTryAgain">下载失败的重试次数</param>
  892. /// <param name="timeout">超时时间</param>
  893. public ResourceDownloaderOperation CreateBundleDownloader(string location, int downloadingMaxNumber, int failedTryAgain, int timeout = 60)
  894. {
  895. DebugCheckInitialize();
  896. var assetInfo = ConvertLocationToAssetInfo(location, null);
  897. AssetInfo[] assetInfos = new AssetInfo[] { assetInfo };
  898. return _playModeImpl.CreateResourceDownloaderByPaths(assetInfos, downloadingMaxNumber, failedTryAgain, timeout);
  899. }
  900. /// <summary>
  901. /// 创建资源下载器,用于下载指定的资源列表依赖的资源包文件
  902. /// </summary>
  903. /// <param name="locations">资源的定位地址列表</param>
  904. /// <param name="downloadingMaxNumber">同时下载的最大文件数</param>
  905. /// <param name="failedTryAgain">下载失败的重试次数</param>
  906. /// <param name="timeout">超时时间</param>
  907. public ResourceDownloaderOperation CreateBundleDownloader(string[] locations, int downloadingMaxNumber, int failedTryAgain, int timeout = 60)
  908. {
  909. DebugCheckInitialize();
  910. List<AssetInfo> assetInfos = new List<AssetInfo>(locations.Length);
  911. foreach (var location in locations)
  912. {
  913. var assetInfo = ConvertLocationToAssetInfo(location, null);
  914. assetInfos.Add(assetInfo);
  915. }
  916. return _playModeImpl.CreateResourceDownloaderByPaths(assetInfos.ToArray(), downloadingMaxNumber, failedTryAgain, timeout);
  917. }
  918. /// <summary>
  919. /// 创建资源下载器,用于下载指定的资源依赖的资源包文件
  920. /// </summary>
  921. /// <param name="assetInfo">资源信息</param>
  922. /// <param name="downloadingMaxNumber">同时下载的最大文件数</param>
  923. /// <param name="failedTryAgain">下载失败的重试次数</param>
  924. /// <param name="timeout">超时时间</param>
  925. public ResourceDownloaderOperation CreateBundleDownloader(AssetInfo assetInfo, int downloadingMaxNumber, int failedTryAgain, int timeout = 60)
  926. {
  927. DebugCheckInitialize();
  928. AssetInfo[] assetInfos = new AssetInfo[] { assetInfo };
  929. return _playModeImpl.CreateResourceDownloaderByPaths(assetInfos, downloadingMaxNumber, failedTryAgain, timeout);
  930. }
  931. /// <summary>
  932. /// 创建资源下载器,用于下载指定的资源列表依赖的资源包文件
  933. /// </summary>
  934. /// <param name="assetInfos">资源信息列表</param>
  935. /// <param name="downloadingMaxNumber">同时下载的最大文件数</param>
  936. /// <param name="failedTryAgain">下载失败的重试次数</param>
  937. /// <param name="timeout">超时时间</param>
  938. public ResourceDownloaderOperation CreateBundleDownloader(AssetInfo[] assetInfos, int downloadingMaxNumber, int failedTryAgain, int timeout = 60)
  939. {
  940. DebugCheckInitialize();
  941. return _playModeImpl.CreateResourceDownloaderByPaths(assetInfos, downloadingMaxNumber, failedTryAgain, timeout);
  942. }
  943. #endregion
  944. #region 资源解压
  945. /// <summary>
  946. /// 创建内置资源解压器,用于解压当前资源版本所有的资源包文件
  947. /// </summary>
  948. /// <param name="unpackingMaxNumber">同时解压的最大文件数</param>
  949. /// <param name="failedTryAgain">解压失败的重试次数</param>
  950. public ResourceUnpackerOperation CreateResourceUnpacker(int unpackingMaxNumber, int failedTryAgain)
  951. {
  952. DebugCheckInitialize();
  953. return _playModeImpl.CreateResourceUnpackerByAll(unpackingMaxNumber, failedTryAgain, int.MaxValue);
  954. }
  955. /// <summary>
  956. /// 创建内置资源解压器,用于解压指定的资源标签关联的资源包文件
  957. /// </summary>
  958. /// <param name="tag">资源标签</param>
  959. /// <param name="unpackingMaxNumber">同时解压的最大文件数</param>
  960. /// <param name="failedTryAgain">解压失败的重试次数</param>
  961. public ResourceUnpackerOperation CreateResourceUnpacker(string tag, int unpackingMaxNumber, int failedTryAgain)
  962. {
  963. DebugCheckInitialize();
  964. return _playModeImpl.CreateResourceUnpackerByTags(new string[] { tag }, unpackingMaxNumber, failedTryAgain, int.MaxValue);
  965. }
  966. /// <summary>
  967. /// 创建内置资源解压器,用于解压指定的资源标签列表关联的资源包文件
  968. /// </summary>
  969. /// <param name="tags">资源标签列表</param>
  970. /// <param name="unpackingMaxNumber">同时解压的最大文件数</param>
  971. /// <param name="failedTryAgain">解压失败的重试次数</param>
  972. public ResourceUnpackerOperation CreateResourceUnpacker(string[] tags, int unpackingMaxNumber, int failedTryAgain)
  973. {
  974. DebugCheckInitialize();
  975. return _playModeImpl.CreateResourceUnpackerByTags(tags, unpackingMaxNumber, failedTryAgain, int.MaxValue);
  976. }
  977. #endregion
  978. #region 资源导入
  979. /// <summary>
  980. /// 创建资源导入器
  981. /// 注意:资源文件名称必须和资源服务器部署的文件名称一致!
  982. /// </summary>
  983. /// <param name="filePaths">资源路径列表</param>
  984. /// <param name="importerMaxNumber">同时导入的最大文件数</param>
  985. /// <param name="failedTryAgain">导入失败的重试次数</param>
  986. public ResourceImporterOperation CreateResourceImporter(string[] filePaths, int importerMaxNumber, int failedTryAgain)
  987. {
  988. DebugCheckInitialize();
  989. return _playModeImpl.CreateResourceImporterByFilePaths(filePaths, importerMaxNumber, failedTryAgain, int.MaxValue);
  990. }
  991. #endregion
  992. #region 内部方法
  993. /// <summary>
  994. /// 是否包含资源文件
  995. /// </summary>
  996. internal bool IsIncludeBundleFile(string cacheGUID)
  997. {
  998. // NOTE : 编辑器模拟模式下始终返回TRUE
  999. if (_playMode == EPlayMode.EditorSimulateMode)
  1000. return true;
  1001. return _playModeImpl.ActiveManifest.IsIncludeBundleFile(cacheGUID);
  1002. }
  1003. private AssetInfo ConvertLocationToAssetInfo(string location, System.Type assetType)
  1004. {
  1005. return _playModeImpl.ActiveManifest.ConvertLocationToAssetInfo(location, assetType);
  1006. }
  1007. private AssetInfo ConvertAssetGUIDToAssetInfo(string assetGUID, System.Type assetType)
  1008. {
  1009. return _playModeImpl.ActiveManifest.ConvertAssetGUIDToAssetInfo(assetGUID, assetType);
  1010. }
  1011. #endregion
  1012. #region 调试方法
  1013. [Conditional("DEBUG")]
  1014. private void DebugCheckInitialize(bool checkActiveManifest = true)
  1015. {
  1016. if (_initializeStatus == EOperationStatus.None)
  1017. throw new Exception("Package initialize not completed !");
  1018. else if (_initializeStatus == EOperationStatus.Failed)
  1019. throw new Exception($"Package initialize failed ! {_initializeError}");
  1020. if (checkActiveManifest)
  1021. {
  1022. if (_playModeImpl.ActiveManifest == null)
  1023. throw new Exception("Not found active package manifest !");
  1024. }
  1025. }
  1026. [Conditional("DEBUG")]
  1027. private void DebugCheckRawFileLoadMethod(string method)
  1028. {
  1029. if (_playModeImpl.ActiveManifest.BuildPipeline != EDefaultBuildPipeline.RawFileBuildPipeline.ToString())
  1030. {
  1031. throw new Exception($"Cannot load asset bundle file using {method} method !");
  1032. }
  1033. }
  1034. [Conditional("DEBUG")]
  1035. private void DebugCheckAssetLoadMethod(string method)
  1036. {
  1037. if (_playModeImpl.ActiveManifest.BuildPipeline == EDefaultBuildPipeline.RawFileBuildPipeline.ToString())
  1038. {
  1039. throw new Exception($"Cannot load raw file using {method} method !");
  1040. }
  1041. }
  1042. [Conditional("DEBUG")]
  1043. private void DebugCheckAssetLoadType(System.Type type)
  1044. {
  1045. if (type == null)
  1046. return;
  1047. if (typeof(UnityEngine.Behaviour).IsAssignableFrom(type))
  1048. {
  1049. throw new Exception($"Load asset type is invalid : {type.FullName} !");
  1050. }
  1051. if (typeof(UnityEngine.Object).IsAssignableFrom(type) == false)
  1052. {
  1053. throw new Exception($"Load asset type is invalid : {type.FullName} !");
  1054. }
  1055. }
  1056. #endregion
  1057. #region 调试信息
  1058. internal DebugPackageData GetDebugPackageData()
  1059. {
  1060. DebugPackageData data = new DebugPackageData();
  1061. data.PackageName = PackageName;
  1062. data.ProviderInfos = _resourceMgr.GetDebugReportInfos();
  1063. return data;
  1064. }
  1065. #endregion
  1066. }
  1067. }