Now let’s start with a very simple app. In this, we will add a cube and rotate it on the world’s Y axis. And you’ll see something like this in the end on your emulator or your Hololens device.
Open your Unity. For future easy access, pin this to your taskbar. Click on New and provide a project name and location of your choice. Make sure 3D is selected. Click on Create project. Unity opens up in a minute or so.
I would recommend changing the layout to 2 by 3. Click on Window -> Layouts and select 2 by 3. It should look like the screenshot below. Don’t worry if your theme is white instead of black. I just prefer the dark side 🙂 .The 2 by 3 layout will make it easier for you to see your scene and game previews together.
I will not walk through Unity here. We need basic knowledge for now. For that you can refer to the Unity Tutorials
Your Unity first will look empty. However you may already have a Camera in your Hierarchy (previous versions of Unity) – keep it. I will show you how to change the setup of your camera later. If you don’t have a Camera, then we will add one. Right click inside your Hierarchy and click on Camera to add it. It will somehow add 2 cameras. Delete the one named camera. Keep the Main Camera and Directional Light
Now, we add the cube. Right click on the Hierarchy -> 3D Object -> Cube. Select the Cube and on the Inspector which is on your right, click on Add Component. Type New Script in the search field and click. Now enter the name of your script, make sure the language selected is C#, and click Create and Add.
The script will be added to your Cube and when you select the Cube, you can see it on it’s Inspector. Right click the script and choose Edit Script. It should ideally start in Visual Studio. However, if you have MonoDevelop, then change the settings to choose your default editor as Visual Studio (Unity -> Edit -> Preferences -> External Tools -> External Script Editor -> change to Visual Studio in the dropdown) It is a lot easier and all of my future articles will be showing VS as the editor. You can also keep MonoDevelop if you are comfortable with it
In the script, add the transform.Rotate line in the Update frame. This enables the cube to rotate around the world’s Y axis (don’t worry about understanding all of this now). Go back to Unity and click on the Main Camera and change the Position under Transform to X:-0.2 Y:-0.2 Z:-7.5. This makes you see the Cube in the Game window and also later on the Emulator clearly. Note: This is only for this project. For the others, you may have to play around with the camera position to show the objects in front of it clearly
Save the scene by pressing Ctrl-S and give a name for the scene. Now test your code in Unity. If all works fine, you should be seeing this when you press Play (the play button on top of the screen)
Great, now let’s try that on the Emulator/Hololens. For this, you need to do the following changes:
Change Camera settings
Click on Main Camera. On the Inspector, change to Solid Color under Clear Flags. Change Background to Black (you can choose any color). Change Near to 0.85 on Clipping Planes. This is a comfortable distance in Mixed Reality to render the object to the user.
Change Player Settings
Click on Edit -> Project Settings -> Player. On the Inspector, click the checkbox Virtual Reality Supported under XR Settings. Also change the Scripting Backend to .Net
Build and Deploy
Click on File -> Build Settings -> Universal Windows Platform. Change the Target Device to Hololens. SDK to Latest Installed. Check Unity C# Projects. Now add the current scene by clicking on Add Open Scenes and check the scene which you have previously saved. Click on Build. In the explorer window that opens up, create a new folder called app (or anything you want). Choose that and click on Select Folder. The build takes takes a few minutes.
After the build is complete, the app folder opens up automatically. Double click on the .sln file which is created in that. This opens up in Visual Studio.
To deploy to the Emulator, Choose Release, x86, Hololens Emulator on the top menu. If you don’t see the Hololens emulator, then download it from here and restart Visual Studio.
Now choose Ctrl-F5 or Debug -> Start without Debugging. After a minute or so (or may take longer depending on how fast your machine is) the Hololens Emulator opens up and after the Made with Unity splash screen, shows the cube rotating.
To deploy to the Hololens, first connect the Hololens device to the PC via the USB cable provided. Make sure the Hololens is on. It goes to sleep most times when it is idle for about 15-20 minutes. Choose Release, x86, Device. For first time use of device to Visual Studio, you will be prompted to pair the device. For this navigate to the Settings -> Update & Security -> For developers on your Hololens. Under Paired devices, click on Pair and you will get a PIN. Type this into Visual Studio. This needs to be done only for the first time.
Once the app is deployed onto the Hololens, it can be accessed again by navigating the Apps on the Hololens.
You can also deploy to the Hololens without a USB cable i.e remotely. First select Remote Machine option for your solution while deploying.
Enter the IP Address of the Hololens (explained below where you would find this) . The Hololens sometimes is also shown under Auto Detected
Sidenote: There is also a quick way to see any UI based changes (not code) via Unity if you have a Hololens. You are not building and deploying here, but just very quickly checking if your UI based changes would look okay on the Hololens. As you have experienced, the build and deploy takes a lot of time for the Hololens. It is slow. I hope Microsoft does something soon about that. So this method will save you oodles of time when you just want to see your UI based changes
Now for the quick check for your UI changes, go to Unity, click on Window -> Holographic Emulation. In Emulation Mode choose Remote to Device. In Remote Machine enter the IP address of the Hololens. For this find the app Holographic Remoting on the Hololens and tap to place it. The device IP Address will be displayed. Keep the Holographic Remoting on even after you enter the IP Address for this method to work. Ensure that both your Hololens and your PC is on the same network. Else this won’t work. Press on Connect. You’ll know it’s connected, if the Connection Status is in green and says Connected. If not, reduce the Max BitRate and try again. Now click the Play button in Unity and it shows up on your Hololens too! Note that any code changes you do CANNOT be checked with this method. Only UI based changes i.e via Unity can be emulated.
Error: which I encountered at this stage: XR Open VR… Installation Path not found.
Solution: Simply close Unity and open it again. This should work after
Some other possible errors and its solutions
Error: do you not have windows UAP SDK?
Solution: The installed VS 2017 was supposed to have the latest Windows 10 SDK installed. But maybe I skipped it while installing. So go here and download the latest SDK https://developer.microsoft.com/en-US/windows/downloads/windows-10-sdk Choose the ones which I selected (see below) HDD space required: 2.1 GB
Error: application.dlls not found While deploying the project solution in Visual Studio, this might occur. For this, select the project solution in Visual Studio and right click and Clean solution. If this doesn’t fix it, then delete app folder and rebuild and deploy
Did you have any other errors or things went smoothly? Drop me a comment below.