AssetsLoaderAsync.cs 1.3 KB

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