2019-05-19 23:14:47 +08:00
|
|
|
|
#if CSHARP_7_OR_LATER || (UNITY_2018_3_OR_NEWER && (NET_STANDARD_2_0 || NET_4_6))
|
|
|
|
|
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Runtime.CompilerServices;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using UniRx.Async.Internal;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
namespace UniRx.Async
|
|
|
|
|
{
|
2020-04-19 02:24:53 +08:00
|
|
|
|
public partial struct UniTask
|
2020-04-19 01:55:44 +08:00
|
|
|
|
{
|
2020-04-19 02:24:53 +08:00
|
|
|
|
public static YieldAwaitable Yield(PlayerLoopTiming timing = PlayerLoopTiming.Update)
|
2020-04-19 01:55:44 +08:00
|
|
|
|
{
|
|
|
|
|
// optimized for single continuation
|
2020-04-19 02:24:53 +08:00
|
|
|
|
return new YieldAwaitable(timing);
|
2020-04-19 01:55:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-19 02:24:53 +08:00
|
|
|
|
public static UniTask Yield(PlayerLoopTiming timing, CancellationToken cancellationToken)
|
2020-04-19 01:55:44 +08:00
|
|
|
|
{
|
2020-04-19 02:24:53 +08:00
|
|
|
|
return new UniTask(YieldPromise.Create(timing, cancellationToken, out var token), token);
|
2020-04-19 01:55:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-19 02:24:53 +08:00
|
|
|
|
public static UniTask DelayFrame(int delayFrameCount, PlayerLoopTiming delayTiming = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken))
|
2020-04-19 01:55:44 +08:00
|
|
|
|
{
|
|
|
|
|
if (delayFrameCount < 0)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentOutOfRangeException("Delay does not allow minus delayFrameCount. delayFrameCount:" + delayFrameCount);
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-19 02:24:53 +08:00
|
|
|
|
return new UniTask(DelayFramePromise.Create(delayFrameCount, delayTiming, cancellationToken, out var token), token);
|
2020-04-19 01:55:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-19 02:24:53 +08:00
|
|
|
|
public static UniTask Delay(int millisecondsDelay, bool ignoreTimeScale = false, PlayerLoopTiming delayTiming = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken))
|
2020-04-19 01:55:44 +08:00
|
|
|
|
{
|
|
|
|
|
var delayTimeSpan = TimeSpan.FromMilliseconds(millisecondsDelay);
|
|
|
|
|
if (delayTimeSpan < TimeSpan.Zero)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentOutOfRangeException("Delay does not allow minus millisecondsDelay. millisecondsDelay:" + millisecondsDelay);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (ignoreTimeScale)
|
2020-04-19 02:24:53 +08:00
|
|
|
|
? new UniTask(DelayIgnoreTimeScalePromise.Create(delayTimeSpan, delayTiming, cancellationToken, out var token), token)
|
|
|
|
|
: new UniTask(DelayPromise.Create(delayTimeSpan, delayTiming, cancellationToken, out token), token);
|
2020-04-19 01:55:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-19 02:24:53 +08:00
|
|
|
|
public static UniTask Delay(TimeSpan delayTimeSpan, bool ignoreTimeScale = false, PlayerLoopTiming delayTiming = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken))
|
2020-04-19 01:55:44 +08:00
|
|
|
|
{
|
|
|
|
|
if (delayTimeSpan < TimeSpan.Zero)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentOutOfRangeException("Delay does not allow minus delayTimeSpan. delayTimeSpan:" + delayTimeSpan);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (ignoreTimeScale)
|
2020-04-19 02:24:53 +08:00
|
|
|
|
? new UniTask(DelayIgnoreTimeScalePromise.Create(delayTimeSpan, delayTiming, cancellationToken, out var token), token)
|
|
|
|
|
: new UniTask(DelayPromise.Create(delayTimeSpan, delayTiming, cancellationToken, out token), token);
|
2020-04-19 01:55:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class YieldPromise : IUniTaskSource, IPlayerLoopItem, IPromisePoolItem
|
|
|
|
|
{
|
|
|
|
|
static readonly PromisePool<YieldPromise> pool = new PromisePool<YieldPromise>();
|
|
|
|
|
|
|
|
|
|
CancellationToken cancellationToken;
|
|
|
|
|
UniTaskCompletionSourceCore<object> core;
|
|
|
|
|
|
|
|
|
|
YieldPromise()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static IUniTaskSource Create(PlayerLoopTiming timing, CancellationToken cancellationToken, out short token)
|
|
|
|
|
{
|
|
|
|
|
if (cancellationToken.IsCancellationRequested)
|
|
|
|
|
{
|
|
|
|
|
return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var result = pool.TryRent() ?? new YieldPromise();
|
|
|
|
|
|
|
|
|
|
result.cancellationToken = cancellationToken;
|
|
|
|
|
|
|
|
|
|
TaskTracker2.TrackActiveTask(result, 3);
|
|
|
|
|
|
|
|
|
|
PlayerLoopHelper.AddAction(timing, result);
|
|
|
|
|
|
|
|
|
|
token = result.core.Version;
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void GetResult(short token)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
TaskTracker2.RemoveTracking(this);
|
|
|
|
|
core.GetResult(token);
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
pool.TryReturn(this);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public AwaiterStatus GetStatus(short token)
|
|
|
|
|
{
|
|
|
|
|
return core.GetStatus(token);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public AwaiterStatus 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.SetCanceled(cancellationToken);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
core.SetResult(null);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Reset()
|
|
|
|
|
{
|
|
|
|
|
core.Reset();
|
|
|
|
|
cancellationToken = default;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class DelayFramePromise : IUniTaskSource, IPlayerLoopItem, IPromisePoolItem
|
|
|
|
|
{
|
|
|
|
|
static readonly PromisePool<DelayFramePromise> pool = new PromisePool<DelayFramePromise>();
|
|
|
|
|
|
|
|
|
|
int delayFrameCount;
|
|
|
|
|
CancellationToken cancellationToken;
|
|
|
|
|
|
|
|
|
|
int currentFrameCount;
|
|
|
|
|
UniTaskCompletionSourceCore<object> core;
|
|
|
|
|
|
|
|
|
|
DelayFramePromise()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static IUniTaskSource Create(int delayFrameCount, PlayerLoopTiming timing, CancellationToken cancellationToken, out short token)
|
|
|
|
|
{
|
|
|
|
|
if (cancellationToken.IsCancellationRequested)
|
|
|
|
|
{
|
|
|
|
|
return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var result = pool.TryRent() ?? new DelayFramePromise();
|
|
|
|
|
|
|
|
|
|
result.delayFrameCount = delayFrameCount;
|
|
|
|
|
result.cancellationToken = cancellationToken;
|
|
|
|
|
|
|
|
|
|
TaskTracker2.TrackActiveTask(result, 3);
|
|
|
|
|
|
|
|
|
|
PlayerLoopHelper.AddAction(timing, result);
|
|
|
|
|
|
|
|
|
|
token = result.core.Version;
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void GetResult(short token)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
TaskTracker2.RemoveTracking(this);
|
|
|
|
|
core.GetResult(token);
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
pool.TryReturn(this);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public AwaiterStatus GetStatus(short token)
|
|
|
|
|
{
|
|
|
|
|
return core.GetStatus(token);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public AwaiterStatus 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.SetCanceled(cancellationToken);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (currentFrameCount == delayFrameCount)
|
|
|
|
|
{
|
|
|
|
|
core.SetResult(null);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
currentFrameCount++;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Reset()
|
|
|
|
|
{
|
|
|
|
|
core.Reset();
|
|
|
|
|
currentFrameCount = default;
|
|
|
|
|
delayFrameCount = default;
|
|
|
|
|
cancellationToken = default;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class DelayPromise : IUniTaskSource, IPlayerLoopItem, IPromisePoolItem
|
|
|
|
|
{
|
|
|
|
|
static readonly PromisePool<DelayPromise> pool = new PromisePool<DelayPromise>();
|
|
|
|
|
|
|
|
|
|
float delayFrameTimeSpan;
|
|
|
|
|
float elapsed;
|
|
|
|
|
CancellationToken cancellationToken;
|
|
|
|
|
|
|
|
|
|
UniTaskCompletionSourceCore<object> core;
|
|
|
|
|
|
|
|
|
|
DelayPromise()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static IUniTaskSource Create(TimeSpan delayFrameTimeSpan, PlayerLoopTiming timing, CancellationToken cancellationToken, out short token)
|
|
|
|
|
{
|
|
|
|
|
if (cancellationToken.IsCancellationRequested)
|
|
|
|
|
{
|
|
|
|
|
return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var result = pool.TryRent() ?? new DelayPromise();
|
|
|
|
|
|
|
|
|
|
result.elapsed = 0.0f;
|
|
|
|
|
result.delayFrameTimeSpan = (float)delayFrameTimeSpan.TotalSeconds;
|
|
|
|
|
result.cancellationToken = cancellationToken;
|
|
|
|
|
|
|
|
|
|
TaskTracker2.TrackActiveTask(result, 3);
|
|
|
|
|
|
|
|
|
|
PlayerLoopHelper.AddAction(timing, result);
|
|
|
|
|
|
|
|
|
|
token = result.core.Version;
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void GetResult(short token)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
TaskTracker2.RemoveTracking(this);
|
|
|
|
|
core.GetResult(token);
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
pool.TryReturn(this);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public AwaiterStatus GetStatus(short token)
|
|
|
|
|
{
|
|
|
|
|
return core.GetStatus(token);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public AwaiterStatus 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.SetCanceled(cancellationToken);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
elapsed += Time.deltaTime;
|
|
|
|
|
if (elapsed >= delayFrameTimeSpan)
|
|
|
|
|
{
|
|
|
|
|
core.SetResult(null);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Reset()
|
|
|
|
|
{
|
|
|
|
|
core.Reset();
|
|
|
|
|
delayFrameTimeSpan = default;
|
|
|
|
|
elapsed = default;
|
|
|
|
|
cancellationToken = default;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class DelayIgnoreTimeScalePromise : IUniTaskSource, IPlayerLoopItem, IPromisePoolItem
|
|
|
|
|
{
|
|
|
|
|
static readonly PromisePool<DelayIgnoreTimeScalePromise> pool = new PromisePool<DelayIgnoreTimeScalePromise>();
|
|
|
|
|
|
|
|
|
|
float delayFrameTimeSpan;
|
|
|
|
|
float elapsed;
|
|
|
|
|
CancellationToken cancellationToken;
|
|
|
|
|
|
|
|
|
|
UniTaskCompletionSourceCore<object> core;
|
|
|
|
|
|
|
|
|
|
DelayIgnoreTimeScalePromise()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static IUniTaskSource Create(TimeSpan delayFrameTimeSpan, PlayerLoopTiming timing, CancellationToken cancellationToken, out short token)
|
|
|
|
|
{
|
|
|
|
|
if (cancellationToken.IsCancellationRequested)
|
|
|
|
|
{
|
|
|
|
|
return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var result = pool.TryRent() ?? new DelayIgnoreTimeScalePromise();
|
|
|
|
|
|
|
|
|
|
result.elapsed = 0.0f;
|
|
|
|
|
result.delayFrameTimeSpan = (float)delayFrameTimeSpan.TotalSeconds;
|
|
|
|
|
result.cancellationToken = cancellationToken;
|
|
|
|
|
|
|
|
|
|
TaskTracker2.TrackActiveTask(result, 3);
|
|
|
|
|
|
|
|
|
|
PlayerLoopHelper.AddAction(timing, result);
|
|
|
|
|
|
|
|
|
|
token = result.core.Version;
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void GetResult(short token)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
TaskTracker2.RemoveTracking(this);
|
|
|
|
|
core.GetResult(token);
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
pool.TryReturn(this);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public AwaiterStatus GetStatus(short token)
|
|
|
|
|
{
|
|
|
|
|
return core.GetStatus(token);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public AwaiterStatus 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.SetCanceled(cancellationToken);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
elapsed += Time.unscaledDeltaTime;
|
|
|
|
|
if (elapsed >= delayFrameTimeSpan)
|
|
|
|
|
{
|
|
|
|
|
core.SetResult(null);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Reset()
|
|
|
|
|
{
|
|
|
|
|
core.Reset();
|
|
|
|
|
delayFrameTimeSpan = default;
|
|
|
|
|
elapsed = default;
|
|
|
|
|
cancellationToken = default;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-19 23:14:47 +08:00
|
|
|
|
public 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 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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|