Skip to main content

IInputAction

Important Attention Reminder

Coding Style wiki


IInputAction is the input-action interface of InputSystem (universal signal dispatcher): implementations convert input from Unity New InputSystem or any other input plugin into C# events, so in-game control logic only subscribes to events and does not need to know about platform or device differences; registered via Inputs.IA.RegisterInputAction and driven by Inputs.IA.DriveUpdate.

NamespaceOxGKit.InputSystem
Typepublic interface IInputAction
SourceIInputAction.cs
using OxGKit.InputSystem;

Quick Start

using System;
using OxGKit.InputSystem;
using UnityEngine;
using UnityEngine.InputSystem;

public class PlayerAction : IInputAction
{
// Custom input event (subscribed to by subscribers)
public event Action<Vector2> onMove;

public void OnCreate()
{
// Bind events on registration (Unity New InputSystem as an example)
var ctrls = Inputs.CM.GetControlMap<PlayerControls>();
if (ctrls == null) return;
ctrls.Player.Move.performed += this._OnMove;
ctrls.Player.Move.canceled += this._OnMove;
}

public void OnUpdate(float dt)
{
// Optional polling-style input detection (driven by Inputs.IA.DriveUpdate)
}

public void RemoveAllListeners()
{
this.onMove = null;
}

private void _OnMove(InputAction.CallbackContext ctx)
{
// Re-dispatch as a game-level input signal
this.onMove?.Invoke(ctx.ReadValue<Vector2>());
}
}

General Rules

Lifecycle

  1. OnCreate: called once automatically by RegisterInputAction on registration — bind Control Maps or initialize other input plugins here.
  2. OnUpdate: driven every frame by DriveUpdate, used for polling-style input detection (can be left empty).
  3. RemoveAllListeners: must be called manually at the appropriate time (e.g., scene transitions or feature exit) to clear all subscribed events.

Attention Gameplay/feature code should only subscribe to the events dispatched by IInputAction — avoid scattering raw InputAction reads through the project, so the control logic stays decoupled from platforms and devices.

Reminder Remember to unsubscribe events when a feature exits; on scene/domain transitions call RemoveAllListeners() to clear subscriptions and avoid stale delegates.

Method Overview

MethodDescription
OnCreateCalled on registration (bind Control Maps or plugin input here).
OnUpdatePolled every frame (driven by Inputs.IA.DriveUpdate).
RemoveAllListenersClears all subscribed events.

OnCreate

void OnCreate()

Called automatically on registration by Inputs.IA.RegisterInputAction; usually fetches a control map via Inputs.CM.GetControlMap for event binding, or initializes signal sources of other input plugins.

Important If OnCreate fetches a Control Map, that Control Map must be registered first, otherwise GetControlMap returns null and the bindings are silently skipped.

OnUpdate

void OnUpdate(float dt)

Driven every frame by Inputs.IA.DriveUpdate (passing dt); use it for polling-style input detection (e.g., sampling input from non-event plugins), or leave it empty when polling is not needed.

RemoveAllListeners

void RemoveAllListeners()

Clears all subscribed events (sets the events to null); must be called manually at the appropriate time (e.g., scene transitions or feature exit).

Inputs.IA.GetInputAction<PlayerAction>().RemoveAllListeners();

Script Templates

Template Input Action.cs

Use Right-Click Create/OxGKit/Input System/Template Input Action.cs (Input Interface For Any) to quickly create an IInputAction implementation script (universal signal dispatcher — any input source can be implemented freely).

Activation flow after creation (at game startup):

  1. (If using Unity New InputSystem) register the control map first via Inputs.CM.RegisterControlMap.
  2. Register the Input Action via Inputs.IA.RegisterInputAction (automatically triggers OnCreate for event binding).
  3. Drive updates from your main loop (Main MonoBehaviour) via Inputs.IA.DriveUpdate(dt) (see the Module Intro for a complete example).

Template Input Binding Composite.cs

Use Right-Click Create/OxGKit/Input System/New Input System (Extension)/Template Input Binding Composite.cs (For Unity New Input System) to create a Binding Composite script for Unity New InputSystem, synthesizing several input parts into a single custom input value (e.g., "modifier key + direction keys" combined into a move input):

  • The template inherits InputBindingComposite<TValue> and defines a custom value struct.
  • Composite parts are declared with [InputControl(layout = "...")] (Button / Integer / Vector2 / Axis).
  • ReadValue reads each part, processes them, and returns the custom value.
  • [InitializeOnLoad] (Editor) and [RuntimeInitializeOnLoadMethod] trigger the static constructor that calls InputSystem.RegisterBindingComposite for automatic registration.

Activation flow after creation:

  1. The template's static constructor registers automatically at startup (InputSystem.RegisterBindingComposite) — no manual call needed.
  2. Open the Input Action Asset, Add Binding on the target Action, pick your custom Composite from the composite list, and assign keys to each part.
  3. Read the custom value with context.ReadValue inside the Input Action's callback (see below).
// Read the custom composite value inside the Input Action's callback (demo's MoveInputComposite as an example)
protected void OnMoveAction(InputAction.CallbackContext context)
{
// Read your composite values
var moveInput = context.ReadValue<MoveInputComposite.MoveInput>();
this.onMoveAction?.Invoke(moveInput);
}

Reminder A composite must be registered before it is first used in a binding; for it to show up in the Input Action Asset editor, it has to be registered from code that runs in Edit Mode. See the Samples InputSystem Demo (MoveInputComposite and PlayerControls.inputactions) for a complete example.