Getting Focus With The Mixed Reality Toolkit

Getting Focus 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 a Cube
  4. Create a new script named HighlightHandler
  5. Attach the HighlightHandler to your Cube
  6. Open the new script in your preferred text editor
  7. Implement the IFocusable interface
using UnityEngine;
using HoloToolkit.Unity.InputModule;

public class HighlightHandler : MonoBehaviour, IFocusable
{
    void IFocusable.OnFocusEnter()
    {
    }

    void IFocusable.OnFocusExit()
    {
    }
}
  1. Add Color, Renderer, and Material fields
using UnityEngine;
using HoloToolkit.Unity.InputModule;

[RequireComponent(typeof(Renderer))]
public class HighlightHandler : MonoBehaviour, IFocusable
{
    [SerializeField]
    private Color highlightColor;

    private Color initialColor;
    private Renderer rendererInstance;
    private Material materialInstance;

    void IFocusable.OnFocusEnter()
    {
    }

    void IFocusable.OnFocusExit()
    {
    }
}
  1. Cache initial Color, Renderer, and Material in Awake
  2. Properly destroy the Material instance in OnDestroy
private void Awake()
{
    rendererInstance = GetComponent<Renderer>();
    materialInstance = rendererInstance.material;
    initialColor = materialInstance.color;
}

private void OnDestroy()
{
    Destroy(materialInstance);
}
  1. Handle color changes in implemented methods
void IFocusable.OnFocusEnter()
{
    materialInstance.color = highlightColor;
}

void IFocusable.OnFocusExit()
{
    materialInstance.color = initialColor;
}

Next, let’s take a look at getting Generic Input and Clicking.

Comments are closed.