Events

Various events are invoked throughout the tween lifecycle. A TweenCallback function delegate can be used to respond to the following events:

  • onUpdate: Invoked every time the tween is updated
  • onStart: Invoked when the tween is started
  • onStop: Invoked when the tween is stopped
  • onLoop: Invoked when the tween is looped
  • onComplete: Invoked when the tween is completed
  • onKill: Invoked when the tween is killed

🗣️ Assigning callbacks

Tween callbacks can be assigned with delegates or lambdas. They have no parameters or return types. You can also use property chaining to make it easier to set multiple callbacks.

// assigning callbacks with functions
tween.onUpdate += OnTweenUpdated
tween.onStart += OnTweenStarted;
tween.onStop += OnTweenStopped;
tween.onLoop += OnTweenLooped;
tween.onComplete += OnTweenCompleted;
tween.onKill += OnTweenKilled;
// assigning callbacks with lambdas
tween.onUpdate += () => Debug.Log("Updated");
tween.onStart += () => Debug.Log("Started");
tween.onStop += () => Debug.Log("Stopped");
tween.onLoop += () => Debug.Log("Looped");
tween.onComplete += () => Debug.Log("Completed");
tween.onKill += () => Debug.Log("Killed");
// assigning callbacks with property chaining
transform.TweenPosition(Vector3.zero, 1f)
         .OnUpdate(OnTweenUpdated)
         .OnStart(OnTweenStarted)
         .OnStop(OnTweenStopped)
         .OnLoop(OnTweenLooped)
         .OnComplete(OnTweenCompleted)
         .OnKill(OnTweenKilled);

🎫 Event Handler

One drawback of using delegates is that it produces GC allocations. If you are highly concerned about performance, you can use the ITweenEventHandler interface to avoid allocations.

public class Example : MonoBehaviour, ITweenEventHandler
{
    private void Start()
    {
        transform.TweenPosition(Vector3.zero, 1f)
                 .SetEventHandler(this);
    }

    public void OnTweenUpdate(Tween tween)
    {
        Debug.Log("Updated");
    }

    public void OnTweenStart(Tween tween)
    {
        Debug.Log("Started");
    }

    public void OnTweenStop(Tween tween)
    {
        Debug.Log("Stopped");
    }

    public void OnTweenLoop(Tween tween)
    {
        Debug.Log("Looped");
    }

    public void OnTweenComplete(Tween tween)
    {
        Debug.Log("Completed");
    }

    public void OnTweenKill(Tween tween)
    {
        Debug.Log("Killed");
    }
}