Adds a start and end callback to a state in the animator. Can be used to track when an animation state has started and ended.
This package can be installed through the Unity Package Manager
with Unity version 2019.3 or greater.
Open up the package manager Window/Package Manager
and click on Add package from git URL...
.
Paste in this repository's url.
https://github.com/Jason-Skillman/State-Machine-Callback.git
Click Add
and the package will be installed in your project.
NOTE: For Unity version 2019.2 or lower
If you are using Unity 2019.2 or lower than you will not be able to install the package with the above method. Here are a few other ways to install the package.
- You can clone this git repository into your project's
Packages
folder. - Another alternative would be to download this package from GitHub as a zip file. Unzip and in the
Package Manager
click onAdd package from disk...
and select the package's root folder.
Alternatively you can also install this package as a git submodule.
$ git submodule add https://github.com/Jason-Skillman/State-Machine-Callback.git Packages/State-Machine-Callback
First click on the animation state in your animator. Click Add Behavior
and search for the StateMachineEvent
component. This component will catch events from StateMachineBehaviour
and send it through the IStateMachineCallback
interface.
Now that the callback behavior is attached to the animation state its time to use its callbacks. Create an empty script and attach it to the game object with the animator. Make sure that the script implements IStateMachineCallback
. This will give you all of the callbacks that the StateMachineEvent
component fires.
Multible animation state callbacks can be used, just attach the StateMachineEvent
component to each one in the animator. In your driver script that implements IStateMachineCallback
you can use the stateInfo
and layerIndex
parameters to separate out the animator callbacks.
In this example there are two states in the animator called TurnOn
and TurnOff
which both have the StateMachineEvent
component. You can use stateinfo.IsName
method to check which state is currently animating. This sample can also be installed by using the package manager.
public void OnAnimationStart(AnimatorStateInfo stateInfo, int layerIndex) {
//Use stateInfo to get the correct animation state name
if(stateInfo.IsName("TurnOn")) {
//...
} else if(stateInfo.IsName("TurnOff")) {
//...
}
}
If you don't want to make a custom script to handle all of the different kinds of animation events then you can use the StateMachineCallbackEvents
component. This component has already setup the animation events and converted them into UnityEvents
for quick access. Just attach this script to the game object with an animator.
Member | Description |
---|---|
animationStateName |
The name of the animation state. |
layerIndex |
The layer index of the animation state. |
Method Name | Parameters | Description |
---|---|---|
void OnAnimationStart() |
stateInfo, layerIndex | Called once when the animation state starts. |
void OnAnimationUpdate() |
stateInfo, layerIndex | Called every frame while animating. |
void OnAnimationEnd() |
stateInfo, layerIndex | Called once when the animation state ends. |