Callbacks.md

May 23, 2024 · View on GitHub

Home | Life Cycle | Setter | Easing | ▸Callbacks◂ | Playback | Relative Values | Timer, Sequence, and Parallel | Anonymous Playback | Shared Assets | Batch Operations |

Callbacks

C#

Motion objects have the following callbacks:

CallbackTriggered
OnStartJust before the Motion starts.
OnUpdateOn every update, while the Motion is playing.
OnInterruptWhen the Motion is stopped or played before it has completed.
OnCompleteWhen the Motion completes the animation.

An example of how to set an OnComplete callback, lambda style:

MotionKit.GetMotion(m_Ball, "Position", p => m_Ball.localPosition = p)
	.Play(new Vector3(0, 0, 0), new Vector3(3, 0, 0), 2)
	.SetOnComplete(() => Debug.Log("Motion Completed!!!"));

Or this one, with a function:

MotionKit.GetMotion(m_Ball, "Position", p => m_Ball.localPosition = p)
	.Play(new Vector3(0, 0, 0), new Vector3(3, 0, 0), 2)
	.SetOnComplete(OnComplete);

void OnComplete() {
	Debug.Log("Motion Completed!!!");
}

To set the other callbacks you can use these methods: SetOnStart(), SetOnUpdate(), and SetOnInterrupt().

The callbacks can also receive the Motion object as a parameter, for example:

MotionKit.GetMotion(m_Ball, "Position", p => m_Ball.localPosition = p)
	.Play(new Vector3(0, 0, 0), new Vector3(3, 0, 0), 2)
	.SetOnUpdate(m => Debug.Log($"Motion progress: {m.Progress}"));

Or this one, with a function:

MotionKit.GetMotion(m_Ball, "Position", p => m_Ball.localPosition = p)
	.Play(new Vector3(0, 0, 0), new Vector3(3, 0, 0), 2)
	.SetOnUpdate(OnUpdate);

void OnUpdate(Motion3D motion3D) {
	Debug.Log($"Motion progress: {motion3D.Progress}");
}

This parameter comes handy in case you want to read or change any of the Motion's properties while its playing.

Inspector

In the MotionKitComponent, the callbacks can be assigned as Unity events in the bottom part of the inspector:

Note that the callbacks that receive the Motion object as a parameter are not available from the inspector.