AnimatorExtensions
Extension methods for UnityEngine.Animator that provide callback-based subscriptions to OnAnimatorIK and OnAnimatorMove messages. Uses a hidden proxy MonoBehaviour (AnimatorListener) that is auto-attached to the Animator's GameObject on first subscription.
Definition
Namespace: Paragon Assembly: Paragon.dll
public static class AnimatorExtensionsRemarks
Unity's OnAnimatorIK and OnAnimatorMove messages are only sent to MonoBehaviour scripts on the same GameObject as the Animator. This extension class solves the problem of subscribing to these messages from external scripts that do not live on the Animator's GameObject.
It works by lazily attaching a private AnimatorListener component (hidden in the Inspector via HideFlags.HideInInspector) to the Animator's GameObject. The listener receives the Unity messages and forwards them via C# event Action delegates.
Listener Lifecycle
First
Add*Listenercall: If noAnimatorListenerexists on theGameObject, one is added automatically.Subsequent calls: Reuses the existing
AnimatorListener.Remove*Listenercalls: Unsubscribes the callback. TheAnimatorListenercomponent remains on theGameObject(not removed).
Quick Lookup
Subscribe to IK pass
animator.AddIKListener(callback)
Unsubscribe from IK pass
animator.RemoveIKListener(callback)
Subscribe to root motion
animator.AddMoveListener(callback)
Unsubscribe from root motion
animator.RemoveMoveListener(callback)
Methods
AddIKListener
Subscribes a callback to be invoked during OnAnimatorIK. Adds an AnimatorListener component if one does not already exist.
animator
Animator
The Animator to listen to
onAnimatorIKCallback
Action
Callback invoked each IK pass
RemoveIKListener
Unsubscribes a callback from OnAnimatorIK. No-op if no AnimatorListener exists on the GameObject.
animator
Animator
The Animator to stop listening to
onAnimatorIKCallback
Action
Callback to remove
AddMoveListener
Subscribes a callback to be invoked during OnAnimatorMove. Adds an AnimatorListener component if one does not already exist.
animator
Animator
The Animator to listen to
onAnimatorMoveCallback
Action
Callback invoked each move pass
OnAnimatorMove overrides root motion. When any MonoBehaviour on the GameObject implements OnAnimatorMove, Unity stops applying root motion automatically. Adding a move listener via this method has the same effect — root motion must be manually applied inside the callback.
RemoveMoveListener
Unsubscribes a callback from OnAnimatorMove. No-op if no AnimatorListener exists on the GameObject.
animator
Animator
The Animator to stop listening to
onAnimatorMoveCallback
Action
Callback to remove
Common Pitfalls
AnimatorListener is never auto-removed. Calling RemoveIKListener / RemoveMoveListener unsubscribes the delegate but does not remove the AnimatorListener component. If all listeners are removed, the component remains as an inert hidden component.
IK must be enabled on the Animator layer. OnAnimatorIK is only called for layers that have IK Pass enabled in the Animator Controller. If the IK listener callback is never invoked, check the layer settings.
Examples
Subscribing to IK from an External Script
See Also
Extensions — extension system overview
Last updated