Last active
March 3, 2020 16:43
-
-
Save baobao/67b2ddaaa94d3eb74c424656ac74a091 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Runtime.CompilerServices; | |
using System.Threading; | |
using DG.Tweening; | |
using UnityEngine; | |
/// <summary> | |
/// DOTweenをasync/awaitに返還すDOTweenAwaiterの拡張メソッド | |
/// </summary> | |
public static class DOTweenAwaiterExtension | |
{ | |
public static DOTweenAwaiter ToAwaiter(this Tween tween, | |
CancellationToken cancellationToken = default, | |
TweenCancelBehaviour behaviour = TweenCancelBehaviour.Kill) | |
{ | |
return new DOTweenAwaiter(tween, cancellationToken, behaviour); | |
} | |
} | |
/// <summary> | |
/// DOTweenをawaiterに変換する | |
/// </summary> | |
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(() => | |
{ | |
// tokenが発火したらタイプをチェックしてTweenの終了振る舞いを変更する | |
switch (tmpThis._behaviour) | |
{ | |
case TweenCancelBehaviour.Kill: | |
tween.Kill(); | |
break; | |
case TweenCancelBehaviour.KillWithCompleteCallback: | |
tween.Kill(true); | |
break; | |
case TweenCancelBehaviour.Complete: | |
tween.Complete(); | |
break; | |
} | |
}); | |
_tween.OnKill(() => | |
{ | |
// CancellationTokenRegistrationを破棄する | |
regist.Dispose(); | |
// 続きを実行 | |
continuation(); | |
}); | |
} | |
public DOTweenAwaiter GetAwaiter() => this; | |
} | |
/// <summary> | |
/// Tweenキャンセル時の振る舞い | |
/// </summary> | |
public enum TweenCancelBehaviour | |
{ | |
Kill, | |
KillWithCompleteCallback, | |
Complete, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment