Detecting Input Sources with the Mixed Reality Toolkit

Detecting Input Sources 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 InputSourceHandler
  5. Attach a SetGlobalListener component to the InputSourceHander GameObject
  6. Create a new script named InputSourceHandler
  7. Attach the InputSourceHandler to your InputSourceHander GameObject
  8. Open the new script in your preferred text editor
  9. Implement the ISourceStateHandler interface after the Monobehaviour class inheritance declaration
  10. Add logic for handling source detection and loss
using UnityEngine;
using HoloToolkit.Unity.InputModule;

public class InputSourceDetectionHandler : MonoBehaviour, ISourceStateHandler
{
    public void OnSourceDetected(SourceStateEventData eventData)
    {
        Debug.LogFormat("OnSourceDetected\r\nSource: {0}  SourceId: {1}",
            eventData.InputSource,
            eventData.SourceId);
        // Mark the event as used, so it doesn't fall through to other handlers.
        eventData.Use();
    }

    public void OnSourceLost(SourceStateEventData eventData)
    {
        Debug.LogFormat("OnSourceLost\r\nSource: {0}  SourceId: {1}",
            eventData.InputSource,
            eventData.SourceId);
        // Mark the event as used, so it doesn't fall through to other handlers.
        eventData.Use();
    }
}

Next, let’s get some Focus.

Comments are closed.