- Home /
How to Determine What AnimationClip/AnimationState Generated an AnimationEvent?
I am using Unity Pro 4.3.4f1 to add AnimationEvents to AnimationClips that I am playing within a AnimatorController. These AnimationEvents correspond to moments during a walk-run BlendTree where my character should generate footstep effects. It appears that AnimationEvents will occur for all AnimationClips whose weight is greater than zero within a BlendTree, so the character will generate two footstep effects per every one footstep actually taken unless the character is absolutely walking or absolutely running.
To address this, I planned to examine the AnimationEvent.animationState parameter passed to my message handler method to determine which events are duplicates that should be ignored. However, it appears that AnimationEvent.animationState is always null whether handling messages from Mecanim or the legacy Animation system. Thus, I am unable to determine which AnimationState/AnimationClip generated any given AnimationEvent and therefore cannot determine which AnimationEvents to ignore.
In my project, AnimationEvents are sent to methods named "OnAnimationEvent", where AnimationEvent.stringParameter is the name of the event, such as "FootstepLeft". Here is my handler method:
private void OnAnimationEvent( AnimationEvent e )
{
// Why is this always true?
if( e.animationState == null ) throw new System.NullReferenceException( "e.animationState" );
// ...omitted code...
}
My questions are:
Why is AnimationEvent.animationState null? Is it ever defined?
Are there other means to determine which AnimationState/AnimationClip generated a given AnimationEvent?
Update
I have also investigated the following workarounds, but they have failed and/or would require unreasonable workflows:
Set AnimationEvent.objectReferenceParameter to the AnimationClip itself. Unfortunately, UnityEditor throws errors if the AnimationClip is still embedded within an FBX file. This is a dealbreaker because I need the AnimationClips to remain embedded so they automatically update whenever an animator adjusts an animation.
Use AnimationEvent.stringParameter to identify the AnimationClip. This is susceptible to typos and changes in asset name, and would require the creation and maintenance of an AnimationEvent enumeration indexed with AnimationEvent.intParameter, since AnimationEvent.stringParameter was stolen to identify AnimationClips.
Use AnimationEvent.intParameter to identify the AnimationClip. This presents the same problems as workaround #2, but with AnimationClip and AnimationEvent swapped.
Thanks!