UnityAsyncExtensions.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932
  1. #pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
  2. using System;
  3. using System.Runtime.CompilerServices;
  4. using System.Threading;
  5. using UnityEngine;
  6. using Cysharp.Threading.Tasks.Internal;
  7. #if ENABLE_UNITYWEBREQUEST && (!UNITY_2019_1_OR_NEWER || UNITASK_WEBREQUEST_SUPPORT)
  8. using UnityEngine.Networking;
  9. #endif
  10. namespace Cysharp.Threading.Tasks
  11. {
  12. public static partial class UnityAsyncExtensions
  13. {
  14. #region AsyncOperation
  15. #if !UNITY_2023_1_OR_NEWER
  16. // from Unity2023.1.0a15, AsyncOperationAwaitableExtensions.GetAwaiter is defined in UnityEngine.
  17. public static AsyncOperationAwaiter GetAwaiter(this AsyncOperation asyncOperation)
  18. {
  19. Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation));
  20. return new AsyncOperationAwaiter(asyncOperation);
  21. }
  22. #endif
  23. public static UniTask WithCancellation(this AsyncOperation asyncOperation, CancellationToken cancellationToken)
  24. {
  25. return ToUniTask(asyncOperation, cancellationToken: cancellationToken);
  26. }
  27. public static UniTask ToUniTask(this AsyncOperation asyncOperation, IProgress<float> progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken))
  28. {
  29. Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation));
  30. if (cancellationToken.IsCancellationRequested) return UniTask.FromCanceled(cancellationToken);
  31. if (asyncOperation.isDone) return UniTask.CompletedTask;
  32. return new UniTask(AsyncOperationConfiguredSource.Create(asyncOperation, timing, progress, cancellationToken, out var token), token);
  33. }
  34. public struct AsyncOperationAwaiter : ICriticalNotifyCompletion
  35. {
  36. AsyncOperation asyncOperation;
  37. Action<AsyncOperation> continuationAction;
  38. public AsyncOperationAwaiter(AsyncOperation asyncOperation)
  39. {
  40. this.asyncOperation = asyncOperation;
  41. this.continuationAction = null;
  42. }
  43. public bool IsCompleted => asyncOperation.isDone;
  44. public void GetResult()
  45. {
  46. if (continuationAction != null)
  47. {
  48. asyncOperation.completed -= continuationAction;
  49. continuationAction = null;
  50. asyncOperation = null;
  51. }
  52. else
  53. {
  54. asyncOperation = null;
  55. }
  56. }
  57. public void OnCompleted(Action continuation)
  58. {
  59. UnsafeOnCompleted(continuation);
  60. }
  61. public void UnsafeOnCompleted(Action continuation)
  62. {
  63. Error.ThrowWhenContinuationIsAlreadyRegistered(continuationAction);
  64. continuationAction = PooledDelegate<AsyncOperation>.Create(continuation);
  65. asyncOperation.completed += continuationAction;
  66. }
  67. }
  68. sealed class AsyncOperationConfiguredSource : IUniTaskSource, IPlayerLoopItem, ITaskPoolNode<AsyncOperationConfiguredSource>
  69. {
  70. static TaskPool<AsyncOperationConfiguredSource> pool;
  71. AsyncOperationConfiguredSource nextNode;
  72. public ref AsyncOperationConfiguredSource NextNode => ref nextNode;
  73. static AsyncOperationConfiguredSource()
  74. {
  75. TaskPool.RegisterSizeGetter(typeof(AsyncOperationConfiguredSource), () => pool.Size);
  76. }
  77. AsyncOperation asyncOperation;
  78. IProgress<float> progress;
  79. CancellationToken cancellationToken;
  80. UniTaskCompletionSourceCore<AsyncUnit> core;
  81. AsyncOperationConfiguredSource()
  82. {
  83. }
  84. public static IUniTaskSource Create(AsyncOperation asyncOperation, PlayerLoopTiming timing, IProgress<float> progress, CancellationToken cancellationToken, out short token)
  85. {
  86. if (cancellationToken.IsCancellationRequested)
  87. {
  88. return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token);
  89. }
  90. if (!pool.TryPop(out var result))
  91. {
  92. result = new AsyncOperationConfiguredSource();
  93. }
  94. result.asyncOperation = asyncOperation;
  95. result.progress = progress;
  96. result.cancellationToken = cancellationToken;
  97. TaskTracker.TrackActiveTask(result, 3);
  98. PlayerLoopHelper.AddAction(timing, result);
  99. token = result.core.Version;
  100. return result;
  101. }
  102. public void GetResult(short token)
  103. {
  104. try
  105. {
  106. core.GetResult(token);
  107. }
  108. finally
  109. {
  110. TryReturn();
  111. }
  112. }
  113. public UniTaskStatus GetStatus(short token)
  114. {
  115. return core.GetStatus(token);
  116. }
  117. public UniTaskStatus UnsafeGetStatus()
  118. {
  119. return core.UnsafeGetStatus();
  120. }
  121. public void OnCompleted(Action<object> continuation, object state, short token)
  122. {
  123. core.OnCompleted(continuation, state, token);
  124. }
  125. public bool MoveNext()
  126. {
  127. if (cancellationToken.IsCancellationRequested)
  128. {
  129. core.TrySetCanceled(cancellationToken);
  130. return false;
  131. }
  132. if (progress != null)
  133. {
  134. progress.Report(asyncOperation.progress);
  135. }
  136. if (asyncOperation.isDone)
  137. {
  138. core.TrySetResult(AsyncUnit.Default);
  139. return false;
  140. }
  141. return true;
  142. }
  143. bool TryReturn()
  144. {
  145. TaskTracker.RemoveTracking(this);
  146. core.Reset();
  147. asyncOperation = default;
  148. progress = default;
  149. cancellationToken = default;
  150. return pool.TryPush(this);
  151. }
  152. }
  153. #endregion
  154. #region ResourceRequest
  155. public static ResourceRequestAwaiter GetAwaiter(this ResourceRequest asyncOperation)
  156. {
  157. Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation));
  158. return new ResourceRequestAwaiter(asyncOperation);
  159. }
  160. public static UniTask<UnityEngine.Object> WithCancellation(this ResourceRequest asyncOperation, CancellationToken cancellationToken)
  161. {
  162. return ToUniTask(asyncOperation, cancellationToken: cancellationToken);
  163. }
  164. public static UniTask<UnityEngine.Object> ToUniTask(this ResourceRequest asyncOperation, IProgress<float> progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken))
  165. {
  166. Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation));
  167. if (cancellationToken.IsCancellationRequested) return UniTask.FromCanceled<UnityEngine.Object>(cancellationToken);
  168. if (asyncOperation.isDone) return UniTask.FromResult(asyncOperation.asset);
  169. return new UniTask<UnityEngine.Object>(ResourceRequestConfiguredSource.Create(asyncOperation, timing, progress, cancellationToken, out var token), token);
  170. }
  171. public struct ResourceRequestAwaiter : ICriticalNotifyCompletion
  172. {
  173. ResourceRequest asyncOperation;
  174. Action<AsyncOperation> continuationAction;
  175. public ResourceRequestAwaiter(ResourceRequest asyncOperation)
  176. {
  177. this.asyncOperation = asyncOperation;
  178. this.continuationAction = null;
  179. }
  180. public bool IsCompleted => asyncOperation.isDone;
  181. public UnityEngine.Object GetResult()
  182. {
  183. if (continuationAction != null)
  184. {
  185. asyncOperation.completed -= continuationAction;
  186. continuationAction = null;
  187. var result = asyncOperation.asset;
  188. asyncOperation = null;
  189. return result;
  190. }
  191. else
  192. {
  193. var result = asyncOperation.asset;
  194. asyncOperation = null;
  195. return result;
  196. }
  197. }
  198. public void OnCompleted(Action continuation)
  199. {
  200. UnsafeOnCompleted(continuation);
  201. }
  202. public void UnsafeOnCompleted(Action continuation)
  203. {
  204. Error.ThrowWhenContinuationIsAlreadyRegistered(continuationAction);
  205. continuationAction = PooledDelegate<AsyncOperation>.Create(continuation);
  206. asyncOperation.completed += continuationAction;
  207. }
  208. }
  209. sealed class ResourceRequestConfiguredSource : IUniTaskSource<UnityEngine.Object>, IPlayerLoopItem, ITaskPoolNode<ResourceRequestConfiguredSource>
  210. {
  211. static TaskPool<ResourceRequestConfiguredSource> pool;
  212. ResourceRequestConfiguredSource nextNode;
  213. public ref ResourceRequestConfiguredSource NextNode => ref nextNode;
  214. static ResourceRequestConfiguredSource()
  215. {
  216. TaskPool.RegisterSizeGetter(typeof(ResourceRequestConfiguredSource), () => pool.Size);
  217. }
  218. ResourceRequest asyncOperation;
  219. IProgress<float> progress;
  220. CancellationToken cancellationToken;
  221. UniTaskCompletionSourceCore<UnityEngine.Object> core;
  222. ResourceRequestConfiguredSource()
  223. {
  224. }
  225. public static IUniTaskSource<UnityEngine.Object> Create(ResourceRequest asyncOperation, PlayerLoopTiming timing, IProgress<float> progress, CancellationToken cancellationToken, out short token)
  226. {
  227. if (cancellationToken.IsCancellationRequested)
  228. {
  229. return AutoResetUniTaskCompletionSource<UnityEngine.Object>.CreateFromCanceled(cancellationToken, out token);
  230. }
  231. if (!pool.TryPop(out var result))
  232. {
  233. result = new ResourceRequestConfiguredSource();
  234. }
  235. result.asyncOperation = asyncOperation;
  236. result.progress = progress;
  237. result.cancellationToken = cancellationToken;
  238. TaskTracker.TrackActiveTask(result, 3);
  239. PlayerLoopHelper.AddAction(timing, result);
  240. token = result.core.Version;
  241. return result;
  242. }
  243. public UnityEngine.Object GetResult(short token)
  244. {
  245. try
  246. {
  247. return core.GetResult(token);
  248. }
  249. finally
  250. {
  251. TryReturn();
  252. }
  253. }
  254. void IUniTaskSource.GetResult(short token)
  255. {
  256. GetResult(token);
  257. }
  258. public UniTaskStatus GetStatus(short token)
  259. {
  260. return core.GetStatus(token);
  261. }
  262. public UniTaskStatus UnsafeGetStatus()
  263. {
  264. return core.UnsafeGetStatus();
  265. }
  266. public void OnCompleted(Action<object> continuation, object state, short token)
  267. {
  268. core.OnCompleted(continuation, state, token);
  269. }
  270. public bool MoveNext()
  271. {
  272. if (cancellationToken.IsCancellationRequested)
  273. {
  274. core.TrySetCanceled(cancellationToken);
  275. return false;
  276. }
  277. if (progress != null)
  278. {
  279. progress.Report(asyncOperation.progress);
  280. }
  281. if (asyncOperation.isDone)
  282. {
  283. core.TrySetResult(asyncOperation.asset);
  284. return false;
  285. }
  286. return true;
  287. }
  288. bool TryReturn()
  289. {
  290. TaskTracker.RemoveTracking(this);
  291. core.Reset();
  292. asyncOperation = default;
  293. progress = default;
  294. cancellationToken = default;
  295. return pool.TryPush(this);
  296. }
  297. }
  298. #endregion
  299. #if UNITASK_ASSETBUNDLE_SUPPORT
  300. #region AssetBundleRequest
  301. public static AssetBundleRequestAwaiter GetAwaiter(this AssetBundleRequest asyncOperation)
  302. {
  303. Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation));
  304. return new AssetBundleRequestAwaiter(asyncOperation);
  305. }
  306. public static UniTask<UnityEngine.Object> WithCancellation(this AssetBundleRequest asyncOperation, CancellationToken cancellationToken)
  307. {
  308. return ToUniTask(asyncOperation, cancellationToken: cancellationToken);
  309. }
  310. public static UniTask<UnityEngine.Object> ToUniTask(this AssetBundleRequest asyncOperation, IProgress<float> progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken))
  311. {
  312. Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation));
  313. if (cancellationToken.IsCancellationRequested) return UniTask.FromCanceled<UnityEngine.Object>(cancellationToken);
  314. if (asyncOperation.isDone) return UniTask.FromResult(asyncOperation.asset);
  315. return new UniTask<UnityEngine.Object>(AssetBundleRequestConfiguredSource.Create(asyncOperation, timing, progress, cancellationToken, out var token), token);
  316. }
  317. public struct AssetBundleRequestAwaiter : ICriticalNotifyCompletion
  318. {
  319. AssetBundleRequest asyncOperation;
  320. Action<AsyncOperation> continuationAction;
  321. public AssetBundleRequestAwaiter(AssetBundleRequest asyncOperation)
  322. {
  323. this.asyncOperation = asyncOperation;
  324. this.continuationAction = null;
  325. }
  326. public bool IsCompleted => asyncOperation.isDone;
  327. public UnityEngine.Object GetResult()
  328. {
  329. if (continuationAction != null)
  330. {
  331. asyncOperation.completed -= continuationAction;
  332. continuationAction = null;
  333. var result = asyncOperation.asset;
  334. asyncOperation = null;
  335. return result;
  336. }
  337. else
  338. {
  339. var result = asyncOperation.asset;
  340. asyncOperation = null;
  341. return result;
  342. }
  343. }
  344. public void OnCompleted(Action continuation)
  345. {
  346. UnsafeOnCompleted(continuation);
  347. }
  348. public void UnsafeOnCompleted(Action continuation)
  349. {
  350. Error.ThrowWhenContinuationIsAlreadyRegistered(continuationAction);
  351. continuationAction = PooledDelegate<AsyncOperation>.Create(continuation);
  352. asyncOperation.completed += continuationAction;
  353. }
  354. }
  355. sealed class AssetBundleRequestConfiguredSource : IUniTaskSource<UnityEngine.Object>, IPlayerLoopItem, ITaskPoolNode<AssetBundleRequestConfiguredSource>
  356. {
  357. static TaskPool<AssetBundleRequestConfiguredSource> pool;
  358. AssetBundleRequestConfiguredSource nextNode;
  359. public ref AssetBundleRequestConfiguredSource NextNode => ref nextNode;
  360. static AssetBundleRequestConfiguredSource()
  361. {
  362. TaskPool.RegisterSizeGetter(typeof(AssetBundleRequestConfiguredSource), () => pool.Size);
  363. }
  364. AssetBundleRequest asyncOperation;
  365. IProgress<float> progress;
  366. CancellationToken cancellationToken;
  367. UniTaskCompletionSourceCore<UnityEngine.Object> core;
  368. AssetBundleRequestConfiguredSource()
  369. {
  370. }
  371. public static IUniTaskSource<UnityEngine.Object> Create(AssetBundleRequest asyncOperation, PlayerLoopTiming timing, IProgress<float> progress, CancellationToken cancellationToken, out short token)
  372. {
  373. if (cancellationToken.IsCancellationRequested)
  374. {
  375. return AutoResetUniTaskCompletionSource<UnityEngine.Object>.CreateFromCanceled(cancellationToken, out token);
  376. }
  377. if (!pool.TryPop(out var result))
  378. {
  379. result = new AssetBundleRequestConfiguredSource();
  380. }
  381. result.asyncOperation = asyncOperation;
  382. result.progress = progress;
  383. result.cancellationToken = cancellationToken;
  384. TaskTracker.TrackActiveTask(result, 3);
  385. PlayerLoopHelper.AddAction(timing, result);
  386. token = result.core.Version;
  387. return result;
  388. }
  389. public UnityEngine.Object GetResult(short token)
  390. {
  391. try
  392. {
  393. return core.GetResult(token);
  394. }
  395. finally
  396. {
  397. TryReturn();
  398. }
  399. }
  400. void IUniTaskSource.GetResult(short token)
  401. {
  402. GetResult(token);
  403. }
  404. public UniTaskStatus GetStatus(short token)
  405. {
  406. return core.GetStatus(token);
  407. }
  408. public UniTaskStatus UnsafeGetStatus()
  409. {
  410. return core.UnsafeGetStatus();
  411. }
  412. public void OnCompleted(Action<object> continuation, object state, short token)
  413. {
  414. core.OnCompleted(continuation, state, token);
  415. }
  416. public bool MoveNext()
  417. {
  418. if (cancellationToken.IsCancellationRequested)
  419. {
  420. core.TrySetCanceled(cancellationToken);
  421. return false;
  422. }
  423. if (progress != null)
  424. {
  425. progress.Report(asyncOperation.progress);
  426. }
  427. if (asyncOperation.isDone)
  428. {
  429. core.TrySetResult(asyncOperation.asset);
  430. return false;
  431. }
  432. return true;
  433. }
  434. bool TryReturn()
  435. {
  436. TaskTracker.RemoveTracking(this);
  437. core.Reset();
  438. asyncOperation = default;
  439. progress = default;
  440. cancellationToken = default;
  441. return pool.TryPush(this);
  442. }
  443. }
  444. #endregion
  445. #endif
  446. #if UNITASK_ASSETBUNDLE_SUPPORT
  447. #region AssetBundleCreateRequest
  448. public static AssetBundleCreateRequestAwaiter GetAwaiter(this AssetBundleCreateRequest asyncOperation)
  449. {
  450. Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation));
  451. return new AssetBundleCreateRequestAwaiter(asyncOperation);
  452. }
  453. public static UniTask<AssetBundle> WithCancellation(this AssetBundleCreateRequest asyncOperation, CancellationToken cancellationToken)
  454. {
  455. return ToUniTask(asyncOperation, cancellationToken: cancellationToken);
  456. }
  457. public static UniTask<AssetBundle> ToUniTask(this AssetBundleCreateRequest asyncOperation, IProgress<float> progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken))
  458. {
  459. Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation));
  460. if (cancellationToken.IsCancellationRequested) return UniTask.FromCanceled<AssetBundle>(cancellationToken);
  461. if (asyncOperation.isDone) return UniTask.FromResult(asyncOperation.assetBundle);
  462. return new UniTask<AssetBundle>(AssetBundleCreateRequestConfiguredSource.Create(asyncOperation, timing, progress, cancellationToken, out var token), token);
  463. }
  464. public struct AssetBundleCreateRequestAwaiter : ICriticalNotifyCompletion
  465. {
  466. AssetBundleCreateRequest asyncOperation;
  467. Action<AsyncOperation> continuationAction;
  468. public AssetBundleCreateRequestAwaiter(AssetBundleCreateRequest asyncOperation)
  469. {
  470. this.asyncOperation = asyncOperation;
  471. this.continuationAction = null;
  472. }
  473. public bool IsCompleted => asyncOperation.isDone;
  474. public AssetBundle GetResult()
  475. {
  476. if (continuationAction != null)
  477. {
  478. asyncOperation.completed -= continuationAction;
  479. continuationAction = null;
  480. var result = asyncOperation.assetBundle;
  481. asyncOperation = null;
  482. return result;
  483. }
  484. else
  485. {
  486. var result = asyncOperation.assetBundle;
  487. asyncOperation = null;
  488. return result;
  489. }
  490. }
  491. public void OnCompleted(Action continuation)
  492. {
  493. UnsafeOnCompleted(continuation);
  494. }
  495. public void UnsafeOnCompleted(Action continuation)
  496. {
  497. Error.ThrowWhenContinuationIsAlreadyRegistered(continuationAction);
  498. continuationAction = PooledDelegate<AsyncOperation>.Create(continuation);
  499. asyncOperation.completed += continuationAction;
  500. }
  501. }
  502. sealed class AssetBundleCreateRequestConfiguredSource : IUniTaskSource<AssetBundle>, IPlayerLoopItem, ITaskPoolNode<AssetBundleCreateRequestConfiguredSource>
  503. {
  504. static TaskPool<AssetBundleCreateRequestConfiguredSource> pool;
  505. AssetBundleCreateRequestConfiguredSource nextNode;
  506. public ref AssetBundleCreateRequestConfiguredSource NextNode => ref nextNode;
  507. static AssetBundleCreateRequestConfiguredSource()
  508. {
  509. TaskPool.RegisterSizeGetter(typeof(AssetBundleCreateRequestConfiguredSource), () => pool.Size);
  510. }
  511. AssetBundleCreateRequest asyncOperation;
  512. IProgress<float> progress;
  513. CancellationToken cancellationToken;
  514. UniTaskCompletionSourceCore<AssetBundle> core;
  515. AssetBundleCreateRequestConfiguredSource()
  516. {
  517. }
  518. public static IUniTaskSource<AssetBundle> Create(AssetBundleCreateRequest asyncOperation, PlayerLoopTiming timing, IProgress<float> progress, CancellationToken cancellationToken, out short token)
  519. {
  520. if (cancellationToken.IsCancellationRequested)
  521. {
  522. return AutoResetUniTaskCompletionSource<AssetBundle>.CreateFromCanceled(cancellationToken, out token);
  523. }
  524. if (!pool.TryPop(out var result))
  525. {
  526. result = new AssetBundleCreateRequestConfiguredSource();
  527. }
  528. result.asyncOperation = asyncOperation;
  529. result.progress = progress;
  530. result.cancellationToken = cancellationToken;
  531. TaskTracker.TrackActiveTask(result, 3);
  532. PlayerLoopHelper.AddAction(timing, result);
  533. token = result.core.Version;
  534. return result;
  535. }
  536. public AssetBundle GetResult(short token)
  537. {
  538. try
  539. {
  540. return core.GetResult(token);
  541. }
  542. finally
  543. {
  544. TryReturn();
  545. }
  546. }
  547. void IUniTaskSource.GetResult(short token)
  548. {
  549. GetResult(token);
  550. }
  551. public UniTaskStatus GetStatus(short token)
  552. {
  553. return core.GetStatus(token);
  554. }
  555. public UniTaskStatus UnsafeGetStatus()
  556. {
  557. return core.UnsafeGetStatus();
  558. }
  559. public void OnCompleted(Action<object> continuation, object state, short token)
  560. {
  561. core.OnCompleted(continuation, state, token);
  562. }
  563. public bool MoveNext()
  564. {
  565. if (cancellationToken.IsCancellationRequested)
  566. {
  567. core.TrySetCanceled(cancellationToken);
  568. return false;
  569. }
  570. if (progress != null)
  571. {
  572. progress.Report(asyncOperation.progress);
  573. }
  574. if (asyncOperation.isDone)
  575. {
  576. core.TrySetResult(asyncOperation.assetBundle);
  577. return false;
  578. }
  579. return true;
  580. }
  581. bool TryReturn()
  582. {
  583. TaskTracker.RemoveTracking(this);
  584. core.Reset();
  585. asyncOperation = default;
  586. progress = default;
  587. cancellationToken = default;
  588. return pool.TryPush(this);
  589. }
  590. }
  591. #endregion
  592. #endif
  593. #if ENABLE_UNITYWEBREQUEST && (!UNITY_2019_1_OR_NEWER || UNITASK_WEBREQUEST_SUPPORT)
  594. #region UnityWebRequestAsyncOperation
  595. public static UnityWebRequestAsyncOperationAwaiter GetAwaiter(this UnityWebRequestAsyncOperation asyncOperation)
  596. {
  597. Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation));
  598. return new UnityWebRequestAsyncOperationAwaiter(asyncOperation);
  599. }
  600. public static UniTask<UnityWebRequest> WithCancellation(this UnityWebRequestAsyncOperation asyncOperation, CancellationToken cancellationToken)
  601. {
  602. return ToUniTask(asyncOperation, cancellationToken: cancellationToken);
  603. }
  604. public static UniTask<UnityWebRequest> ToUniTask(this UnityWebRequestAsyncOperation asyncOperation, IProgress<float> progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken))
  605. {
  606. Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation));
  607. if (cancellationToken.IsCancellationRequested) return UniTask.FromCanceled<UnityWebRequest>(cancellationToken);
  608. if (asyncOperation.isDone)
  609. {
  610. if (asyncOperation.webRequest.IsError())
  611. {
  612. return UniTask.FromException<UnityWebRequest>(new UnityWebRequestException(asyncOperation.webRequest));
  613. }
  614. return UniTask.FromResult(asyncOperation.webRequest);
  615. }
  616. return new UniTask<UnityWebRequest>(UnityWebRequestAsyncOperationConfiguredSource.Create(asyncOperation, timing, progress, cancellationToken, out var token), token);
  617. }
  618. public struct UnityWebRequestAsyncOperationAwaiter : ICriticalNotifyCompletion
  619. {
  620. UnityWebRequestAsyncOperation asyncOperation;
  621. Action<AsyncOperation> continuationAction;
  622. public UnityWebRequestAsyncOperationAwaiter(UnityWebRequestAsyncOperation asyncOperation)
  623. {
  624. this.asyncOperation = asyncOperation;
  625. this.continuationAction = null;
  626. }
  627. public bool IsCompleted => asyncOperation.isDone;
  628. public UnityWebRequest GetResult()
  629. {
  630. if (continuationAction != null)
  631. {
  632. asyncOperation.completed -= continuationAction;
  633. continuationAction = null;
  634. var result = asyncOperation.webRequest;
  635. asyncOperation = null;
  636. if (result.IsError())
  637. {
  638. throw new UnityWebRequestException(result);
  639. }
  640. return result;
  641. }
  642. else
  643. {
  644. var result = asyncOperation.webRequest;
  645. asyncOperation = null;
  646. if (result.IsError())
  647. {
  648. throw new UnityWebRequestException(result);
  649. }
  650. return result;
  651. }
  652. }
  653. public void OnCompleted(Action continuation)
  654. {
  655. UnsafeOnCompleted(continuation);
  656. }
  657. public void UnsafeOnCompleted(Action continuation)
  658. {
  659. Error.ThrowWhenContinuationIsAlreadyRegistered(continuationAction);
  660. continuationAction = PooledDelegate<AsyncOperation>.Create(continuation);
  661. asyncOperation.completed += continuationAction;
  662. }
  663. }
  664. sealed class UnityWebRequestAsyncOperationConfiguredSource : IUniTaskSource<UnityWebRequest>, IPlayerLoopItem, ITaskPoolNode<UnityWebRequestAsyncOperationConfiguredSource>
  665. {
  666. static TaskPool<UnityWebRequestAsyncOperationConfiguredSource> pool;
  667. UnityWebRequestAsyncOperationConfiguredSource nextNode;
  668. public ref UnityWebRequestAsyncOperationConfiguredSource NextNode => ref nextNode;
  669. static UnityWebRequestAsyncOperationConfiguredSource()
  670. {
  671. TaskPool.RegisterSizeGetter(typeof(UnityWebRequestAsyncOperationConfiguredSource), () => pool.Size);
  672. }
  673. UnityWebRequestAsyncOperation asyncOperation;
  674. IProgress<float> progress;
  675. CancellationToken cancellationToken;
  676. UniTaskCompletionSourceCore<UnityWebRequest> core;
  677. UnityWebRequestAsyncOperationConfiguredSource()
  678. {
  679. }
  680. public static IUniTaskSource<UnityWebRequest> Create(UnityWebRequestAsyncOperation asyncOperation, PlayerLoopTiming timing, IProgress<float> progress, CancellationToken cancellationToken, out short token)
  681. {
  682. if (cancellationToken.IsCancellationRequested)
  683. {
  684. return AutoResetUniTaskCompletionSource<UnityWebRequest>.CreateFromCanceled(cancellationToken, out token);
  685. }
  686. if (!pool.TryPop(out var result))
  687. {
  688. result = new UnityWebRequestAsyncOperationConfiguredSource();
  689. }
  690. result.asyncOperation = asyncOperation;
  691. result.progress = progress;
  692. result.cancellationToken = cancellationToken;
  693. TaskTracker.TrackActiveTask(result, 3);
  694. PlayerLoopHelper.AddAction(timing, result);
  695. token = result.core.Version;
  696. return result;
  697. }
  698. public UnityWebRequest GetResult(short token)
  699. {
  700. try
  701. {
  702. return core.GetResult(token);
  703. }
  704. finally
  705. {
  706. TryReturn();
  707. }
  708. }
  709. void IUniTaskSource.GetResult(short token)
  710. {
  711. GetResult(token);
  712. }
  713. public UniTaskStatus GetStatus(short token)
  714. {
  715. return core.GetStatus(token);
  716. }
  717. public UniTaskStatus UnsafeGetStatus()
  718. {
  719. return core.UnsafeGetStatus();
  720. }
  721. public void OnCompleted(Action<object> continuation, object state, short token)
  722. {
  723. core.OnCompleted(continuation, state, token);
  724. }
  725. public bool MoveNext()
  726. {
  727. if (cancellationToken.IsCancellationRequested)
  728. {
  729. asyncOperation.webRequest.Abort();
  730. core.TrySetCanceled(cancellationToken);
  731. return false;
  732. }
  733. if (progress != null)
  734. {
  735. progress.Report(asyncOperation.progress);
  736. }
  737. if (asyncOperation.isDone)
  738. {
  739. if (asyncOperation.webRequest.IsError())
  740. {
  741. core.TrySetException(new UnityWebRequestException(asyncOperation.webRequest));
  742. }
  743. else
  744. {
  745. core.TrySetResult(asyncOperation.webRequest);
  746. }
  747. return false;
  748. }
  749. return true;
  750. }
  751. bool TryReturn()
  752. {
  753. TaskTracker.RemoveTracking(this);
  754. core.Reset();
  755. asyncOperation = default;
  756. progress = default;
  757. cancellationToken = default;
  758. return pool.TryPush(this);
  759. }
  760. }
  761. #endregion
  762. #endif
  763. }
  764. }