Los animation events pueden ser adjuntados a clips de animación importados en la pestaña de Animaciones de los Ajustes de importación de Animación.
Estos eventos permiten agregar datos adicionales a un clip importado, que determina cuando ciertas acciones deberían ocurrir en tiempo con la animación. Por ejemplo, para un personaje animado se podría querer agregar eventos para caminar y ciclos corriendo indicando cuando el sonido de pisadas debería reproducirse.
Para agregar un evento a una animación importada, se debe seleccionar el archivo de animación importado en la Ventana de Proyecto, luego en los ajustes de importación en el inspector, seleccione el botón Animaciones.
Luego se debe desplazar hasta el final de la parte inferior del inspector para encontrar el encabezado de Eventos, entre otros cuatro títulos desplegables como se muestra:
Expand the events heading to reveal the events timeline for the current imported animation clip.
To move the playback head to a different point in the timeline, use the timeline in the preview pane of the window:
Position the playback head at the point where you want to add an event, then click Add Event. A new event is created, indicated by a small white marker on the timeline. In the Function field, fill in the name of the function to call when the event is reached.
Make sure that any GameObject which uses this animation in its animator has a corresponding script attached that contains a function with a matching event name.
The example below demonstrates an event set up to call the Footstep
function in a script attached to the Player GameObject. This could be used in combination with an AudioSource to play a footstep sound synchronised with the animation.
You can also choose to specify a parameter, which is sent to the function called by the event. There are four different parameter types: Float, Int, String or Object.
By filling out a value in one of these fields, and implementing your function to accept a parameter of that type, you can have the value specified in the event passed through to your function in the script.
For example, you might want to pass a float value to specify how loud the footstep should be during different actions, such as quiet footstep events on a walking loop and loud footstep events on a running loop. You could also pass a reference to an effect Prefab, allowing your script to instantiate different effects at certain points during your animation.