BundledSubAssetsProvider.cs 3.0 KB

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