Inputs Sample
This section describes how to read Magic Leap 2's controller input using Unity's Input System and Input Actions. The samples in this category assume that you are using the MagicLeapOpenXRInput.inputactions
asset provided in the Magic Leap Samples. However, they can be easily modified to support custom input actions.
Import Magic Leap Rig & OpenXR Input Samples
Prerequisites
- Magic Leap Unity SDK Installed
Importing the Magic Leap Rig & OpenXR Input Samples
- Opening Unity Package Manager
- Navigate to Window > Package Manager in Unity. This will open the Package Manager window.
- Selecting the Package
- In the Package Manager, select the Magic Leap SDK package from the list.
- Importing Samples
- In the package details, locate the Samples section which lists available samples.
- Click on the Import button next to the ML Rig & OpenXR Input sample.
- Confirming Import
- If prompted, confirm the import action by clicking Import.
- Unity will import the selected sample into your project. This may take a moment.
- Accessing Imported Samples
- Locate Magic Leap Rig and Input assets inside your project's
/Assets/Samples/MagicLeapSDK/<Version>/
directory.
- Locate Magic Leap Rig and Input assets inside your project's
Generate C# Classes from Input Actions
Once the input Samples have been imported, you can obtain input from the Magic Leap 2 using Unity Input System. One of the fastest ways to get started is to auto-generate the code required to access Magic Leap Input and it's bindings in your scripts.
- In the Project window, navigate to the
Assets/Samples/Magic Leap SDK/<Version>/ML Rig & OpenXR Input/
directory. - Select the MagicLeapOpenXRInput asset to view it's properties in the Inspector.
- In the Inspector, select Generate C# Class
- Click Apply.
Using the Input Script
This section demonstrates how to use Unity's Input System to access input from the Magic Leap 2 controller using the script generated from the Magic Leap Samples. Developers can read the data directly or by subscribing to an InputAction
callback.
Input actions contain 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) |
Example
The example script below subscribes to the Magic Leap controller's input events, and debugs the input values to the Debug Log.
This example assumes that the MagicLeapOpenXRInput script was generated using the default settings from the MagicLeapOpenXRInput.inputactions
asset. If you changed the input action bindings or if you changed the namespace and name of the generated script, you will need to adjust the code below accordingly.
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 MagicLeapOpenXRInput _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 MagicLeapOpenXRInput.ControllerAction _controllerActions;
void Start()
{
// Initialize the InputActionAsset
_magicLeapInputs = new MagicLeapOpenXRInput();
_magicLeapInputs.Enable();
//Initialize the ControllerActions using the Magic Leap Input
_controllerActions = new MagicLeapOpenXRInput.ControllerActions(_magicLeapInputs);
//Subscribe to your choice of the controller events
_controllerActions.IsTracked.performed += IsTrackedOnPerformed;
_controllerActions.Trigger.performed += HandleOnTrigger;
_controllerActions.Bumper.performed += HandleOnBumper;
}
void Update()
{
//Only debug the values if the controller is connected.
if(_controllerActions.IsTracked.IsPressed()){
//Read the input values directly
Debug.Log("Position:" + _controllerActions.Position.ReadValue<Vector3>());
Debug.Log("Rotation:" + _controllerActions.Rotation.ReadValue<Quaternion>());
}
}
// 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;
_magicLeapOpenXRInputs.Dispose();
}
}
See also
Unity/Using Input Action Assets
- For more information on using Input Action Assets in your project.
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.