UniTask icon indicating copy to clipboard operation
UniTask copied to clipboard

DOAnchorPosY returns wrong canceled

Open SherryOever18 opened this issue 2 years ago • 1 comments

J))9F @Q{413YB$UD))B8ZW 7_G @R8S2D7~PMLX07XTOLS When I canceled this , the animation stopped, but log shows "Not Canceled.'

SherryOever18 avatar Dec 14 '23 11:12 SherryOever18

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");  
}

Saismirk avatar Dec 16 '23 18:12 Saismirk

@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);

hadashiA avatar Feb 09 '24 07:02 hadashiA