BundleAssetRequestAsync.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using UnityEngine;
  5. public class BundleAssetRequestAsync : BundleAssetRequest
  6. {
  7. private AssetBundleRequest _request;
  8. public BundleAssetRequestAsync(string bundle)
  9. : base(bundle)
  10. {
  11. }
  12. public override float progress
  13. {
  14. get
  15. {
  16. if (isDone) return 1;
  17. if (LoadState == AssetLoadState.Init) return 0;
  18. if (_request != null) return _request.progress * 0.7f + 0.3f;
  19. if (BundleRequest == null) return 1;
  20. var value = BundleRequest.progress;
  21. var max = children.Count;
  22. if (max <= 0)
  23. return value * 0.3f;
  24. for (var i = 0; i < max; i++)
  25. {
  26. var item = children[i];
  27. value += item.progress;
  28. }
  29. return value / (max + 1) * 0.3f;
  30. }
  31. }
  32. private bool OnError(BundleRequest bundleRequest)
  33. {
  34. error = bundleRequest.error;
  35. if (!string.IsNullOrEmpty(error))
  36. {
  37. LoadState = AssetLoadState.Loaded;
  38. return true;
  39. }
  40. return false;
  41. }
  42. internal override bool Update()
  43. {
  44. if (!base.Update()) return false;
  45. if (LoadState == AssetLoadState.Init) return true;
  46. if (_request == null)
  47. {
  48. if (!BundleRequest.isDone) return true;
  49. if (OnError(BundleRequest)) return false;
  50. for (var i = 0; i < children.Count; i++)
  51. {
  52. var item = children[i];
  53. if (!item.isDone) return true;
  54. if (OnError(item)) return false;
  55. }
  56. var assetName = Path.GetFileName(name);
  57. _request = BundleRequest.assetBundle.LoadAssetAsync(assetName, assetType);
  58. if (_request == null)
  59. {
  60. error = "request == null";
  61. LoadState = AssetLoadState.Loaded;
  62. return false;
  63. }
  64. return true;
  65. }
  66. if (_request.isDone)
  67. {
  68. asset = _request.asset;
  69. LoadState = AssetLoadState.Loaded;
  70. if (asset == null) error = "asset == null";
  71. return false;
  72. }
  73. return true;
  74. }
  75. internal override void Load()
  76. {
  77. BundleRequest = LoadModule.LoadBundleAsync(assetBundleName);
  78. var bundles = LoadModule.GetAllDependencies(assetBundleName);
  79. foreach (var item in bundles) children.Add(LoadModule.LoadBundleAsync(item));
  80. LoadState = AssetLoadState.LoadAssetBundle;
  81. }
  82. internal override void Unload()
  83. {
  84. _request = null;
  85. LoadState = AssetLoadState.Unload;
  86. base.Unload();
  87. }
  88. internal override void LoadImmediate()
  89. {
  90. BundleRequest.LoadImmediate();
  91. foreach (var item in children) item.LoadImmediate();
  92. if (BundleRequest.assetBundle != null)
  93. {
  94. var assetName = Path.GetFileName(name);
  95. asset = BundleRequest.assetBundle.LoadAsset(assetName, assetType);
  96. }
  97. LoadState = AssetLoadState.Loaded;
  98. if (asset == null) error = "asset == null";
  99. }
  100. }