|
| 1 | +--- |
| 2 | +slug: "/manual/events" |
| 3 | +--- |
| 4 | + |
| 5 | +# Events |
| 6 | + |
| 7 | +Various events are invoked throughout the tween lifecycle. A [TweenCallback](/api/Zigurous.Tweening/TweenCallback) function delegate can be used to respond to the following events: |
| 8 | + |
| 9 | +- [onUpdate](/api/Zigurous.Tweening/Tween/onUpdate): Invoked every time the tween is updated |
| 10 | +- [onStart](/api/Zigurous.Tweening/Tween/onStart): Invoked when the tween is started |
| 11 | +- [onStop](/api/Zigurous.Tweening/Tween/onStop): Invoked when the tween is stopped |
| 12 | +- [onLoop](/api/Zigurous.Tweening/Tween/onLoop): Invoked when the tween is looped |
| 13 | +- [onComplete](/api/Zigurous.Tweening/Tween/onComplete): Invoked when the tween is completed |
| 14 | +- [onKill](/api/Zigurous.Tweening/Tween/onKill): Invoked when the tween is killed |
| 15 | + |
| 16 | +<hr/> |
| 17 | + |
| 18 | +## 🗣️ Assigning callbacks |
| 19 | + |
| 20 | +Tween callbacks can be assigned with delegates or lambdas. They have no parameters or return types. You can also use [property chaining](/manual/property-chaining) to make it easier to set multiple callbacks. |
| 21 | + |
| 22 | +```csharp |
| 23 | +// assigning callbacks with functions |
| 24 | +tween.onUpdate += OnTweenUpdated |
| 25 | +tween.onStart += OnTweenStarted; |
| 26 | +tween.onStop += OnTweenStopped; |
| 27 | +tween.onLoop += OnTweenLooped; |
| 28 | +tween.onComplete += OnTweenCompleted; |
| 29 | +tween.onKill += OnTweenKilled; |
| 30 | +``` |
| 31 | + |
| 32 | +```csharp |
| 33 | +// assigning callbacks with lambdas |
| 34 | +tween.onUpdate += () => Debug.Log("Updated"); |
| 35 | +tween.onStart += () => Debug.Log("Started"); |
| 36 | +tween.onStop += () => Debug.Log("Stopped"); |
| 37 | +tween.onLoop += () => Debug.Log("Looped"); |
| 38 | +tween.onComplete += () => Debug.Log("Completed"); |
| 39 | +tween.onKill += () => Debug.Log("Killed"); |
| 40 | +``` |
| 41 | + |
| 42 | +```csharp |
| 43 | +// assigning callbacks with property chaining |
| 44 | +transform.TweenPosition(Vector3.zero, 1f) |
| 45 | + .OnUpdate(OnTweenUpdated) |
| 46 | + .OnStart(OnTweenStarted) |
| 47 | + .OnStop(OnTweenStopped) |
| 48 | + .OnLoop(OnTweenLooped) |
| 49 | + .OnComplete(OnTweenCompleted) |
| 50 | + .OnKill(OnTweenKilled); |
| 51 | +``` |
| 52 | + |
| 53 | +## 🎫 Event Handler |
| 54 | + |
| 55 | +One drawback of using delegates is that it produces GC allocations. If you are highly concerned about performance, you can use the [ITweenEventHandler](/api/Zigurous.Tweening/ITweenEventHandler) interface to avoid allocations. |
| 56 | + |
| 57 | +```csharp |
| 58 | +public class Example : MonoBehaviour, ITweenEventHandler |
| 59 | +{ |
| 60 | + private void Start() |
| 61 | + { |
| 62 | + transform.TweenPosition(Vector3.zero, 1f) |
| 63 | + .SetEventHandler(this); |
| 64 | + } |
| 65 | + |
| 66 | + public void OnTweenUpdate(Tween tween) |
| 67 | + { |
| 68 | + Debug.Log("Updated"); |
| 69 | + } |
| 70 | + |
| 71 | + public void OnTweenStart(Tween tween) |
| 72 | + { |
| 73 | + Debug.Log("Started"); |
| 74 | + } |
| 75 | + |
| 76 | + public void OnTweenStop(Tween tween) |
| 77 | + { |
| 78 | + Debug.Log("Stopped"); |
| 79 | + } |
| 80 | + |
| 81 | + public void OnTweenLoop(Tween tween) |
| 82 | + { |
| 83 | + Debug.Log("Looped"); |
| 84 | + } |
| 85 | + |
| 86 | + public void OnTweenComplete(Tween tween) |
| 87 | + { |
| 88 | + Debug.Log("Completed"); |
| 89 | + } |
| 90 | + |
| 91 | + public void OnTweenKill(Tween tween) |
| 92 | + { |
| 93 | + Debug.Log("Killed"); |
| 94 | + } |
| 95 | +} |
0 commit comments