AssetsLoaderAsync.cs 1.3 KB

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