Skip to main content
Version: 14 Oct 2024

API Overview

Controller API Overview

This section provides an overview on how to use Unity's Input System to access Magic Leap 2's controller input.

  1. First create a new instance of MagicLeapInputs and initialize it by calling .Enable().
note

The MagicLeapInputs is an auto generated class that creates a dynamic instance of an InputActionAsset which includes predefined Action Maps that correspond to all of the Magic Leap 2's input.

  1. Create an instance of MagicLeapInputs.ControllerActions, passing in an instance of the MagicLeapInputs.
note

The MagicLeapInputs.ControllerActions is an Action Map that was auto-generated by the Unity Input System and includes predefined bindings for the Magic Leap 2 Controller Input Events.

  1. Subscribe to the input using the events in the ControllerActions and read input values using the Input Action's CallbackContext or read the Input Action's values directly using InputAction.ReadValue<T>().
note

View Unity's InputActions Documentation for more information on how to read values from the callback.

private MagicLeapInputs mlInputs;
private MagicLeapInputs.ControllerActions controllerActions;

void Start()
{
mlInputs = new MagicLeapInputs();
mlInputs.Enable();
controllerActions = new MagicLeapInputs.ControllerActions(mlInputs);
controllerActions.Bumper.performed += HandleOnBumper;
}

private void HandleOnBumper(InputAction.CallbackContext obj)
{
bool bumperDown = obj.ReadValueAsButton();
Debug.Log("The Bumper is pressed down " + bumperDown);
}

void OnDestroy()
{
mlInputs.Dispose();
}

Input Table

The following inputs can be accessed via ControllerActions

NameTypeControlTypeInitialStateCheckDescription
PositionValueVector3TRUEThe position of the controller in world space
RotationValueQuaternionTRUEThe rotation of the controller in world space
TriggerValueAnalogTRUEAn analog value between 0-1 corresponding to how far the trigger is pressed. Its activation range and "dead zone" can be assigned under Project Settings > Input System Package
BumperButtonButtonTRUEA digital value that corresponds to the pressing of the bumper button above the trigger
Touchpad1PositionValueVector2TRUEThe XY position of the first touch on the touchpad, where the center is 0,0 and the XY values are between -1 and 1
Touchpad1ValueButtonTRUEA digital value that corresponds to the pressing of the touchpad as a button
Touchpad1ForceValueAnalogTRUEAn analog value between 0-1 that corresponds to how hard the touchpad is pressed
Touchpad2ValueVector2TRUEThe XY position of the second touch on the touchpad, where the center is 0,0 and the XY values are between -1 and 1
MenuButtonButtonFALSEA digital value that corresponds to the pressing of the menu button (above the home button)
IsTrackedPass-Through--TRUEA digital value that represents whether the controller is currently connected and tracked