Xbox Controller Input With The Mixed Reality Toolkit

Xbox Controller Input With The Mixed Reality Toolkit

What you’ll need

If you haven’t done so already, be sure you’ve properly setup your development environment and you’ve imported the Mixed Reality Toolkit into your project.  You’ll also need to be familiar with the Unity Editor and its interface controls.  If you are not, there is a great tutorial series to get you started.

Getting Started

  1. Create a new scene
  2. Run the MRTK scene wizard via:
    MixedRealityToolkit/Configure/Apply Scene Settings
  3. Create an empty GameObject
  4. Rename the GameObject to XboxControllerSource
  5. Attach the XboxControllerSource to the XboxControllerSource GameObject
  6. Create a new script named XboxControllerHandler
  7. Attach the XboxControllerHandler to the XboxControllerSource GameObject
  8. Open the new script in any text editor
  9. Your new XboxControllerHandler class should inherit from XboxControllerHandlerBase
  10. Add logic for handling source detection/loss and XboxInputUpdate
using UnityEngine;
using HoloToolkit.Unity.InputModule;

public class XboxInputHandler : XboxControllerHandlerBase
{
    public override void OnSourceDetected(SourceStateEventData eventData)
    {
        base.OnSourceDetected(eventData);
        Debug.LogFormat("Joystick {0} with id: \"{1}\" Connected", GamePadName, eventData.SourceId);
    }

    public override void OnSourceLost(SourceStateEventData eventData)
    {
        Debug.LogFormat("Joystick {0} with id: \"{1}\" Disconnected", GamePadName, eventData.SourceId);
        base.OnSourceLost(eventData);
    }

    public override void OnXboxInputUpdate(XboxControllerEventData eventData)
    {
        // FYI, eventData.Use() is called in the base implementation.
        base.OnXboxInputUpdate(eventData);

        Debug.LogFormat(
            "{19}\n" +
            "LS Horizontal: {0:0.000} Vertical: {1:0.000}\n" +
            "RS Horizontal: {2:0.000} Vertical: {3:0.000}\n" +
            "DP Horizontal: {4:0.000} Vertical: {5:0.000}\n" +
            "Left Trigger:  {6:0.000} Right Trigger: {7:0.000} Shared Trigger: {8:0.00}\n" +
            "A: {9} B: {10} X: {11} Y: {12}\n" +
            "LB: {13} RB: {14} " +
            "LS: {15} RS: {16}\n" +
            "View: {17} Menu: {18}\n",
            eventData.XboxLeftStickHorizontalAxis, eventData.XboxLeftStickVerticalAxis,
            eventData.XboxRightStickHorizontalAxis, eventData.XboxRightStickVerticalAxis,
            eventData.XboxDpadHorizontalAxis, eventData.XboxDpadVerticalAxis,
            eventData.XboxLeftTriggerAxis, eventData.XboxRightTriggerAxis, eventData.XboxSharedTriggerAxis,
            eventData.XboxA_Pressed, eventData.XboxB_Pressed, eventData.XboxX_Pressed, eventData.XboxY_Pressed,
            eventData.XboxLeftBumper_Pressed, eventData.XboxRightBumper_Pressed,
            eventData.XboxLeftStick_Pressed, eventData.XboxRightStick_Pressed,
            eventData.XboxView_Pressed, eventData.XboxMenu_Pressed,
            GamePadName);
    }
}

Next, let’s implement Voice Commands.

Comments are closed.