AssetsLoaderAsync.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using System.IO;
  2. using System.Threading.Tasks;
  3. using UnityEngine;
  4. namespace Model
  5. {
  6. [ObjectEvent]
  7. public class AssetsLoaderAsyncEvent : ObjectEvent<AssetsLoaderAsync>, IUpdate, IAwake<AssetBundle>
  8. {
  9. public void Awake(AssetBundle assetBundle)
  10. {
  11. this.Get().Awake(assetBundle);
  12. }
  13. public void Update()
  14. {
  15. this.Get().Update();
  16. }
  17. }
  18. public class AssetsLoaderAsync : Disposer, IUpdate
  19. {
  20. private AssetBundle assetBundle;
  21. private AssetBundleRequest request;
  22. private TaskCompletionSource<bool> tcs;
  23. public void Awake(AssetBundle ab)
  24. {
  25. this.assetBundle = ab;
  26. }
  27. public void Update()
  28. {
  29. if (!this.request.isDone)
  30. {
  31. return;
  32. }
  33. TaskCompletionSource<bool> t = tcs;
  34. t.SetResult(true);
  35. }
  36. public override void Dispose()
  37. {
  38. if (this.Id == 0)
  39. {
  40. return;
  41. }
  42. base.Dispose();
  43. this.assetBundle = null;
  44. this.request = null;
  45. }
  46. public async Task<UnityEngine.Object[]> LoadAllAssetsAsync()
  47. {
  48. await InnerLoadAllAssetsAsync();
  49. return this.request.allAssets;
  50. }
  51. private Task<bool> InnerLoadAllAssetsAsync()
  52. {
  53. this.tcs = new TaskCompletionSource<bool>();
  54. this.request = assetBundle.LoadAllAssetsAsync();
  55. return this.tcs.Task;
  56. }
  57. }
  58. }