Project “Master Thesis” #4 – Adding Code

Now it’s time for some actual code. So far if we’d build the project and open it with Visual Studio Code, it’d be executable on the HoloLens Emulator. However, there’s nothing to do. That’s about to change.I need (by the time of writing this article) three functions to be implemented. The first one is to color the elements of the hand whenever one is clicked. Secondly, I want to be able to rotate the bones. And lastly I want to user to be able to place the hand wherever he wants.

Let’s start with the last one, as it’s the easiest. All I have to do in order for this to work, is drag the TapToPlace script (which comes with the MixedReality Toolkit) onto the “handle” game object. When I then click on the “handle” game object I can further tweak the settings in the inspector. I want the parent game object to move, so I enable the checkbox. Since I can tell the script what the parent game object is, I choose “HoloCollection” because I want the UI elements to be moved too.

Second easiest is the other UI element: the rotation pill. I want the pill and the hand, but not the rest of the objects to rotate by 90 degrees when I perform an AirTap on the pill game object. That’s fairly easy to realise.

The “hard” part of this task is figuring out how to detect the AirTap. Here’s how: First we need to state, that we’re using the InputModule from the HoloToolkit. We do so by adding using HoloToolkit.Unity.InputModule; to the top of our C# script.

Secondly, the class itself must derive from IInputClickHandler, so the class declaration must look like this: public class RotateObject : MonoBehaviour, IInputClickHandler.

Finally one can add the OnInputClicked-function:

public virtual void OnInputClicked(InputClickedEventData eventData)
    elementsToRotate.transform.Rotate(90, 0, 0);
    this.transform.Rotate(90, 0, 0);

But wait, there’s more. Where does that elementsToRotate come from? Simple: It’s a public variable I declared as a field. Great thing about this is, that public fields appear in Unity as a field you can drop things (like game objects) on.

So if you declare public GameObject elementsToRotate; Unity will allow you to drop any game object on it in the Editor. You can then simply access that game object from code without the need to first search it by name or tag.

Keep in mind that you can restrict what can be dropped in the Editor by appropriately adjusting the field type to i.e. Text.

Knowing how to handle click events is the most important anomaly when it comes to developing HoloLens Applications, in my opinion.

Since this series focuses on developing a HoloLens application, I will not go into the details of manipulating game objects from code like the rotation depicted above.

Leave a Reply

Your email address will not be published. Required fields are marked *