UnityAsyncExtensions.AssetBundleRequestAllAssets.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. #pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
  2. #if UNITY_2018_4 || UNITY_2019_4_OR_NEWER
  3. #if UNITASK_ASSETBUNDLE_SUPPORT
  4. using Cysharp.Threading.Tasks.Internal;
  5. using System;
  6. using System.Runtime.CompilerServices;
  7. using System.Threading;
  8. using UnityEngine;
  9. namespace Cysharp.Threading.Tasks
  10. {
  11. public static partial class UnityAsyncExtensions
  12. {
  13. public static AssetBundleRequestAllAssetsAwaiter AwaitForAllAssets(this AssetBundleRequest asyncOperation)
  14. {
  15. Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation));
  16. return new AssetBundleRequestAllAssetsAwaiter(asyncOperation);
  17. }
  18. public static UniTask<UnityEngine.Object[]> AwaitForAllAssets(this AssetBundleRequest asyncOperation, CancellationToken cancellationToken)
  19. {
  20. return AwaitForAllAssets(asyncOperation, null, PlayerLoopTiming.Update, cancellationToken: cancellationToken);
  21. }
  22. public static UniTask<UnityEngine.Object[]> AwaitForAllAssets(this AssetBundleRequest asyncOperation, IProgress<float> progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken))
  23. {
  24. Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation));
  25. if (cancellationToken.IsCancellationRequested) return UniTask.FromCanceled<UnityEngine.Object[]>(cancellationToken);
  26. if (asyncOperation.isDone) return UniTask.FromResult(asyncOperation.allAssets);
  27. return new UniTask<UnityEngine.Object[]>(AssetBundleRequestAllAssetsConfiguredSource.Create(asyncOperation, timing, progress, cancellationToken, out var token), token);
  28. }
  29. public struct AssetBundleRequestAllAssetsAwaiter : ICriticalNotifyCompletion
  30. {
  31. AssetBundleRequest asyncOperation;
  32. Action<AsyncOperation> continuationAction;
  33. public AssetBundleRequestAllAssetsAwaiter(AssetBundleRequest asyncOperation)
  34. {
  35. this.asyncOperation = asyncOperation;
  36. this.continuationAction = null;
  37. }
  38. public AssetBundleRequestAllAssetsAwaiter GetAwaiter()
  39. {
  40. return this;
  41. }
  42. public bool IsCompleted => asyncOperation.isDone;
  43. public UnityEngine.Object[] GetResult()
  44. {
  45. if (continuationAction != null)
  46. {
  47. asyncOperation.completed -= continuationAction;
  48. continuationAction = null;
  49. var result = asyncOperation.allAssets;
  50. asyncOperation = null;
  51. return result;
  52. }
  53. else
  54. {
  55. var result = asyncOperation.allAssets;
  56. asyncOperation = null;
  57. return result;
  58. }
  59. }
  60. public void OnCompleted(Action continuation)
  61. {
  62. UnsafeOnCompleted(continuation);
  63. }
  64. public void UnsafeOnCompleted(Action continuation)
  65. {
  66. Error.ThrowWhenContinuationIsAlreadyRegistered(continuationAction);
  67. continuationAction = PooledDelegate<AsyncOperation>.Create(continuation);
  68. asyncOperation.completed += continuationAction;
  69. }
  70. }
  71. sealed class AssetBundleRequestAllAssetsConfiguredSource : IUniTaskSource<UnityEngine.Object[]>, IPlayerLoopItem, ITaskPoolNode<AssetBundleRequestAllAssetsConfiguredSource>
  72. {
  73. static TaskPool<AssetBundleRequestAllAssetsConfiguredSource> pool;
  74. AssetBundleRequestAllAssetsConfiguredSource nextNode;
  75. public ref AssetBundleRequestAllAssetsConfiguredSource NextNode => ref nextNode;
  76. static AssetBundleRequestAllAssetsConfiguredSource()
  77. {
  78. TaskPool.RegisterSizeGetter(typeof(AssetBundleRequestAllAssetsConfiguredSource), () => pool.Size);
  79. }
  80. AssetBundleRequest asyncOperation;
  81. IProgress<float> progress;
  82. CancellationToken cancellationToken;
  83. UniTaskCompletionSourceCore<UnityEngine.Object[]> core;
  84. AssetBundleRequestAllAssetsConfiguredSource()
  85. {
  86. }
  87. public static IUniTaskSource<UnityEngine.Object[]> Create(AssetBundleRequest asyncOperation, PlayerLoopTiming timing, IProgress<float> progress, CancellationToken cancellationToken, out short token)
  88. {
  89. if (cancellationToken.IsCancellationRequested)
  90. {
  91. return AutoResetUniTaskCompletionSource<UnityEngine.Object[]>.CreateFromCanceled(cancellationToken, out token);
  92. }
  93. if (!pool.TryPop(out var result))
  94. {
  95. result = new AssetBundleRequestAllAssetsConfiguredSource();
  96. }
  97. result.asyncOperation = asyncOperation;
  98. result.progress = progress;
  99. result.cancellationToken = cancellationToken;
  100. TaskTracker.TrackActiveTask(result, 3);
  101. PlayerLoopHelper.AddAction(timing, result);
  102. token = result.core.Version;
  103. return result;
  104. }
  105. public UnityEngine.Object[] GetResult(short token)
  106. {
  107. try
  108. {
  109. return core.GetResult(token);
  110. }
  111. finally
  112. {
  113. TryReturn();
  114. }
  115. }
  116. void IUniTaskSource.GetResult(short token)
  117. {
  118. GetResult(token);
  119. }
  120. public UniTaskStatus GetStatus(short token)
  121. {
  122. return core.GetStatus(token);
  123. }
  124. public UniTaskStatus UnsafeGetStatus()
  125. {
  126. return core.UnsafeGetStatus();
  127. }
  128. public void OnCompleted(Action<object> continuation, object state, short token)
  129. {
  130. core.OnCompleted(continuation, state, token);
  131. }
  132. public bool MoveNext()
  133. {
  134. if (cancellationToken.IsCancellationRequested)
  135. {
  136. core.TrySetCanceled(cancellationToken);
  137. return false;
  138. }
  139. if (progress != null)
  140. {
  141. progress.Report(asyncOperation.progress);
  142. }
  143. if (asyncOperation.isDone)
  144. {
  145. core.TrySetResult(asyncOperation.allAssets);
  146. return false;
  147. }
  148. return true;
  149. }
  150. bool TryReturn()
  151. {
  152. TaskTracker.RemoveTracking(this);
  153. core.Reset();
  154. asyncOperation = default;
  155. progress = default;
  156. cancellationToken = default;
  157. return pool.TryPush(this);
  158. }
  159. }
  160. }
  161. }
  162. #endif
  163. #endif