/ReactiveInputSystem

Reactive Extensions for Unity Input System

Primary LanguageC#MIT LicenseMIT

ReactiveInputSystem

Reactive Extensions for Unity Input System

license

日本語版READMEはこちら

Overview

ReactiveInputSystem is a library that provides functionality to convert events and device inputs from the Input System into Observables.

Setup

Requirements

  • Unity 2021.3 or later (Unity 2022.2 or later recommended)
  • Input System 1.0.0 or later
  • R3 0.1.0 or later

Installation

  1. Open Package Manager from Window > Package Manager.
  2. Click the "+" button > Add package from git URL.
  3. Enter the following URL:
https://github.com/AnnulusGames/ReactiveInputSystem.git?path=src/ReactiveInputSystem/Assets/ReactiveInputSystem

Alternatively, open Packages/manifest.json and add the following to the dependencies block:

{
    "dependencies": {
        "com.annulusgames.reactive-input-system": "https://github.com/AnnulusGames/ReactiveInputSystem.git?path=src/ReactiveInputSystem/Assets/ReactiveInputSystem"
    }
}

Extension Methods

By introducing ReactiveInputSystem, extension methods are added to convert events from InputAction, PlayerInput, PlayerInputManager, and others into Observables.

using UnityEngine.InputSystem;
using R3;
using ReactiveInputSystem;

InputAction inputAction;

inputAction.StartedAsObservable(cancellationToken)
    .Subscribe(x => ...);
inputAction.PerformedAsObservable(cancellationToken)
    .Subscribe(x => ...);
inputAction.CanceledAsObservable(cancellationToken)
    .Subscribe(x => ...);

PlayerInput playerInput;

playerInput.OnActionTriggeredAsObservable(cancellationToken)
    .Subscribe(x => ...)

Device Input

You can obtain input from any device as an Observable using the InputRx class.

InputRx.OnKeyDown(Key.Space)
    .Subscribe(_ => ...);

InputRx.OnMousePositionChanged()
    .Subscribe(x => ...);

InputRx.OnGamepadButtonDown(GamepadButton.North, cancellationToken)
    .Subscribe(_ => ...);

Additionally, using the OnAny** methods allows you to retrieve information about the pressed button.

InputRx.OnAnyKeyDown()
    .Subscribe(key => ...);

InputRx.OnAnyMouseButtonUp()
    .Subscribe(mouseButton => ...);

InputRx.OnAnyGamepadButton()
    .Subscribe(gamepadButton => ...);

Other Events

InputRx provides methods to convert events from the InputSystem class and events from InputUser into Observables.

// InputSystem.onAfterUpdate
InputRx.OnAfterUpdate()
    .Subscribe(_ => ...);

// InputSystem.onAnyButtonPress
InputRx.OnAnyButtonPress()
    .Subscribe(control => ...);

// InputUser.onChange
InputRx.OnUserChange()
    .Subscribe(x => ...);

InputControlPathEx

As a utility for Control Path-related operations, the InputControlPathEx class is provided.

You can use InputControlPathEx.GetControlPath() to obtain a Control Path from enumerations such as Key, MouseButton, GamepadButton, etc. This can be useful when implementing interactive rebinding, for example, with InputRx.OnAny**().

InputAction inputAction;

async Task RebindAsync(CancellationToken cancellationToken)
{
    inputAction.Disable();

    var path = await InputRx.OnAnyKeyDown(cancellationToken)
        .Select(x => InputControlPathEx.GetControlPath(x))
        .FirstAsync();

    inputAction.ApplyBindingOverride(0, path);
    inputAction.Enable();
}

License

MIT License