AssetBundleLoaderAsync.cs 920 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. using System.Threading.Tasks;
  2. using UnityEngine;
  3. namespace Model
  4. {
  5. public class AssetBundleLoaderAsync : Entity
  6. {
  7. private AssetBundle assetBundle;
  8. private AssetBundleRequest request;
  9. private TaskCompletionSource<bool> tcs;
  10. public AssetBundleLoaderAsync(AssetBundle assetBundle)
  11. {
  12. this.assetBundle = assetBundle;
  13. }
  14. public void Update()
  15. {
  16. if (!this.request.isDone)
  17. {
  18. return;
  19. }
  20. TaskCompletionSource<bool> t = this.tcs;
  21. t.SetResult(true);
  22. }
  23. public override void Dispose()
  24. {
  25. if (this.Id == 0)
  26. {
  27. return;
  28. }
  29. base.Dispose();
  30. }
  31. public async Task<UnityEngine.Object[]> LoadAllAssetsAsync()
  32. {
  33. await InnerLoadAllAssetsAsync();
  34. return this.request.allAssets;
  35. }
  36. private Task<bool> InnerLoadAllAssetsAsync()
  37. {
  38. this.tcs = new TaskCompletionSource<bool>();
  39. this.request = assetBundle.LoadAllAssetsAsync();
  40. return this.tcs.Task;
  41. }
  42. }
  43. }