BundledSubAssetsProvider.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace YooAsset
  5. {
  6. internal sealed class BundledSubAssetsProvider : ProviderBase
  7. {
  8. private AssetBundleRequest _cacheRequest;
  9. public BundledSubAssetsProvider(AssetSystemImpl impl, string providerGUID, AssetInfo assetInfo) : base(impl, providerGUID, assetInfo)
  10. {
  11. }
  12. public override void Update()
  13. {
  14. DebugBeginRecording();
  15. if (IsDone)
  16. return;
  17. if (Status == EStatus.None)
  18. {
  19. Status = EStatus.CheckBundle;
  20. }
  21. // 1. 检测资源包
  22. if (Status == EStatus.CheckBundle)
  23. {
  24. if (IsWaitForAsyncComplete)
  25. {
  26. DependBundles.WaitForAsyncComplete();
  27. OwnerBundle.WaitForAsyncComplete();
  28. }
  29. if (DependBundles.IsDone() == false)
  30. return;
  31. if (OwnerBundle.IsDone() == false)
  32. return;
  33. if (DependBundles.IsSucceed() == false)
  34. {
  35. Status = EStatus.Failed;
  36. LastError = DependBundles.GetLastError();
  37. InvokeCompletion();
  38. return;
  39. }
  40. if (OwnerBundle.Status != BundleLoaderBase.EStatus.Succeed)
  41. {
  42. Status = EStatus.Failed;
  43. LastError = OwnerBundle.LastError;
  44. InvokeCompletion();
  45. return;
  46. }
  47. if (OwnerBundle.CacheBundle == null)
  48. {
  49. ProcessCacheBundleException();
  50. return;
  51. }
  52. Status = EStatus.Loading;
  53. }
  54. // 2. 加载资源对象
  55. if (Status == EStatus.Loading)
  56. {
  57. if (IsWaitForAsyncComplete)
  58. {
  59. if (MainAssetInfo.AssetType == null)
  60. AllAssetObjects = OwnerBundle.CacheBundle.LoadAssetWithSubAssets(MainAssetInfo.AssetPath);
  61. else
  62. AllAssetObjects = OwnerBundle.CacheBundle.LoadAssetWithSubAssets(MainAssetInfo.AssetPath, MainAssetInfo.AssetType);
  63. }
  64. else
  65. {
  66. if (MainAssetInfo.AssetType == null)
  67. _cacheRequest = OwnerBundle.CacheBundle.LoadAssetWithSubAssetsAsync(MainAssetInfo.AssetPath);
  68. else
  69. _cacheRequest = OwnerBundle.CacheBundle.LoadAssetWithSubAssetsAsync(MainAssetInfo.AssetPath, MainAssetInfo.AssetType);
  70. }
  71. Status = EStatus.Checking;
  72. }
  73. // 3. 检测加载结果
  74. if (Status == EStatus.Checking)
  75. {
  76. if (_cacheRequest != null)
  77. {
  78. if (IsWaitForAsyncComplete)
  79. {
  80. // 强制挂起主线程(注意:该操作会很耗时)
  81. YooLogger.Warning("Suspend the main thread to load unity asset.");
  82. AllAssetObjects = _cacheRequest.allAssets;
  83. }
  84. else
  85. {
  86. Progress = _cacheRequest.progress;
  87. if (_cacheRequest.isDone == false)
  88. return;
  89. AllAssetObjects = _cacheRequest.allAssets;
  90. }
  91. }
  92. Status = AllAssetObjects == null ? EStatus.Failed : EStatus.Succeed;
  93. if (Status == EStatus.Failed)
  94. {
  95. if (MainAssetInfo.AssetType == null)
  96. LastError = $"Failed to load sub assets : {MainAssetInfo.AssetPath} AssetType : null AssetBundle : {OwnerBundle.MainBundleInfo.Bundle.BundleName}";
  97. else
  98. LastError = $"Failed to load sub assets : {MainAssetInfo.AssetPath} AssetType : {MainAssetInfo.AssetType} AssetBundle : {OwnerBundle.MainBundleInfo.Bundle.BundleName}";
  99. YooLogger.Error(LastError);
  100. }
  101. InvokeCompletion();
  102. }
  103. }
  104. }
  105. }