BundledAssetProvider.cs 4.4 KB

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