Skip to main content
Version: 22 May 2023

Controller Input Events

This section demonstrates how to use Unity's Input System to access and receive the input events that are triggered whenever an action is performed on the Magic Leap 2's controller.

Input actions contain using 3 callbacks, started, performed, and canceled. While the order and how the events are triggered depend on the type of the action, the table below shows the default behavior.

TypeDescription
InputAction.startedWhen a control is actuated (i.e. moving away from its resting position)
InputAction.performedCalled right after started, and continues to be called while the action is being performed.
InputAction.canceledWhen the control moves back to its default value (i.e. resting position)

Example

The example script below subscribes to the Magic Leap controller's input events, and debugs the input values to the Debug Log.

caution

This feature requires the CONTROLLER_POSE permission to be enabled in your project's Manifest Settings. (Edit > Project Settings > Magic Leap > Manifest Settings)

using UnityEngine;
using UnityEngine.InputSystem;

public class ExampleClass : MonoBehaviour
{
// This is was autogenerated and allows developers to create a dynamic
// instance of an InputActionAsset which includes predefined action maps
// that correspond to all of the Magic Leap 2's input.
private MagicLeapInputs _magicLeapInputs;

// This class is an Action Map and was autogenerated by the Unity Input
// System and includes predefined bindings for the Magic Leap 2 Controller
// Input Events.
private MagicLeapInputs.ControllerActions _controllerActions;

void Start()
{
// Initialize the InputActionAsset
_magicLeapInputs = new MagicLeapInputs();
_magicLeapInputs.Enable();

//Initialize the ControllerActions using the Magic Leap Input
_controllerActions = new MagicLeapInputs.ControllerActions(_magicLeapInputs);

//Subscribe to your choice of the controller events
_controllerActions.IsTracked.performed += IsTrackedOnPerformed;
_controllerActions.Trigger.performed += HandleOnTrigger;
_controllerActions.Bumper.performed += HandleOnBumper;
}

// Handles the event to determine if the controller is tracking.
private void IsTrackedOnPerformed(InputAction.CallbackContext obj)
{
Debug.Log("The Controller Is tracking");
}

// Handles the event for the Trigger.
private void HandleOnTrigger(InputAction.CallbackContext obj)
{
float triggerValue = obj.ReadValue<float>();
Debug.Log("The Trigger value is : " + triggerValue);
}

// Handles the event for the Bumper.
private void HandleOnBumper(InputAction.CallbackContext obj)
{
bool bumperDown = obj.ReadValueAsButton();
Debug.Log("The Bumper is pressed down " + bumperDown);
Debug.Log("The Bumper was released this frame: " + obj.action.WasReleasedThisFrame());
}

// Handles the disposing all of the input events.
void OnDestroy()
{
_controllerActions.IsTracked.performed -= IsTrackedOnPerformed;
_controllerActions.Trigger.performed -= HandleOnTrigger;
_controllerActions.Bumper.performed -= HandleOnBumper;
_magicLeapInputs.Dispose();
}
}

See also