Controller Input Events
This section demonstrates how to use Unity's Input System to access input events from the Magic Leap 2 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.
Type | Description |
---|---|
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) |
Controller Example - Input Action Events
The example script below subscribes to the Magic Leap controller's input events, and debugs the input values to the Debug Log.
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
UnityAPI/InputActionAsset
- An asset containing action maps and control schemes.
UnityAPI/ActionMap
- A mechanism for collecting a series of input actions and treating them as a group.
UnityAPI/InputAction
- 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.