Updated Hologram Placement with Unity 5.5 and the HoloToolkit

Blog Posts ,HoloLens ,Programming ,Unity
December 8, 2016

Hey everyone!

In my post yesterday, I mentioned that there had been some changes to the HoloToolkit for Unity, and one of the things that I noticed today that had changed was the process for developing a hologram placement script. Allowing the user to leave a hologram in the world, so that the user can define where it belongs, is pretty important for a good experience (and not one where holograms are statically assigned an arbitrary location based on original orientation). The Holographic Academy covers hologram placement in Holograms 240, but it’s using an earlier version of the HolotoolKit and as such, the placement scripts defined there no longer work.

In the Holograms 240 course, the hologram placement script relies on the Gesture Manager, which, if you followed along yesterday, you’ll notice is no longer part of the HoloToolkit and has been replaced with the Input Manager. The Input Manager no longer has an option to override the focused object (at least, that I could find quickly) but I did notice that the GazeManager class stored the information about the currently selected hologram in the HitObject property.

By default, the GazeManager HitObject property has a public getter and private set function, which mostly makes sense – we normally won’t want to override the object that a user is gazing at – but in the case where we want to place a hologram, we can set the HitObject to whatever we want in order to force it in view of the user. The approach that I took for this was to:

  • Modify the GazeManager HitObject property to allow us to access the set functionality and force it to keep a specific Hologram in view
  • Update our Hologram Placement to use the Gaze Manager instead of the now-decommissioned Gesture Manager

For the hologram that we want to place, attach a “PlaceHologram” script to your game object and fill in the script like so:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using HoloToolkit.Unity.InputModule;
using System;

public class HologramPlacement : MonoBehaviour, IInputClickHandler {

    public bool GotTransform { get; private set; }
    // Use this for initialization
    void Start () {
        GazeManager.Instance.HitObject = gameObject;
    }
	
	// Update is called once per frame
	void Update () {
        if (!GotTransform)
        {
            transform.position = Vector3.Lerp(transform.position, ProposeTransformPosition(), 0.2f);
        }
    }

    Vector3 ProposeTransformPosition()
    {
        // Put the model 2m in front of the user.
        Vector3 retval = Camera.main.transform.position + Camera.main.transform.forward * 2;
        return retval;
    }

    public void OnInputClicked(InputEventData eventData)
    {
        // Note that we have a transform.
        GotTransform = true;
        GazeManager.Instance.HitObject = null;
    }
}

Like last time, you’ll need this class to implement the IInputClickHandler interface, contrasted with the older version that used OnSelect(). You’ll also want to make sure that your selected holograms have mesh colliders attached to them, or else the OnInputClicked object will never update and the hologram won’t be placed.

Then, you can do things like add giant hats to your friends!

 

Related Posts

3 thoughts on “Updated Hologram Placement with Unity 5.5 and the HoloToolkit”

  1. Flori says:

    Hi Livi,

    i found your blog – now im very happy about this amount of VR informations! 🙂 I have encountered the same problem and dont find the solution… Maybe you can help me?

    I activated gravity (=activating much fun) on an object and this worked well with the old holotoolkit using “OnSelect()”. With the new one it doent´t work anymore. Nothing happens.

    Here my old Code:
    ==============
    using UnityEngine;

    public class ActivateGravity : MonoBehaviour
    {
    // Called by GazeGestureManager when the user performs a Select gesture
    void OnSelect()
    {
    // If the sphere has no Rigidbody component, add one to enable physics.
    if (!this.GetComponent())
    {
    var rigidbody = this.gameObject.AddComponent();
    rigidbody.collisionDetectionMode = CollisionDetectionMode.Continuous;
    }
    }
    }

    And here the new one:
    =================
    using UnityEngine;

    public class ActivateGravity : MonoBehaviour
    {
    // Called by Gesturesinput?! when the user performs a Select gesture
    void OnInputClicked()
    {
    // If the sphere has no Rigidbody component, add one to enable physics.
    if (!this.GetComponent())
    {
    var rigidbody = this.gameObject.AddComponent();
    rigidbody.collisionDetectionMode = CollisionDetectionMode.Continuous;
    }
    }
    }

    I wish you a merry christmas and happy new VR 2017 😉 Keep up your good work!

    Flo (Black Forest, Germany)

  2. Flori says:

    Hi again Liv,

    sorry for annoying you – it´s working! Another post from you helped me. 😛

    using HoloToolkit.Unity.InputModule;
    using UnityEngine;

    public class ActivateGravity: MonoBehaviour, IInputClickHandler {
    public void OnInputClicked(InputEventData eventData)
    {
    // If the sphere has no Rigidbody component, add one to enable physics.
    if (!this.GetComponent())
    {
    var rigidbody = this.gameObject.AddComponent();
    rigidbody.collisionDetectionMode = CollisionDetectionMode.Continuous;
    }
    }
    }

    Flo

Leave a Reply