DOTween Awaiter

原文

使用

await transform.DOLocalMoveX(3f, 2f).ToAwaiter();

代码

将下面的代码放到项目中即可

public static class DOTweenAwaiterHelper {
    public static DOTweenAwaiter ToAwaiter(this Tween           tween,
                                           CancellationToken    cancellationToken = default,
                                           TweenCancelBehaviour behaviour         = TweenCancelBehaviour.Kill) {
        return new DOTweenAwaiter(tween, cancellationToken, behaviour);
    }
}
public struct DOTweenAwaiter: ICriticalNotifyCompletion {
    private Tween                _tween;
    private CancellationToken    _cancellationToken;
    private TweenCancelBehaviour _behaviour;

    public DOTweenAwaiter(Tween tween, CancellationToken cancellationToken, TweenCancelBehaviour behaviour) {
        _tween             = tween;
        _cancellationToken = cancellationToken;
        _behaviour         = behaviour;
    }

    public bool IsCompleted => _tween.IsPlaying() == false;

    public void GetResult() => _cancellationToken.ThrowIfCancellationRequested();

    public void OnCompleted(Action continuation) => UnsafeOnCompleted(continuation);

    public void UnsafeOnCompleted(Action continuation) {
        DOTweenAwaiter tmpThis = this;
        var            tween   = _tween;
        var regist = tmpThis._cancellationToken.Register(
            () => {
                switch(tmpThis._behaviour){
                    case TweenCancelBehaviour.Kill:
                        tween.Kill();
                        break;
                    case TweenCancelBehaviour.KillWithCompleteCallback:
                        tween.Kill(true);
                        break;
                    case TweenCancelBehaviour.Complete:
                        tween.Complete();
                        break;
                    default: throw new ArgumentOutOfRangeException();
                }
            }
        );

        _tween.OnKill(
            () => {
                regist.Dispose();
                continuation();
            }
        );
    }

    public DOTweenAwaiter GetAwaiter() => this;
}

public enum TweenCancelBehaviour {
    Kill,
    KillWithCompleteCallback,
    Complete
}
作者:L
本文采用 CC BY-NC-SA 4.0 协议
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇