Skip to main content
Version: 14 Oct 2024

Migrating from ML1

Upgrade to Magic Leap's new Controller APIs

This guide provides an overview on how to upgrade an existing Magic leap Controller Script to the Magic Leap 2. To learn more about the Unity Input System view Unity's Input System Documentation page.

API Transition Table

TopicMagic Leap 1Magic Leap 2
ButtonsMLInput.OnControllerButtonUp

MLInput.GetController(0).IsBumperDown
controllerActions.Bumper.IsPressed()
controllerActions.Bumper.performed / cancelled delegate

controllerActions.Menu.IsPressed()
controllerActions.Menu.performed / cancelled delegate
TouchpadMLInput.GetController(0).TouchPosAndForce

MLInput.GetController(0).Touch1Active
controllerActions.TouchpadPosition.ReadValue<Vector2>()

controllerActions.TouchpadForce.ReadValue<float>()
Touchpad GesturesMLInput.OnControllerTouchpadGestureStart/End/Continue delegateGestureSubsystemComponent.gestureSubsystem.touchpadGestureEvents
TransformMLInput.GetController(0).Position

MLInput.GetController(0).Orientation
controllerActions.Position.ReadValue<Vector3>()

controllerActions.Rotation.ReadValue<Quaternion>()
TriggerMLInput.OnTriggerDown/Up delegate

MLInput.GetController(0).TriggerValue
controllerActions.Trigger.performed delegate

controllerActions.Trigger.ReadValue<float>()

Example

The differences between the Magic Leap 1 and Magic Leap 2 scripts
public class ExampleClass : MonoBehaviour
{
+ private MagicLeapInputs _magicLeapInputs;
+ private MagicLeapInputs.ControllerActions _controllerActions;

private void Start(){

+ _magicLeapInputs = new MagicLeapInputs();
+ _magicLeapInputs.Enable();
+ _controllerActions = new MagicLeapInputs.ControllerActions(_magicLeapInputs);

+ _controllerActions.Trigger.performed += HandleOnTrigger;
+ _controllerActions.IsTracked.performed += HandleOnControllerChanged;
+ _controllerActions.IsTracked.canceled += HandleOnControllerChanged;

- MLInput.OnTriggerDown += HandleOnTriggerDown;
- MLInput.OnControllerConnected += MLControllerConnected;
- MLInput.OnControllerDisconnected += MLControllerDisconnected;

}

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

+ private void HandleOnControllerChanged(InputAction.CallbackContext callbackContext)
+ {
+ if (controllerActions.IsTracked.IsPressed())
+ {
+ Debug.Log("Controller Connected");
+ }else{
+ Debug.Log("Controller Disconnected");
+ }
+ }

- private void HandleOnTriggerDown(byte controllerId, float value)
- {
- Debug.Log("Tigger Value " + value);
- }

- void MLControllerConnected(byte controllerId)
- {
- Debug.Log("Controller Connected");
- }

- void MLControllerDisconnected(byte controllerId)
- {
- Debug.Log("Controller Disconnected");
- }

}

Read the Controller's Input Directly

Using the Unity Input system, you can read Magic Leap 2's controller input directly using the InputAction.ReadValue<T>() method.

Note:

  • ML1's MLInput.Controller API is now obsolete, controller input should be accessed using MagicLeapInputs.ControllerActions instead.

Example

The differences between the Magic Leap 1 and Magic Leap 2 scripts
public class ExampleClass : MonoBehaviour
{
+ private MagicLeapInputs _magicLeapInputs;
+ private MagicLeapInputs.ControllerActions _controlActions;
- private MLInput.Controller _connectedController;

private void Start(){

+ _magicLeapInputs = new MagicLeapInputs();
+ _magicLeapInputs.Enable();
+ _controlActions = new MagicLeapInputs.ControllerActions(_magicLeapInputs);
+
- MLInput.OnControllerConnected += MLControllerConnected;
- MLInput.OnControllerDisconnected += MLControllerDisconnected;
}

void Update()
{
- if(_connectedController && _connectedController.Type == MLInput.Controller.ControlType.Control){

+ Debug.Log("Position:" + _controlActions.Position.ReadValue<Vector3>());
- Debug.Log("Position:" + _connectedController.Position);

+ Debug.Log("Rotation:" + _controlActions.Rotation.ReadValue<Quaternion>());
- Debug.Log("Rotation:" + _connectedController.Orientation);

+ Debug.Log("Trigger:" + _controlActions.Trigger.ReadValue<float>());
- Debug.Log("Trigger:" +_connectedController.TriggerValue);

+ Debug.Log("Bumper:" + _controlActions.Bumper.IsPressed());
- Debug.Log("Bumper:" + _connectedController.IsBumperDown);

+ Debug.Log("Touchpad1Position:" + _controlActions.TouchpadPosition.ReadValue<Vector2>());
- Debug.Log("Touchpad1Position:" + _control.Touch1PosAndForce.x +","+ _connectedController.Touch1PosAndForce.y);

+ Debug.Log("Touchpad1Force:" + _controlActions.TouchpadForce.ReadValue<float>());
- Debug.Log("Touchpad1Force:" + _control.Touch1PosAndForce.z);
- }
}

- void MLControllerConnected(byte controllerId)
- {
- _connectedController = MLInput.GetController(_controllerId);
- }

- void MLControllerDisconnected(byte controllerId)
- {
- _connectedController = null;
- }
}