IInputAction
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.
| Namespace | OxGKit.InputSystem |
| Type | public interface IInputAction |
| Source | IInputAction.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
- OnCreate: called once automatically by RegisterInputAction on registration — bind Control Maps or initialize other input plugins here.
- OnUpdate: driven every frame by DriveUpdate, used for polling-style input detection (can be left empty).
- 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
| Method | Description |
|---|---|
| OnCreate | Called on registration (bind Control Maps or plugin input here). |
| OnUpdate | Polled every frame (driven by Inputs.IA.DriveUpdate). |
| RemoveAllListeners | Clears 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):
- (If using Unity New InputSystem) register the control map first via
Inputs.CM.RegisterControlMap. - Register the Input Action via
Inputs.IA.RegisterInputAction(automatically triggersOnCreatefor event binding). - 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). ReadValuereads each part, processes them, and returns the custom value.[InitializeOnLoad](Editor) and[RuntimeInitializeOnLoadMethod]trigger the static constructor that callsInputSystem.RegisterBindingCompositefor automatic registration.
Activation flow after creation:
- The template's static constructor registers automatically at startup (
InputSystem.RegisterBindingComposite) — no manual call needed. - 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.
- Read the custom value with
context.ReadValueinside 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.