Skip to main content

Events

Key Events

Milease allows you to insert Actions into animations, making them more flexible.

The syntax is as follows:

[offset] + Action.AsMileaseKeyEvent()

Let’s revisit the previous example and make a small modification:

  • The ball fades in from transparent to white
  • While the ball is changing color, the music volume fades from 0 to 1
  • Once the above animations complete, the ball starts to move
  • Log "The ball has started moving!" — (this is the inserted action)
  • While the ball is moving, it scales up
  • Once the above animations complete, the following actions occur in sequence: the camera's fov decreases, and the camera rotates

Then the code can be modified as:

MAni.Make(
1f / ballSpriteRender.MQuad(x => x.color, Color.clear, Color.white),
1f / music.MQuad(x => x.volume, 0f, 1f)
).Then(
new Action(() => {
Debug.Log("The ball has started moving!");
}).AsMileaseKeyEvent(),
2f / ballTransform.MQuad(x => x.position, Vector3.zero, Vector3.one * 3f),
1f + 0.5f / ballTransform.MQuad(x => x.localScale, Vector3.one, Vector3.one * 2f)
).ThenOneByOne(
1f / camera.MQuad(x => x.fieldOfView, 6f, 4.5f),
1f / camera.MQuad(x => x.localEulerAngles, Vector3.zero, new Vector3(0, 0, 20f))
).Play();

This will log "The ball has started moving!" right when the ball starts to move. Of course, you can also specify a delay for the action, like so:

MAni.Make(
1f / ballSpriteRender.MQuad(x => x.color, Color.clear, Color.white),
1f / music.MQuad(x => x.volume, 0f, 1f)
).Then(
0.5f + new Action(() => {
Debug.Log("The ball has started moving!");
}).AsMileaseKeyEvent(),
2f / ballTransform.MQuad(x => x.position, Vector3.zero, Vector3.one * 3f),
1f + 0.5f / ballTransform.MQuad(x => x.localScale, Vector3.one, Vector3.one * 2f)
).ThenOneByOne(
1f / camera.MQuad(x => x.fieldOfView, 6f, 4.5f),
1f / camera.MQuad(x => x.localEulerAngles, Vector3.zero, new Vector3(0, 0, 20f))
).Play();
info

If the delay time for the key event exceeds the maximum duration of the current animation group, it is still valid. In this case, the next animation group will wait until this key event is triggered before starting.

Animation Completion Event

Additionally, you can pass a delegate to the Play function, which will be called once the animation finishes playing.

MAni.Make(
...
).Play(() => {
Debug.Log("Animation completed!");
});
info
  • If the animation is set to loop, this delegate will be invoked multiple times — once at the end of each cycle.
  • If the same animation is played again before the current one finishes, its progress will reset and the new delegate will replace the previous one, meaning the old one will never be called.