Event callbacks are parameterless by default, but it can sometimes be useful to give them a parameter by setting one of the AnimancerEvent.Parameter...
options as their Callback
. This allows events to do things like specify which foot a footstep is referring to without needing to have a separate event name for each one as demonstrated in the Footstep Events sample.
Inspector | Context Menu |
---|---|
![]() |
When registering a callback to receive a parameter, you must specify the parameter type as a generic argument with slightly different syntax depending on how you're setting up the events:
[SerializeField] private AnimancerComponent _Animancer;
[SerializeField] private ClipTransition _Animation;
[SerializeField] private StringAsset _EventName;
protected virtual void Awake()
{
// Central Events.
_Animancer.Events.AddTo<AudioSource>(_EventName, PlaySound);
// Transition Events.
_Animation.Events.AddCallback<AudioSource>(_EventName, PlaySound);
// State Events.
AnimancerState state = _Animancer.Play(_Animation);
if (state.Events(this, out AnimancerEvent.Sequence events))
{
events.AddCallback<AudioSource>(_EventName, PlaySound);
}
}
private void PlaySound(AudioSource source) { }
Custom Parameter Types
Serialized event callbacks are stored as classes that implement IInvokable
:
Animancer.UnityEvent
inherits fromUnityEngine.Events.UnityEvent
to allow UnityEvents to be used as event callbacks.Animancer.UltEvent
inherits fromUltEvents.UltEvent
to allow UltEvents to be used as event callbacks.AnimancerEvent.Parameter
is the base class for objects which make a parameter value available to other callbacks registered for the same event.AnimancerEvent.ParameterBoxed
does the same thing for value type parameters (struct
s andenum
s).
So if you want to add another parameter type you can simply make a serializable class that inherits from the appropriate base parameter, for example:
[System.Serializable]
public class ParameterVector3 : Animancer.AnimancerEvent.ParameterBoxed<UnityEngine.Vector3> { }
Simply having that class in your project will allow you to select it when assigning event callbacks.
You could also implement IInvokable
to directly make your own custom callback:
[System.Serializable]
public class Invokable : Animancer.IInvokable
{
public virtual void Invoke()
{
// Do stuff.
}
}