UniTask
UniTask copied to clipboard
DOAnchorPosY returns wrong canceled
When I canceled this , the animation stopped, but log shows "Not Canceled.'
I can confirm these tests fail. While the tasks are indeed cancelled, they neither throw a Cancellation exception nor return the proper bool value when the exception is suppressed.
const float TWEEN_TIME = 2f;
const int DEFAULT_WAIT_MS = 500;
bool _isCanceled;
[UnityTest]
public IEnumerator DoTweenTaskCanceledReturnValueTest() => UniTask.ToCoroutine(async () => {
var cts = new CancellationTokenSource();
DoTweenTask(cts.Token).Forget();
await UniTask.Delay(DEFAULT_WAIT_MS);
cts.Cancel();
await UniTask.Delay(DEFAULT_WAIT_MS);
Assert.That(_isCanceled, Is.True);
});
[UnityTest]
public IEnumerator DoTweenTaskTimeoutReturnValueTest() => UniTask.ToCoroutine(async () => {
using var timeoutController = new TimeoutController();
DoTweenTask(timeoutController.Timeout(DEFAULT_WAIT_MS)).Forget();
await UniTask.Delay(DEFAULT_WAIT_MS + 100);
Assert.That(_isCancelled, Is.True);
});
async UniTaskVoid DoTweenTask(CancellationToken token) {
_isCanceled = false;
var time = Time.time;
var value = 0;
_isCanceled = await DOTween.To(() => value, x => value = x, 100, TWEEN_TIME)
.WithCancellation(token)
.SuppressCancellationThrow();
time = Time.time - time;
Debug.Log(time < TWEEN_TIME
? $"{nameof(DoTweenTask)} is cancelled after {time} seconds"
: $"{nameof(DoTweenTask)} is completed after {time} seconds");
}
@SherryOever18
The behavior when a DOTween is canceled can be optionally adjusted.
By default, no exception is used, but the following will throw an OperationCancellationException while killing the tween.
await rectTransform.DOAnchorPos(1000f * Vector2.right, 10f)
.ToUniTask(TweenCancelBehaviour.KillAndCancelAwait, cancellationToken);