123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- #pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
- #if UNITY_2018_4 || UNITY_2019_4_OR_NEWER
- #if UNITASK_ASSETBUNDLE_SUPPORT
- using Cysharp.Threading.Tasks.Internal;
- using System;
- using System.Runtime.CompilerServices;
- using System.Threading;
- using UnityEngine;
- namespace Cysharp.Threading.Tasks
- {
- public static partial class UnityAsyncExtensions
- {
- public static AssetBundleRequestAllAssetsAwaiter AwaitForAllAssets(this AssetBundleRequest asyncOperation)
- {
- Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation));
- return new AssetBundleRequestAllAssetsAwaiter(asyncOperation);
- }
- public static UniTask<UnityEngine.Object[]> AwaitForAllAssets(this AssetBundleRequest asyncOperation, CancellationToken cancellationToken)
- {
- return AwaitForAllAssets(asyncOperation, null, PlayerLoopTiming.Update, cancellationToken: cancellationToken);
- }
- public static UniTask<UnityEngine.Object[]> AwaitForAllAssets(this AssetBundleRequest asyncOperation, IProgress<float> progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken))
- {
- Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation));
- if (cancellationToken.IsCancellationRequested) return UniTask.FromCanceled<UnityEngine.Object[]>(cancellationToken);
- if (asyncOperation.isDone) return UniTask.FromResult(asyncOperation.allAssets);
- return new UniTask<UnityEngine.Object[]>(AssetBundleRequestAllAssetsConfiguredSource.Create(asyncOperation, timing, progress, cancellationToken, out var token), token);
- }
- public struct AssetBundleRequestAllAssetsAwaiter : ICriticalNotifyCompletion
- {
- AssetBundleRequest asyncOperation;
- Action<AsyncOperation> continuationAction;
- public AssetBundleRequestAllAssetsAwaiter(AssetBundleRequest asyncOperation)
- {
- this.asyncOperation = asyncOperation;
- this.continuationAction = null;
- }
- public AssetBundleRequestAllAssetsAwaiter GetAwaiter()
- {
- return this;
- }
- public bool IsCompleted => asyncOperation.isDone;
- public UnityEngine.Object[] GetResult()
- {
- if (continuationAction != null)
- {
- asyncOperation.completed -= continuationAction;
- continuationAction = null;
- var result = asyncOperation.allAssets;
- asyncOperation = null;
- return result;
- }
- else
- {
- var result = asyncOperation.allAssets;
- asyncOperation = null;
- return result;
- }
- }
- public void OnCompleted(Action continuation)
- {
- UnsafeOnCompleted(continuation);
- }
- public void UnsafeOnCompleted(Action continuation)
- {
- Error.ThrowWhenContinuationIsAlreadyRegistered(continuationAction);
- continuationAction = PooledDelegate<AsyncOperation>.Create(continuation);
- asyncOperation.completed += continuationAction;
- }
- }
- sealed class AssetBundleRequestAllAssetsConfiguredSource : IUniTaskSource<UnityEngine.Object[]>, IPlayerLoopItem, ITaskPoolNode<AssetBundleRequestAllAssetsConfiguredSource>
- {
- static TaskPool<AssetBundleRequestAllAssetsConfiguredSource> pool;
- AssetBundleRequestAllAssetsConfiguredSource nextNode;
- public ref AssetBundleRequestAllAssetsConfiguredSource NextNode => ref nextNode;
- static AssetBundleRequestAllAssetsConfiguredSource()
- {
- TaskPool.RegisterSizeGetter(typeof(AssetBundleRequestAllAssetsConfiguredSource), () => pool.Size);
- }
- AssetBundleRequest asyncOperation;
- IProgress<float> progress;
- CancellationToken cancellationToken;
- UniTaskCompletionSourceCore<UnityEngine.Object[]> core;
- AssetBundleRequestAllAssetsConfiguredSource()
- {
- }
- public static IUniTaskSource<UnityEngine.Object[]> Create(AssetBundleRequest asyncOperation, PlayerLoopTiming timing, IProgress<float> progress, CancellationToken cancellationToken, out short token)
- {
- if (cancellationToken.IsCancellationRequested)
- {
- return AutoResetUniTaskCompletionSource<UnityEngine.Object[]>.CreateFromCanceled(cancellationToken, out token);
- }
- if (!pool.TryPop(out var result))
- {
- result = new AssetBundleRequestAllAssetsConfiguredSource();
- }
- result.asyncOperation = asyncOperation;
- result.progress = progress;
- result.cancellationToken = cancellationToken;
- TaskTracker.TrackActiveTask(result, 3);
- PlayerLoopHelper.AddAction(timing, result);
- token = result.core.Version;
- return result;
- }
- public UnityEngine.Object[] GetResult(short token)
- {
- try
- {
- return core.GetResult(token);
- }
- finally
- {
- TryReturn();
- }
- }
- void IUniTaskSource.GetResult(short token)
- {
- GetResult(token);
- }
- public UniTaskStatus GetStatus(short token)
- {
- return core.GetStatus(token);
- }
- public UniTaskStatus UnsafeGetStatus()
- {
- return core.UnsafeGetStatus();
- }
- public void OnCompleted(Action<object> continuation, object state, short token)
- {
- core.OnCompleted(continuation, state, token);
- }
- public bool MoveNext()
- {
- if (cancellationToken.IsCancellationRequested)
- {
- core.TrySetCanceled(cancellationToken);
- return false;
- }
- if (progress != null)
- {
- progress.Report(asyncOperation.progress);
- }
- if (asyncOperation.isDone)
- {
- core.TrySetResult(asyncOperation.allAssets);
- return false;
- }
- return true;
- }
- bool TryReturn()
- {
- TaskTracker.RemoveTracking(this);
- core.Reset();
- asyncOperation = default;
- progress = default;
- cancellationToken = default;
- return pool.TryPush(this);
- }
- }
- }
- }
- #endif
- #endif
|