Created
November 21, 2024 02:55
-
-
Save pukpukpuk/f77459f099ab3e1d825a30eb8d3f0af6 to your computer and use it in GitHub Desktop.
DOTween Tweens Queue
This file contains hidden or 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
| 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>(); | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage example: