Last active
June 7, 2024 16:55
-
-
Save StefanoFiumara/c52928dcc4d6200e8ede to your computer and use it in GitHub Desktop.
Unity Tween Queue
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
public interface ITweenObject | |
{ | |
float CompletionTime { get; } | |
float TimeUntilNextTween { get; } | |
void ExecuteTween(); | |
} |
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 UnityEngine; | |
class MoveTween : ITweenObject | |
{ | |
private readonly GameObject _target; | |
private readonly Vector3 _position; | |
private readonly float _time; | |
private readonly float _delay; | |
private readonly float _timeUntilNextTween; | |
public MoveTween(GameObject target, Vector3 position, float time, float delay, float timeUntilNextTween = 0f) | |
{ | |
_target = target; | |
_position = position; | |
_time = time; | |
_delay = delay; | |
_timeUntilNextTween = timeUntilNextTween; | |
} | |
public float CompletionTime | |
{ | |
get { return _time + _delay; } | |
} | |
public float TimeUntilNextTween | |
{ | |
get { return _timeUntilNextTween; } | |
} | |
public void ExecuteTween() | |
{ | |
iTween.MoveTo(_target, iTween.Hash( | |
"time", _time, | |
"delay", _delay, | |
"position", _position, | |
"easetype", iTween.EaseType.EaseInOutSine, | |
"islocal", true | |
)); | |
} | |
} | |
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.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class TweenQueue | |
{ | |
private Queue<ITweenObject> _queue; | |
private bool _tweenQueueRunning; | |
private float _timeUntilNextTween; | |
public bool TweenQueueIsEmpty { get; private set; } | |
public TweenQueue() | |
{ | |
_queue = new Queue<ITweenObject>(); | |
_tweenQueueRunning = false; | |
_timeUntilNextTween = 0f; | |
TweenQueueIsEmpty = true; | |
} | |
public void AddTweenToQueue(ITweenObject tweenObject) | |
{ | |
_queue.Enqueue(tweenObject); | |
TweenQueueIsEmpty = false; | |
if (_tweenQueueRunning) return; | |
_tweenQueueRunning = true; | |
StartCoroutine(RunTweenQueue()); | |
} | |
private IEnumerator RunTweenQueue() | |
{ | |
while (true) | |
{ | |
if (_queue.Count == 0) | |
{ | |
TweenQueueIsEmpty = true; | |
yield return null; | |
} | |
else | |
{ | |
yield return new WaitForSeconds(_timeUntilNextTween); | |
var tween = _queue.Dequeue(); | |
tween.ExecuteTween(); | |
_timeUntilNextTween = tween.TimeUntilNextTween; | |
yield return new WaitForSeconds(tween.CompletionTime); | |
} | |
} | |
} | |
public void Reset() | |
{ | |
_queue = new Queue<ITweenObject>(); | |
_tweenQueueRunning = false; | |
TweenQueueIsEmpty = true; | |
StopCoroutine(RunTweenQueue()); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks