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 AnimatorExtensions

Remarks

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*Listener call: If no AnimatorListener exists on the GameObject, one is added automatically.

  • Subsequent calls: Reuses the existing AnimatorListener.

  • Remove*Listener calls: Unsubscribes the callback. The AnimatorListener component remains on the GameObject (not removed).

Quick Lookup

Goal
Method

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.

Parameter
Type
Description

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.

Parameter
Type
Description

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.

Parameter
Type
Description

animator

Animator

The Animator to listen to

onAnimatorMoveCallback

Action

Callback invoked each move pass

circle-exclamation

RemoveMoveListener

Unsubscribes a callback from OnAnimatorMove. No-op if no AnimatorListener exists on the GameObject.

Parameter
Type
Description

animator

Animator

The Animator to stop listening to

onAnimatorMoveCallback

Action

Callback to remove

Common Pitfalls

circle-exclamation
circle-exclamation

Examples

Subscribing to IK from an External Script

See Also

Last updated