InputActionCallback

Delegate wrapper that adapts arbitrary callback signatures into Unity's Action<InputAction.CallbackContext> format. Automatically handles type conversion — methods with no parameters, typed value parameters (Action<Vector2>, Action<float>, etc.), or the full CallbackContext are all supported transparently.

Definition

Namespace: Paragon.Core.InputSystem Assembly: Paragon.dll

public class InputActionCallback

Remarks

Unity's Input System requires all callbacks to accept InputAction.CallbackContext. InputActionCallback eliminates this boilerplate by wrapping delegates with different signatures:

Source Signature
Wrapping Strategy

Action (no params)

_ => callback() — context is discarded

Action<InputAction.CallbackContext>

Used directly — no wrapping

Action<T> where T : struct

ctx => callback(ctx.ReadValue<T>()) — value auto-extracted

The generic wrapping uses cached reflection: CallbackWrapper<T> is a generic method resolved once per T and cached in a static dictionary. This avoids repeated reflection overhead for the same input type.

The class also provides an implicit conversion operator to Action<InputAction.CallbackContext>, allowing it to be used directly with Unity's InputAction.started, performed, and canceled events.

Quick Lookup

Goal
How

Create from a delegate

InputActionCallback.From(myDelegate)

Use as Unity callback

Implicit conversion: inputAction.performed += myCallback

Methods

From (static factory)

Creates an InputActionCallback from any supported delegate type.

Parameter
Type
Description

callbackDelegate

Delegate

The callback to wrap. Supported: Action, Action<CallbackContext>, Action<T> where T : struct

Returns: A new InputActionCallback wrapping the delegate.

The factory uses pattern matching to select the wrapping strategy:

  1. Action → wraps with context discard

  2. Action<InputAction.CallbackContext> → passes through

  3. Any other single-parameter delegate → routes through WrapCallback() for generic type conversion

Operators

Implicit to Action<CallbackContext>

Allows InputActionCallback to be used wherever Unity expects Action<InputAction.CallbackContext>.

This enables direct use with Unity's event system:

Internal Mechanism

Type Conversion Pipeline

CallbackWrapper<TInput>

Internal generic method that creates the actual wrapping lambda:

This is never called directly — it is invoked via reflection with the appropriate TInput type.

circle-info

The wrapper method cache (wrapperMethods dictionary) is static and shared across all InputActionCallback instances. Each unique input type (Vector2, float, etc.) is resolved and cached once for the application lifetime.

Common Pitfalls

circle-exclamation
circle-exclamation
circle-exclamation

See Also

Last updated