Playback.md
May 23, 2024 · View on GitHub
Playback
C#
Playback Methods
The Motion objects can be stored in variables and be controlled for playback with the methods Play(), Pause(), Resume() and Stop():
Motion3D m_Motion3D;
void Start() {
// Create the motion but don't play it, yet. Note that SetValuesAndDuration is
// used instead of Play()
m_Motion3D = MotionKit.GetMotion(m_Ball, "Position", p => m_Ball.localPosition = p)
.SetValuesAndDuration(new Vector3(0, 0, 0), new Vector3(3, 0, 0), 2);
}
// Invoke the methods below from UI button clicks, for example
public void OnPlayButtonClick() {
m_Motion3D.Play();
}
public void OnPauseButtonClick() {
m_Motion3D.Pause();
}
public void OnResumeButtonClick() {
m_Motion3D.Resume();
}
public void OnStopButtonClick() {
m_Motion3D.Stop();
}
The Progress Property
Motion objects can be controlled via their Progress property. Progress is a number that goes from 0 to 1 and changes the property by interpolating between the initialValue and the finalValue. One example where the Progress property is useful is if you want to create a slider that changes a property of an object by using a Motion, but you don't want to actually play the Motion:
Motion3D m_Motion3D;
void Start() {
// Create the motion but don't play it at all. Note that SetValuesAndDuration is
// used instead of Play()
m_Motion3D = MotionKit.GetMotion(m_Ball, "Position", p => m_Ball.localPosition = p)
.SetEasing(MotionKitEasing.BackOut) // Use easing for a nicer transition, even when using a slider
.SetValuesAndDuration(new Vector3(0, 0, 0), new Vector3(3, 0, 0), 2);
}
public void OnValueChanged(float value) {
// Set the progress when the slider changes.
// The slider will make the animation to go to any part of the animation at any time,
// between the initial and final values.
m_Motion3D.Progress = value;
}
Inspector
The playback of the MotionKitBlocks created in a MotionKitComponent can be controlled independently by their names:
This is an example of how to play each one:
m_MotionKitComponent.Play("Motion3D");
m_MotionKitComponent.Play("MotionFloat");
m_MotionKitComponent.Play("MotionColor");
The same will apply to Pause(), Resume(), Stop().
The names can be customized when you edit each MotionKitBlock.
If you want more control over the actual Motion object that belongs to the wrapper MotionKitBlock you can obtain direct access to it by first getting the MotionKitBlock and then the Motion object:
// Get the MotionKitBlock that contains the `Motion3D`
Motion3DBlock motion3DBlock = m_MotionKitComponent.GetChild("Motion3D") as Motion3DBlock;
// Get the `Motion3D
var motion3D = motion3DBlock.Motion;
// Do anything that you need to do
motion3D.Progress = 0.5f;