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.
|InputAction.started||When a control is actuated (i.e. moving away from its resting position)|
|InputAction.performed||Called right after started, and continues to be called while the action is being performed.|
|InputAction.canceled||When the control moves back to its default value (i.e. resting position)|
The example script below subscribes to the Magic Leap controller's input events, and debugs the input values to the Debug Log.
This feature requires the
CONTROLLER_POSE permission to be enabled in your project's Manifest Settings. (Edit > Project Settings > Magic Leap > Manifest Settings)
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;
// Initialize the InputActionAsset
_magicLeapInputs = new MagicLeapInputs();
//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.
_controllerActions.IsTracked.performed -= IsTrackedOnPerformed;
_controllerActions.Trigger.performed -= HandleOnTrigger;
_controllerActions.Bumper.performed -= HandleOnBumper;
- An asset containing action maps and control schemes.
- A mechanism for collecting a series of input actions and treating them as a group.
- A named input signal that can flexibly decide which input data to tap.
- Unity Input System Quick Start Guide
- Learn how to get started with the new Unity Input System.