Skip to content

Instantly share code, notes, and snippets.

@pukpukpuk
Created November 21, 2024 02:55
Show Gist options
  • Select an option

  • Save pukpukpuk/f77459f099ab3e1d825a30eb8d3f0af6 to your computer and use it in GitHub Desktop.

Select an option

Save pukpukpuk/f77459f099ab3e1d825a30eb8d3f0af6 to your computer and use it in GitHub Desktop.
DOTween Tweens Queue
public class TweenQueuer : MonoBehaviour
{
private bool isPlaying;
private readonly Queue<Tween> animationQueue = new();
public void EnqueueTween(Tween tween)
{
tween.Pause();
animationQueue.Enqueue(tween);
if (!isPlaying)
{
StartCoroutine(PlayAnimations());
}
}
private IEnumerator PlayAnimations()
{
isPlaying = true;
while (animationQueue.Count > 0)
{
Tween currentAnimation = animationQueue.Dequeue();
currentAnimation.Play();
yield return currentAnimation.WaitForCompletion();
}
isPlaying = false;
}
public void CompleteAllAnimations()
{
StopCoroutine(PlayAnimations());
transform.DOComplete();
while (animationQueue.Count > 0) animationQueue.Dequeue().Complete();
}
private void OnDestroy()
{
StopCoroutine(PlayAnimations());
transform.DOKill();
}
}
public static class TweenExtensions
{
public static void EnqueueTween(this Transform transform, Tween tween)
{
EnqueueTween(transform, new [] {tween});
}
public static void EnqueueTween(this Transform transform, IEnumerable<Tween> tweens)
{
var queuer = transform.GetQueuer();
foreach (var tween in tweens)
{
queuer.EnqueueTween(tween);
}
}
public static TweenQueuer GetQueuer(this Transform transform)
{
var go = transform.gameObject;
return go.GetComponent<TweenQueuer>() ?? go.AddComponent<TweenQueuer>();
}
}
@pukpukpuk
Copy link
Copy Markdown
Author

Usage example:

transform.EnqueueTween(transform.DOMove(Vector3.zero, 1f));
transform.EnqueueTween(transform.DOMove(Vector3.one, 1f));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment