LifeCycle.md
May 26, 2024 · View on GitHub
Life Cycle: owner, reuseID, and Clearance
Motion, Timer, Sequence, and Parallel objects (collectibly named as Playbacks) can be reused for optimal results with a very easy to use API.
For example, you can create a motion that animates the position of an object every time the player clicks the mouse button and moves the object to the clicked location. Instead of creating a new Motion every time, you can reuse only one Motion object to carry out all the animations. This has a very convenient side effect: It will stop the current movement and start a new one towards the new destination point automatically. You wouldn't need to stop or destroy the current one. (The other big advantage over DOTween).
This is what owner and reuseID are for. Playbacks are stored internally assotiated with an owner object and an id string when they are created. When invoking MotionKit.GetMotion(someOwner, "someReuseID", ... repeatedly, this line of code will use and return the same Motion object always.
Example of owners and reuse IDs:
C#
owner and reuseID
In the code below, the owner is the m_Ball and the reuseID is "Position". The ball will be animated two times, one after the other. The second animation starts 1.5 seconds after the first one started, so it will interrupt the first one which is set to last for 2 seconds. This happens because the animation is being carried out by the same Motion object, so it just stops and plays with the new settings.
StartCoroutine(SomeCoroutine());
IEnumerator SomeCoroutine() {
// First motion with a duration of 2 seconds
MotionKit.GetMotion(m_Ball, "Position", p => m_Ball.localPosition = p)
.Play(new Vector3(0, 0, 0), new Vector3(3, 0, 0), 2);
yield return new WaitForSeconds(1.5f);
// Second motion (0.5 before the previous one completes)
MotionKit.GetMotion(m_Ball, "Position", p => m_Ball.localPosition = p)
.Play(m_Ball.localPosition, new Vector3(0, 5, 0), 3); // New initial position, final position, and duration are set
}
Clearance
Once you are done with the motions, you can get rid of them like this:
// MonoBehaviour's OnDestroy
private void OnDestroy() {
// Dispose the motions that were registered
MotionKit.ClearPlaybacks(m_Ball);
MotionKit.ClearPlaybacks(m_CanvasRenderer);
MotionKit.ClearPlaybacks(m_BallColor);
}
The code above will dispose all the motions registered with owners: m_Ball, m_CanvasRenderer, and m_BallColor.
Inspector
owner and reuseID
To set the owner and reuseID in a MotionKitBlock (the inspector version), click Edit Owner and/or Reuse ID and then assign the desired values:
Clearance
In the MotionKitComponent the clearance is carried out automatically when the component is destroyed.