| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132 |
- #pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
- using Cysharp.Threading.Tasks.Internal;
- using System;
- using System.Collections;
- using System.Runtime.CompilerServices;
- using System.Threading;
- using UnityEngine;
- namespace Cysharp.Threading.Tasks
- {
- public enum DelayType
- {
- /// <summary>use Time.deltaTime.</summary>
- DeltaTime,
- /// <summary>Ignore timescale, use Time.unscaledDeltaTime.</summary>
- UnscaledDeltaTime,
- /// <summary>use Stopwatch.GetTimestamp().</summary>
- Realtime
- }
- public partial struct UniTask
- {
- public static YieldAwaitable Yield()
- {
- // optimized for single continuation
- return new YieldAwaitable(PlayerLoopTiming.Update);
- }
- public static YieldAwaitable Yield(PlayerLoopTiming timing)
- {
- // optimized for single continuation
- return new YieldAwaitable(timing);
- }
- public static UniTask Yield(CancellationToken cancellationToken, bool cancelImmediately = false)
- {
- return new UniTask(YieldPromise.Create(PlayerLoopTiming.Update, cancellationToken, cancelImmediately, out var token), token);
- }
- public static UniTask Yield(PlayerLoopTiming timing, CancellationToken cancellationToken, bool cancelImmediately = false)
- {
- return new UniTask(YieldPromise.Create(timing, cancellationToken, cancelImmediately, out var token), token);
- }
- /// <summary>
- /// Similar as UniTask.Yield but guaranteed run on next frame.
- /// </summary>
- public static UniTask NextFrame()
- {
- return new UniTask(NextFramePromise.Create(PlayerLoopTiming.Update, CancellationToken.None, false, out var token), token);
- }
- /// <summary>
- /// Similar as UniTask.Yield but guaranteed run on next frame.
- /// </summary>
- public static UniTask NextFrame(PlayerLoopTiming timing)
- {
- return new UniTask(NextFramePromise.Create(timing, CancellationToken.None, false, out var token), token);
- }
- /// <summary>
- /// Similar as UniTask.Yield but guaranteed run on next frame.
- /// </summary>
- public static UniTask NextFrame(CancellationToken cancellationToken, bool cancelImmediately = false)
- {
- return new UniTask(NextFramePromise.Create(PlayerLoopTiming.Update, cancellationToken, cancelImmediately, out var token), token);
- }
- /// <summary>
- /// Similar as UniTask.Yield but guaranteed run on next frame.
- /// </summary>
- public static UniTask NextFrame(PlayerLoopTiming timing, CancellationToken cancellationToken, bool cancelImmediately = false)
- {
- return new UniTask(NextFramePromise.Create(timing, cancellationToken, cancelImmediately, out var token), token);
- }
- #if UNITY_2023_1_OR_NEWER
- public static async UniTask WaitForEndOfFrame(CancellationToken cancellationToken = default)
- {
- await Awaitable.EndOfFrameAsync(cancellationToken);
- }
- #else
- [Obsolete("Use WaitForEndOfFrame(MonoBehaviour) instead or UniTask.Yield(PlayerLoopTiming.LastPostLateUpdate). Equivalent for coroutine's WaitForEndOfFrame requires MonoBehaviour(runner of Coroutine).")]
- public static YieldAwaitable WaitForEndOfFrame()
- {
- return UniTask.Yield(PlayerLoopTiming.LastPostLateUpdate);
- }
- [Obsolete("Use WaitForEndOfFrame(MonoBehaviour) instead or UniTask.Yield(PlayerLoopTiming.LastPostLateUpdate). Equivalent for coroutine's WaitForEndOfFrame requires MonoBehaviour(runner of Coroutine).")]
- public static UniTask WaitForEndOfFrame(CancellationToken cancellationToken, bool cancelImmediately = false)
- {
- return UniTask.Yield(PlayerLoopTiming.LastPostLateUpdate, cancellationToken, cancelImmediately);
- }
- #endif
- public static UniTask WaitForEndOfFrame(MonoBehaviour coroutineRunner)
- {
- var source = WaitForEndOfFramePromise.Create(coroutineRunner, CancellationToken.None, false, out var token);
- return new UniTask(source, token);
- }
- public static UniTask WaitForEndOfFrame(MonoBehaviour coroutineRunner, CancellationToken cancellationToken, bool cancelImmediately = false)
- {
- var source = WaitForEndOfFramePromise.Create(coroutineRunner, cancellationToken, cancelImmediately, out var token);
- return new UniTask(source, token);
- }
- /// <summary>
- /// Same as UniTask.Yield(PlayerLoopTiming.LastFixedUpdate).
- /// </summary>
- public static YieldAwaitable WaitForFixedUpdate()
- {
- // use LastFixedUpdate instead of FixedUpdate
- // https://github.com/Cysharp/UniTask/issues/377
- return UniTask.Yield(PlayerLoopTiming.LastFixedUpdate);
- }
- /// <summary>
- /// Same as UniTask.Yield(PlayerLoopTiming.LastFixedUpdate, cancellationToken).
- /// </summary>
- public static UniTask WaitForFixedUpdate(CancellationToken cancellationToken, bool cancelImmediately = false)
- {
- return UniTask.Yield(PlayerLoopTiming.LastFixedUpdate, cancellationToken, cancelImmediately);
- }
- public static UniTask WaitForSeconds(float duration, bool ignoreTimeScale = false, PlayerLoopTiming delayTiming = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken), bool cancelImmediately = false)
- {
- return Delay(Mathf.RoundToInt(1000 * duration), ignoreTimeScale, delayTiming, cancellationToken, cancelImmediately);
- }
- public static UniTask WaitForSeconds(int duration, bool ignoreTimeScale = false, PlayerLoopTiming delayTiming = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken), bool cancelImmediately = false)
- {
- return Delay(1000 * duration, ignoreTimeScale, delayTiming, cancellationToken, cancelImmediately);
- }
- public static UniTask DelayFrame(int delayFrameCount, PlayerLoopTiming delayTiming = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken), bool cancelImmediately = false)
- {
- if (delayFrameCount < 0)
- {
- throw new ArgumentOutOfRangeException("Delay does not allow minus delayFrameCount. delayFrameCount:" + delayFrameCount);
- }
- return new UniTask(DelayFramePromise.Create(delayFrameCount, delayTiming, cancellationToken, cancelImmediately, out var token), token);
- }
- public static UniTask Delay(int millisecondsDelay, bool ignoreTimeScale = false, PlayerLoopTiming delayTiming = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken), bool cancelImmediately = false)
- {
- var delayTimeSpan = TimeSpan.FromMilliseconds(millisecondsDelay);
- return Delay(delayTimeSpan, ignoreTimeScale, delayTiming, cancellationToken, cancelImmediately);
- }
- public static UniTask Delay(TimeSpan delayTimeSpan, bool ignoreTimeScale = false, PlayerLoopTiming delayTiming = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken), bool cancelImmediately = false)
- {
- var delayType = ignoreTimeScale ? DelayType.UnscaledDeltaTime : DelayType.DeltaTime;
- return Delay(delayTimeSpan, delayType, delayTiming, cancellationToken, cancelImmediately);
- }
- public static UniTask Delay(int millisecondsDelay, DelayType delayType, PlayerLoopTiming delayTiming = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken), bool cancelImmediately = false)
- {
- var delayTimeSpan = TimeSpan.FromMilliseconds(millisecondsDelay);
- return Delay(delayTimeSpan, delayType, delayTiming, cancellationToken, cancelImmediately);
- }
- public static UniTask Delay(TimeSpan delayTimeSpan, DelayType delayType, PlayerLoopTiming delayTiming = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken), bool cancelImmediately = false)
- {
- if (delayTimeSpan < TimeSpan.Zero)
- {
- throw new ArgumentOutOfRangeException("Delay does not allow minus delayTimeSpan. delayTimeSpan:" + delayTimeSpan);
- }
- #if UNITY_EDITOR
- // force use Realtime.
- if (PlayerLoopHelper.IsMainThread && !UnityEditor.EditorApplication.isPlaying)
- {
- delayType = DelayType.Realtime;
- }
- #endif
- switch (delayType)
- {
- case DelayType.UnscaledDeltaTime:
- {
- return new UniTask(DelayIgnoreTimeScalePromise.Create(delayTimeSpan, delayTiming, cancellationToken, cancelImmediately, out var token), token);
- }
- case DelayType.Realtime:
- {
- return new UniTask(DelayRealtimePromise.Create(delayTimeSpan, delayTiming, cancellationToken, cancelImmediately, out var token), token);
- }
- case DelayType.DeltaTime:
- default:
- {
- return new UniTask(DelayPromise.Create(delayTimeSpan, delayTiming, cancellationToken, cancelImmediately, out var token), token);
- }
- }
- }
- sealed class YieldPromise : IUniTaskSource, IPlayerLoopItem, ITaskPoolNode<YieldPromise>
- {
- static TaskPool<YieldPromise> pool;
- YieldPromise nextNode;
- public ref YieldPromise NextNode => ref nextNode;
- static YieldPromise()
- {
- TaskPool.RegisterSizeGetter(typeof(YieldPromise), () => pool.Size);
- }
- CancellationToken cancellationToken;
- CancellationTokenRegistration cancellationTokenRegistration;
- bool cancelImmediately;
- UniTaskCompletionSourceCore<object> core;
- YieldPromise()
- {
- }
- public static IUniTaskSource Create(PlayerLoopTiming timing, CancellationToken cancellationToken, bool cancelImmediately, out short token)
- {
- if (cancellationToken.IsCancellationRequested)
- {
- return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token);
- }
- if (!pool.TryPop(out var result))
- {
- result = new YieldPromise();
- }
- result.cancellationToken = cancellationToken;
- result.cancelImmediately = cancelImmediately;
-
- if (cancelImmediately && cancellationToken.CanBeCanceled)
- {
- result.cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state =>
- {
- var promise = (YieldPromise)state;
- promise.core.TrySetCanceled(promise.cancellationToken);
- }, result);
- }
- TaskTracker.TrackActiveTask(result, 3);
- PlayerLoopHelper.AddAction(timing, result);
- token = result.core.Version;
- return result;
- }
- public void GetResult(short token)
- {
- try
- {
- core.GetResult(token);
- }
- finally
- {
- if (!(cancelImmediately && cancellationToken.IsCancellationRequested))
- {
- TryReturn();
- }
- else
- {
- TaskTracker.RemoveTracking(this);
- }
- }
- }
- 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;
- }
- core.TrySetResult(null);
- return false;
- }
- bool TryReturn()
- {
- TaskTracker.RemoveTracking(this);
- core.Reset();
- cancellationToken = default;
- cancellationTokenRegistration.Dispose();
- cancelImmediately = default;
- return pool.TryPush(this);
- }
- }
- sealed class NextFramePromise : IUniTaskSource, IPlayerLoopItem, ITaskPoolNode<NextFramePromise>
- {
- static TaskPool<NextFramePromise> pool;
- NextFramePromise nextNode;
- public ref NextFramePromise NextNode => ref nextNode;
- static NextFramePromise()
- {
- TaskPool.RegisterSizeGetter(typeof(NextFramePromise), () => pool.Size);
- }
- int frameCount;
- UniTaskCompletionSourceCore<AsyncUnit> core;
- CancellationToken cancellationToken;
- CancellationTokenRegistration cancellationTokenRegistration;
- bool cancelImmediately;
- NextFramePromise()
- {
- }
- public static IUniTaskSource Create(PlayerLoopTiming timing, CancellationToken cancellationToken, bool cancelImmediately, out short token)
- {
- if (cancellationToken.IsCancellationRequested)
- {
- return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token);
- }
- if (!pool.TryPop(out var result))
- {
- result = new NextFramePromise();
- }
- result.frameCount = PlayerLoopHelper.IsMainThread ? Time.frameCount : -1;
- result.cancellationToken = cancellationToken;
- result.cancelImmediately = cancelImmediately;
- if (cancelImmediately && cancellationToken.CanBeCanceled)
- {
- result.cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state =>
- {
- var promise = (NextFramePromise)state;
- promise.core.TrySetCanceled(promise.cancellationToken);
- }, result);
- }
- TaskTracker.TrackActiveTask(result, 3);
- PlayerLoopHelper.AddAction(timing, result);
- token = result.core.Version;
- return result;
- }
- public void GetResult(short token)
- {
- try
- {
- core.GetResult(token);
- }
- finally
- {
- if (!(cancelImmediately && cancellationToken.IsCancellationRequested))
- {
- TryReturn();
- }
- else
- {
- TaskTracker.RemoveTracking(this);
- }
- }
- }
- 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 (frameCount == Time.frameCount)
- {
- return true;
- }
- core.TrySetResult(AsyncUnit.Default);
- return false;
- }
- bool TryReturn()
- {
- TaskTracker.RemoveTracking(this);
- core.Reset();
- cancellationToken = default;
- cancellationTokenRegistration.Dispose();
- return pool.TryPush(this);
- }
- }
- sealed class WaitForEndOfFramePromise : IUniTaskSource, ITaskPoolNode<WaitForEndOfFramePromise>, System.Collections.IEnumerator
- {
- static TaskPool<WaitForEndOfFramePromise> pool;
- WaitForEndOfFramePromise nextNode;
- public ref WaitForEndOfFramePromise NextNode => ref nextNode;
- static WaitForEndOfFramePromise()
- {
- TaskPool.RegisterSizeGetter(typeof(WaitForEndOfFramePromise), () => pool.Size);
- }
- UniTaskCompletionSourceCore<object> core;
- CancellationToken cancellationToken;
- CancellationTokenRegistration cancellationTokenRegistration;
- bool cancelImmediately;
- WaitForEndOfFramePromise()
- {
- }
- public static IUniTaskSource Create(MonoBehaviour coroutineRunner, CancellationToken cancellationToken, bool cancelImmediately, out short token)
- {
- if (cancellationToken.IsCancellationRequested)
- {
- return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token);
- }
- if (!pool.TryPop(out var result))
- {
- result = new WaitForEndOfFramePromise();
- }
- result.cancellationToken = cancellationToken;
- result.cancelImmediately = cancelImmediately;
- if (cancelImmediately && cancellationToken.CanBeCanceled)
- {
- result.cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state =>
- {
- var promise = (WaitForEndOfFramePromise)state;
- promise.core.TrySetCanceled(promise.cancellationToken);
- }, result);
- }
- TaskTracker.TrackActiveTask(result, 3);
- coroutineRunner.StartCoroutine(result);
- token = result.core.Version;
- return result;
- }
- public void GetResult(short token)
- {
- try
- {
- core.GetResult(token);
- }
- finally
- {
- if (!(cancelImmediately && cancellationToken.IsCancellationRequested))
- {
- TryReturn();
- }
- else
- {
- TaskTracker.RemoveTracking(this);
- }
- }
- }
- 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);
- }
- bool TryReturn()
- {
- TaskTracker.RemoveTracking(this);
- core.Reset();
- Reset(); // Reset Enumerator
- cancellationToken = default;
- cancellationTokenRegistration.Dispose();
- return pool.TryPush(this);
- }
- // Coroutine Runner implementation
- static readonly WaitForEndOfFrame waitForEndOfFrameYieldInstruction = new WaitForEndOfFrame();
- bool isFirst = true;
- object IEnumerator.Current => waitForEndOfFrameYieldInstruction;
- bool IEnumerator.MoveNext()
- {
- if (isFirst)
- {
- isFirst = false;
- return true; // start WaitForEndOfFrame
- }
- if (cancellationToken.IsCancellationRequested)
- {
- core.TrySetCanceled(cancellationToken);
- return false;
- }
- core.TrySetResult(null);
- return false;
- }
- public void Reset()
- {
- isFirst = true;
- }
- }
- sealed class DelayFramePromise : IUniTaskSource, IPlayerLoopItem, ITaskPoolNode<DelayFramePromise>
- {
- static TaskPool<DelayFramePromise> pool;
- DelayFramePromise nextNode;
- public ref DelayFramePromise NextNode => ref nextNode;
- static DelayFramePromise()
- {
- TaskPool.RegisterSizeGetter(typeof(DelayFramePromise), () => pool.Size);
- }
- int initialFrame;
- int delayFrameCount;
- CancellationToken cancellationToken;
- CancellationTokenRegistration cancellationTokenRegistration;
- bool cancelImmediately;
- int currentFrameCount;
- UniTaskCompletionSourceCore<AsyncUnit> core;
- DelayFramePromise()
- {
- }
- public static IUniTaskSource Create(int delayFrameCount, PlayerLoopTiming timing, CancellationToken cancellationToken, bool cancelImmediately, out short token)
- {
- if (cancellationToken.IsCancellationRequested)
- {
- return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token);
- }
- if (!pool.TryPop(out var result))
- {
- result = new DelayFramePromise();
- }
- result.delayFrameCount = delayFrameCount;
- result.cancellationToken = cancellationToken;
- result.initialFrame = PlayerLoopHelper.IsMainThread ? Time.frameCount : -1;
- result.cancelImmediately = cancelImmediately;
- if (cancelImmediately && cancellationToken.CanBeCanceled)
- {
- result.cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state =>
- {
- var promise = (DelayFramePromise)state;
- promise.core.TrySetCanceled(promise.cancellationToken);
- }, result);
- }
- TaskTracker.TrackActiveTask(result, 3);
- PlayerLoopHelper.AddAction(timing, result);
- token = result.core.Version;
- return result;
- }
- public void GetResult(short token)
- {
- try
- {
- core.GetResult(token);
- }
- finally
- {
- if (!(cancelImmediately && cancellationToken.IsCancellationRequested))
- {
- TryReturn();
- }
- else
- {
- TaskTracker.RemoveTracking(this);
- }
- }
- }
- 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 (currentFrameCount == 0)
- {
- if (delayFrameCount == 0) // same as Yield
- {
- core.TrySetResult(AsyncUnit.Default);
- return false;
- }
- // skip in initial frame.
- if (initialFrame == Time.frameCount)
- {
- #if UNITY_EDITOR
- // force use Realtime.
- if (PlayerLoopHelper.IsMainThread && !UnityEditor.EditorApplication.isPlaying)
- {
- //goto ++currentFrameCount
- }
- else
- {
- return true;
- }
- #else
- return true;
- #endif
- }
- }
- if (++currentFrameCount >= delayFrameCount)
- {
- core.TrySetResult(AsyncUnit.Default);
- return false;
- }
- return true;
- }
- bool TryReturn()
- {
- TaskTracker.RemoveTracking(this);
- core.Reset();
- currentFrameCount = default;
- delayFrameCount = default;
- cancellationToken = default;
- cancellationTokenRegistration.Dispose();
- cancelImmediately = default;
- return pool.TryPush(this);
- }
- }
- sealed class DelayPromise : IUniTaskSource, IPlayerLoopItem, ITaskPoolNode<DelayPromise>
- {
- static TaskPool<DelayPromise> pool;
- DelayPromise nextNode;
- public ref DelayPromise NextNode => ref nextNode;
- static DelayPromise()
- {
- TaskPool.RegisterSizeGetter(typeof(DelayPromise), () => pool.Size);
- }
- int initialFrame;
- float delayTimeSpan;
- float elapsed;
- CancellationToken cancellationToken;
- CancellationTokenRegistration cancellationTokenRegistration;
- bool cancelImmediately;
- UniTaskCompletionSourceCore<object> core;
- DelayPromise()
- {
- }
- public static IUniTaskSource Create(TimeSpan delayTimeSpan, PlayerLoopTiming timing, CancellationToken cancellationToken, bool cancelImmediately, out short token)
- {
- if (cancellationToken.IsCancellationRequested)
- {
- return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token);
- }
- if (!pool.TryPop(out var result))
- {
- result = new DelayPromise();
- }
- result.elapsed = 0.0f;
- result.delayTimeSpan = (float)delayTimeSpan.TotalSeconds;
- result.cancellationToken = cancellationToken;
- result.initialFrame = PlayerLoopHelper.IsMainThread ? Time.frameCount : -1;
- result.cancelImmediately = cancelImmediately;
- if (cancelImmediately && cancellationToken.CanBeCanceled)
- {
- result.cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state =>
- {
- var promise = (DelayPromise)state;
- promise.core.TrySetCanceled(promise.cancellationToken);
- }, result);
- }
- TaskTracker.TrackActiveTask(result, 3);
- PlayerLoopHelper.AddAction(timing, result);
- token = result.core.Version;
- return result;
- }
- public void GetResult(short token)
- {
- try
- {
- core.GetResult(token);
- }
- finally
- {
- if (!(cancelImmediately && cancellationToken.IsCancellationRequested))
- {
- TryReturn();
- }
- else
- {
- TaskTracker.RemoveTracking(this);
- }
- }
- }
- 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 (elapsed == 0.0f)
- {
- if (initialFrame == Time.frameCount)
- {
- return true;
- }
- }
- elapsed += Time.deltaTime;
- if (elapsed >= delayTimeSpan)
- {
- core.TrySetResult(null);
- return false;
- }
- return true;
- }
- bool TryReturn()
- {
- TaskTracker.RemoveTracking(this);
- core.Reset();
- delayTimeSpan = default;
- elapsed = default;
- cancellationToken = default;
- cancellationTokenRegistration.Dispose();
- cancelImmediately = default;
- return pool.TryPush(this);
- }
- }
- sealed class DelayIgnoreTimeScalePromise : IUniTaskSource, IPlayerLoopItem, ITaskPoolNode<DelayIgnoreTimeScalePromise>
- {
- static TaskPool<DelayIgnoreTimeScalePromise> pool;
- DelayIgnoreTimeScalePromise nextNode;
- public ref DelayIgnoreTimeScalePromise NextNode => ref nextNode;
- static DelayIgnoreTimeScalePromise()
- {
- TaskPool.RegisterSizeGetter(typeof(DelayIgnoreTimeScalePromise), () => pool.Size);
- }
- float delayFrameTimeSpan;
- float elapsed;
- int initialFrame;
- CancellationToken cancellationToken;
- CancellationTokenRegistration cancellationTokenRegistration;
- bool cancelImmediately;
- UniTaskCompletionSourceCore<object> core;
- DelayIgnoreTimeScalePromise()
- {
- }
- public static IUniTaskSource Create(TimeSpan delayFrameTimeSpan, PlayerLoopTiming timing, CancellationToken cancellationToken, bool cancelImmediately, out short token)
- {
- if (cancellationToken.IsCancellationRequested)
- {
- return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token);
- }
- if (!pool.TryPop(out var result))
- {
- result = new DelayIgnoreTimeScalePromise();
- }
- result.elapsed = 0.0f;
- result.delayFrameTimeSpan = (float)delayFrameTimeSpan.TotalSeconds;
- result.initialFrame = PlayerLoopHelper.IsMainThread ? Time.frameCount : -1;
- result.cancellationToken = cancellationToken;
- result.cancelImmediately = cancelImmediately;
- if (cancelImmediately && cancellationToken.CanBeCanceled)
- {
- result.cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state =>
- {
- var promise = (DelayIgnoreTimeScalePromise)state;
- promise.core.TrySetCanceled(promise.cancellationToken);
- }, result);
- }
- TaskTracker.TrackActiveTask(result, 3);
- PlayerLoopHelper.AddAction(timing, result);
- token = result.core.Version;
- return result;
- }
- public void GetResult(short token)
- {
- try
- {
- core.GetResult(token);
- }
- finally
- {
- if (!(cancelImmediately && cancellationToken.IsCancellationRequested))
- {
- TryReturn();
- }
- else
- {
- TaskTracker.RemoveTracking(this);
- }
- }
- }
- 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 (elapsed == 0.0f)
- {
- if (initialFrame == Time.frameCount)
- {
- return true;
- }
- }
- elapsed += Time.unscaledDeltaTime;
- if (elapsed >= delayFrameTimeSpan)
- {
- core.TrySetResult(null);
- return false;
- }
- return true;
- }
- bool TryReturn()
- {
- TaskTracker.RemoveTracking(this);
- core.Reset();
- delayFrameTimeSpan = default;
- elapsed = default;
- cancellationToken = default;
- cancellationTokenRegistration.Dispose();
- cancelImmediately = default;
- return pool.TryPush(this);
- }
- }
- sealed class DelayRealtimePromise : IUniTaskSource, IPlayerLoopItem, ITaskPoolNode<DelayRealtimePromise>
- {
- static TaskPool<DelayRealtimePromise> pool;
- DelayRealtimePromise nextNode;
- public ref DelayRealtimePromise NextNode => ref nextNode;
- static DelayRealtimePromise()
- {
- TaskPool.RegisterSizeGetter(typeof(DelayRealtimePromise), () => pool.Size);
- }
- long delayTimeSpanTicks;
- ValueStopwatch stopwatch;
- CancellationToken cancellationToken;
- CancellationTokenRegistration cancellationTokenRegistration;
- bool cancelImmediately;
- UniTaskCompletionSourceCore<AsyncUnit> core;
- DelayRealtimePromise()
- {
- }
- public static IUniTaskSource Create(TimeSpan delayTimeSpan, PlayerLoopTiming timing, CancellationToken cancellationToken, bool cancelImmediately, out short token)
- {
- if (cancellationToken.IsCancellationRequested)
- {
- return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token);
- }
- if (!pool.TryPop(out var result))
- {
- result = new DelayRealtimePromise();
- }
- result.stopwatch = ValueStopwatch.StartNew();
- result.delayTimeSpanTicks = delayTimeSpan.Ticks;
- result.cancellationToken = cancellationToken;
- result.cancelImmediately = cancelImmediately;
- if (cancelImmediately && cancellationToken.CanBeCanceled)
- {
- result.cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state =>
- {
- var promise = (DelayRealtimePromise)state;
- promise.core.TrySetCanceled(promise.cancellationToken);
- }, result);
- }
- TaskTracker.TrackActiveTask(result, 3);
- PlayerLoopHelper.AddAction(timing, result);
- token = result.core.Version;
- return result;
- }
- public void GetResult(short token)
- {
- try
- {
- core.GetResult(token);
- }
- finally
- {
- if (!(cancelImmediately && cancellationToken.IsCancellationRequested))
- {
- TryReturn();
- }
- else
- {
- TaskTracker.RemoveTracking(this);
- }
- }
- }
- 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 (stopwatch.IsInvalid)
- {
- core.TrySetResult(AsyncUnit.Default);
- return false;
- }
- if (stopwatch.ElapsedTicks >= delayTimeSpanTicks)
- {
- core.TrySetResult(AsyncUnit.Default);
- return false;
- }
- return true;
- }
- bool TryReturn()
- {
- TaskTracker.RemoveTracking(this);
- core.Reset();
- stopwatch = default;
- cancellationToken = default;
- cancellationTokenRegistration.Dispose();
- cancelImmediately = default;
- return pool.TryPush(this);
- }
- }
- }
- public readonly struct YieldAwaitable
- {
- readonly PlayerLoopTiming timing;
- public YieldAwaitable(PlayerLoopTiming timing)
- {
- this.timing = timing;
- }
- public Awaiter GetAwaiter()
- {
- return new Awaiter(timing);
- }
- public UniTask ToUniTask()
- {
- return UniTask.Yield(timing, CancellationToken.None);
- }
- public readonly struct Awaiter : ICriticalNotifyCompletion
- {
- readonly PlayerLoopTiming timing;
- public Awaiter(PlayerLoopTiming timing)
- {
- this.timing = timing;
- }
- public bool IsCompleted => false;
- public void GetResult() { }
- public void OnCompleted(Action continuation)
- {
- PlayerLoopHelper.AddContinuation(timing, continuation);
- }
- public void UnsafeOnCompleted(Action continuation)
- {
- PlayerLoopHelper.AddContinuation(timing, continuation);
- }
- }
- }
- }
|