the caf2code press

Development for the HoloLens in Unity

by | Augmented Reality, Development, Microsoft, Tutorial

It was only a few years ago that the idea of virtual or augmented reality seemed out of reach for most developers. Thanks to rapidly evolving technology, these tools are now at our fingertips. Microsoft’s HoloLens is one such tool, and what a powerful tool it is. In this article, you’ll get a brief overview of development for the HoloLens.

Basics of Unity

Unity can be downloaded here: https://unity3d.com/get-unity/download. The 3D project feature in Unity can be used for developing for the HoloLens and connects with Visual Studio to create scripts in C#.

A ‘Scene’ contains every object (also called GameObjects) in the ‘game’. You can think of them as if they are unique levels in a game. Each scene is saved under Assets > Scenes. You can double-click the scene within Unity to open it.

Black screen with list of projects and files

The Hierarchy window on the left-hand side of the screen will list all the objects included in the scene. Clicking an object will display its properties on the right-hand side of the screen in the Inspector window. The Inspector window shows all components that are attached to the selected object. Components can vary from built-in Unity elements, existing code, or newly developed code. For example, on a ‘Cat’ object:

Black boxes with list of objects and file names

This shows all the components attached to the ‘Cat’ object. Many components are built-in Unity components – Animator for example allows animation to be connected to the object; NearInteractionGrabbable and Object Manipulator are HoloLens specific components that allow the user to physically ‘grab’ the object while in AR. Some are third-party components that were added to aid with some functionality. Finally, you can attach your own custom-developed code inside of newly created files and fully customize it for what you need it to do.

Unity assets can be downloaded from their asset store found here: https://assetstore.unity.com/.

Existing assets for a project are located under the Assets folder. Assets can be anything from 3D models, custom scripts, or entire scenes; there are both assets for free and some with higher quality that cost money.

This Microsoft Learn module will help you get Unity and the Mixed Reality toolkit setup step by step: https://docs.microsoft.com/en-us/learn/modules/mixed-reality-toolkit-project-unity/

Visual Studio

Visual Studio is where code is modified. In Unity, expand and double click a Script file to open it in Visual Studio.

 

Black windows with white text

Code for Unity is written in C#. Variables that are declared as public will appear in the component script in the Unity Inspector window. They give you the ability to modify them before you run the program, such as the Radius and State variables in the screenshot above. This allows for changing the behavior of a specific object that uses this script without changing it for all objects that use the script.

For example, take the State field in a Cat object and a Dog object. This field is the starting state of the object. Perhaps we want the Cat to start off sleeping when it is spawned in: change the start state variable to ‘Sleep’ for just the cat. But maybe we want a dog object to start off following the camera when it is spawned in: change the state variable to ‘Follow camera’ for just the dog. This method is extremely useful and applies to almost all components.

These are some of the most popular methods that are built into Unity projects:

Awake: Awake is called only one time. It is called when the script is initialized, or the object is set to active (if started inactive).

Start: Start is called only one time. It is called right before the first frame update. Awake will be called before Start, so Awake is more useful for cases where you need to run code before Start.

Update: Update is called every frame and is the most commonly used function. For example, if you need to check for something to change every frame, you can do so here.

These methods are very important to know for any Unity development.

 

Testing

Testing directly through HoloLens can be completed by using the following procedure.

Image of man with hololens on looking at a red chair cube

  1. In Unity, navigate to Edit->Project Settings->Player(left side)->Other settings.
  2. About halfway down the ‘Other settings’ tab, change ‘Active Input Handling’ to ‘Both’.
  3. It may ask to restart, if so, say yes and wait for it to restart.
  4. Once Unity reopens, navigate to File->Build Settings.
  5. Select ‘Universal Windows Platform’ as the Platform.
  6. Change the following settings:
    • Set Target device to HoloLens
    • Set Architecture to ARM 64
    • Set Build Type to D3D Project
    • Set Target SDK Version to Latest Installed
    • Set Minimum Platform Version to 10.0.1024.0
    • Set Visual Studio Version to Latest installed
    • Set Build and Run On to USB Device
    • Set Build configuration to Release
  1. Click the Switch Platform button.
  2. Click Build (not Build And Run).
  3. Navigate to a file location to save the build.
  4. Once the build finishes (it may take a moment), navigate to the location chosen and open the Visual Studio .sln file generated.
  5. Connect the HoloLens directly to the PC.
  6. Follow the steps here to allow developer mode on the HoloLens and connect it to your computer: https://docs.microsoft.com/en-us/windows/mixed-reality/develop/platform-capabilities-and-apis/using-visual-studio?tabs=hl2
  7. In Visual Studio, switch the configuration to Release, architecture to ARM64, and target as Device (HoloLens must be connected to PC).
  8. Turn on HoloLens and put it on.
  9. Run the program, it will take a moment to load and deploy. Then it will display on the HoloLens.

 

 

Image courtesy of Microsoft