.net, HoloLens, Unity, UWP

Coding for the HoloLens with Unity 5 – Part #3: Deploying to the emulator (and using it)

Last time, I created a simple Hello World project in Unity 5 for the HoloLens. This time, I’ll deploy it to the HoloLens Emulator.

There’s a few other things I’ll look at as well – how to use the Emulator from your keyboard, and some hints on setting up your Unity HoloLens project for a code versioning system.

Building your Unity HoloLens project

Now that we have created and saved our Unity project, we’re ready to deploy it to our HoloLens. Inside the Unity environment, open the File menu and select “Build Settings…”.

screenshot.1468616434

This will open a screen similar to the one below. I’ve modified some of the settings:

  • I clicked on the “Add Open Scenes” to add the default “Hello World” scene;
  • I selected the platform “Windows Store”;
  • I’ve changed the SDK to “Universal 10”;
  • I’ve changed the UWP Build Type to D3D;
  • Finally I’ve checked the box beside “Unity C# Projects”.

screenshot.1468616326

Next, click on the button saying “Player Settings…” – the Inspector view will appear on the main Unity development environment.

screenshot.1468618370

The most important property page is “Other Settings”. Make sure that the “Virtual Reality Supported” box is ticked, and that the value under “Virtual Reality SDKs” is “Windows Holographic” (I’ve highlighted this in red below).

screenshot.1468618700

Now click on the “Build” button on the “Build Settings” window, which should still be on top of the Unity IDE. You’ll immediately be shown a folder dialog box, asking you to select a folder in which to create the Visual Studio project. I normally choose to create a folder named “App” alongside the other folders in the Unity Project. Choose this folder and hit the “Select Folder” button.

screenshot.1468619335

A few windows will pop up showing progress bars, and eventually a Windows Explorer window appears with the folder “App” selected. I opened the “App” folder, and double clicked on the “HelloWorld.sln” file, to open the solution in Visual Studio 2015.

Deploying to the emulator

When Visual Studio opens, ensure that the “Release” configuration is chosen, targetting the x86 architecture. I’ve selected the HoloLens Emulator as a deployment device, rather than a HoloLens.

screenshot.1468620806

After clicking on the button to start the HoloLens Emulator, the emulator starts but I see a warning message (shown below).

screenshot.1468621830

I select “Continue Debugging”, and the application starts in the emulator, showing a sphere orbiting the planet.

screenshot.1468622276

Using the emulator

The emulator is obviously not a perfect substitute for the real HoloLens hardware, though it’s still pretty good. One of the things I initially found difficult was how to navigate around the virtual world. If you’re familiar with gaming on a keyboard you’ll probably find it quite easy – forward, back, left and right can be controlled by W,S,A and D, and looking up, down, left and right can be controlled by the corresponding keyboard arrow keys.

I personally don’t really like using the keyboard to control the emulator, and fortunately an alternative option is to connect an Xbox 360 controller to my PC by USB – I find the game controller a much easier way to navigate in the virtual world.

There’s a set of instructions on how to use the emulator at this link, and full details on advanced emulator control at this link.

The emulator even offers an online device portal, available by clicking on the globe icon in the list of icons on the top right of the emulator – this just opens up a site in a browser, as shown below. There’s more information on how to use the device portal at this link.

screenshot.1468700023

All the information and functions available through the device portal are also available through a RESTful API – so for example, to see the device battery status, you can browse to:

http://<your device’s IP address>/api/power/battery

and the response will be a JSON representation of your battery status.

Full details of the RESTful API are available at this link.

Wrapping up

So in the last three posts, we’ve:

We’ve also looked at how to use the emulator, and how to see its Device Portal.

Next time, I’ll look at preparing our code for a code versioning system such as Subversion or GitHub.

.net, HoloLens, Making, Unity, UWP

Coding for the HoloLens with Unity 5 – Part #2: Creating a simple Hello World project

Last time I looked at setting up my development environment to allow me to develop for the Microsoft HoloLens Emulator. This time, I’m going to create a project in Unity, add in a simple primitive object, and use some C# to do something interesting with this object.

Creating a new Unity 5 project

If you’ve installed Unity correctly, you’ll be shown a screen like the one below after you open Unity 5 HTP for the first time.

screenshot.1468527247

Click on the “New project” button, and the screen should change to one similar to the one below. I’ve chosen the name “HelloWorld” for my project, and I’ve saved it to my desktop.

screenshot.1468527270

After entering the name and location of the new Unity project, I clicked the “Create project” button and Unity shows the screen below. This is an (almost) empty project, which only has the project’s main camera and the default directional light.

screenshot.1468527293

The next step is to update the scene with some settings which make sense for a HoloLens app.

Updating the scene for the HoloLens

The default camera is set about 10m behind the origin point of the scene. We’re going to make a few changes to this camera using the Inspector tab on the right hand side.

  • First, I changed the position of the camera to (0,  0, 0), meaning X = 0, Y = 0, and Z = 0;
  • Next, in the Camera section, I changed the Clear Flags dropdown value to Solid Color.
  • Finally, I change the Background property to Black (R = 0, G = 0, B = 0, A = 0).

screenshot.1468527338

These ensure that the camera – i.e. the point through which we will view the world with the HoloLens – is at the origin point.

Also, we’ve removed the default Skybox (i.e. background image), and any pixels rendered as black in our scene will appear as transparent in the HoloLens.

Add a cube

Now that we have the scene configured for the HoloLens, it’s time to add a simple object to our scene.

First, we right click on our Hierarchy pane on the left and side, select “3d Object”, and then select “Cube” from the sub-menu that appears.

screenshot.1468527392

A simple cube should appear in the centre of the scene, like in the image below. If the image does not appear in the correct place, make sure that the cube object appears in the Hierarchy menu at the same level of indentation as the Main Camera and the Directional Light.

screenshot.1468527417

Create a material

I’d like my cube to be a little more interesting than just a grey block – I’d like it to have a red colour. In Unity, we can achieve this by creating a Material asset and adding this component to the grey cube.

To create a material, I right click on the Assets node in the Project panel in the bottom left of the screen. From the context menu that appears, I select “Create”, and from the next menu that appears I select “Material”.

screenshot.1468527457

A new item is created and appears in the Assets panel – the cursor and focus are on this item, and I entered the value “Red”. Also, a grey ball appears in the bottom right hand corner. In the Inspector panel, I clicked on the color picker beside the “Albedo” label. In the pop up that appears, I selected a red colour, which updates the colour of the ball in the bottom right hand corner, as shown below.

screenshot.1468527481

Now that I’ve created a material, I can assign this to the cube. First I selected the Cube object in the Hierarchy panel. Next, I dragged the material named “Red” onto the Inspector panel on the right hand side. This is a surface that I can drag and drop components to. As soon as I drag the Red material to the Inspector for the cube, the cube turns red.

screenshot.1468527501

Moving the cube

It’s not very useful to have this cube surrounding our point of view – it makes more sense to have this sitting out in front of our point of view.

The easiest way to move the cube is to use the draggable axis which point outwards from the visible faces of the block. I clicked on the blue arrow – corresponding to the Z-direction – and dragged it forward about 3.5 units.

screenshot.1468532252

Just to make this block a little more visually interesting, I’d like to rotate it about its axes. To do this, I click on the rotate button in the top left hand corner (it’s the third button in the group of five, and is selected in the image below). The red cube now has a set of circles surrounding it, rather than the three arrows. You can click on these circles, and drag them to rotate the cube, as shown below.

screenshot.1468530487.png

That’s about it for the first section. You can preview what you’re going to see through the HoloLens by clicking on the Play button in the top-centre of the screen, which will show something like the screen below. The rotated cube floats in a black world, directly in front of our point of view.

Finally I saved the scene by hitting Ctrl+S, and typed in HelloWorld – you can see this in the assets panel.

screenshot.1468527590

Create a C# script to make the object rotate

Let’s take the complexity up a notch. We can write C# scripts and apply them to objects in our virtual world.

It’s very straightforward to create a script – right click on the Assets note in the Projects panel, and create a C# script from the context menus, as shown below.

screenshot.1468527606

I created a script called RotatorScript. To edit this, I double click on it. This opens VS2015 for me, though on your install it might open MonoDevelop.

I entered the code below:

using UnityEngine;
 
public class RotationScript : MonoBehaviour {
 
	public float YAxisRotationSpeed;
 
	// Update is called once per frame
	void Update () {
            this.transform.Rotate(0, YAxisRotationSpeed * Time.deltaTime, 0Space.Self);
	}
}

The code above does one thing – each time the frame is updated by the rendering engine, the object that the script is applied to rotates a little around it’s own axes. Specifically in this case, I’ve specified the X-axis rotation and Z-axis rotation to be zero, and the rotation around the Y-axis will be YAxisRotationSpeed degrees per second.

The code above refers to Time.deltaTime – this is a built in Unity function to tell us how long it has been since the last frame. Therefore if we multiply the speed – YAxisRotationSpeed – by the amount of time that passed – Time.deltaTime – the result is the number of degrees to rotate our cube by.

Once I saved the script in Visual Studio, I switched back to Unity. I selected my Cube in the Hierarchy panel, and then dragged the RotationScript to the Inspector for the Cube. In the property page which appears in the Inspector, I changed the value of the “Y Axis Rotation Speed” to be 50.

screenshot.1468527833

Now when I click on the Play button in Unity, I’m able to see the Game view of the scene again, but this time the cube is rotating about its own Y-axis.

screenshot.1468527888

Hello World!

It occurred to me that with the simple skills learned in this post that I could do something quite interesting with Unity – instead of a rotating cube, I could add a sphere to the scene, apply a material which was an image of Earth, and show a rotating globe, which would be a much more appropriate “Hello, World” project. I could even add a second sphere to rotate around this one, which could represent the Moon.

I’m sure I’m not the first person to do this in Unity in a blog post – I’m sure there are many like it, but this one is mine.

  • As a first step, I clicked on the Cube object in my hierarchy and deleted it. This removed the red cube from my scene.
  • Next, I right-clicked on the Hierarchy panel, and selected “Create Empty”. This created an empty GameObject to the hierarchy.
  • Using the Transform panel in the Inspector for the GameObject, I changed the Z-position to be 4, thus placing the GameObject 4m in front of my point of view.

screenshot.1468535169

  • Next, I right clicked on the GameObject in the Hierarchy and added a sphere 3d Object. I renamed this “Earth”, and changed the X, Y, and Z scale values to be 2 (i.e. doubling its size). Notice how this is indented under GameObject, and also how its position in the Transform box in the Inspector is at (0, 0, 0). This means that its centre is at the origin of the parent GameObject, and changes to the position will move it relative to the parent GameObject.

screenshot.1468535345

  • Following this, I right clicked on the GameObject in the Hierarchy again, and added another 3d sphere – I named this object “Moon”, and changed the X, Y, and Z scale values to be 0.5 (i.e. halving its size). I also changed the X-position value to be 2, thus moving its centre 2m to the right of the centre of the “Earth” object.

screenshot.1468535584

  • Finally for this part, I selected the parent GameObject in the Hierarchy view, and dragged the “RotationScript” to the Inspector surface. In the property page which appears on the Inspector, I change the “Y Axis Rotation Speed” to be 50.

When I hit the Play button, I can see the animation rendered, and show a scene from this below.

screenshot.1468535828

I can see that both objects are rotating correctly – the larger central sphere is rotating about its own central vertical axis, and the smaller sphere is orbiting that same axis. However, it doesn’t look very good with the default white colour. I can improve this by using some free assets from the Unity Asset Store.

Downloading assets from the Unity Asset Store

I searched the Unity Asset store through a browser – at http://www.assetstore.unity3d.com – for free renderings of Earth, and found the resource shown below (and linked to here).

screenshot.1468536101.png

I clicked on the “Open in Unity” button, and this switched my in-focus application to Unity. The Asset Store tab was open, and I was able to click on the “Download” button to acquire this resource (I did see a compatibility warning about how this was created with Unity 4). After a couple of pop-ups, I was shown the window below and chose to import one of the Earth material files, shown below.

screenshot.1468537055

After clicking on the “Import” button, this jpeg file appeared in my list of Assets, using its original directory structure.

I was able to select this from the Assets/EarthSimplePlanets/Textures folder in the Project panel, and drag the “EarthSimple1.jpg” file to the Inspector surface for the Earth sphere, and, the surface of this sphere updates to look much more like more characteristic world.

Finally, I selected the GameObject from the Hierarchy, and tilted the Z-axis by -15 degrees, to give a slight planetary tilt. After hitting the Play button, the animation shows a white sphere rotating around a planet.

We could enhance this further by downloading more assets from the store for the Moon – a good candidate is the moon landscape linked to here – but for right now, I think this will look pretty good in our HoloLens mixed reality world.

screenshot.1468573301

Wrapping up

That’s it for this post – so far, we’ve:

  • created a new project with Unity,
  • added some primitive objects to this world,
  • changed these object’s colour with materials,
  • added a C# script to make this object move,
  • arranged objects to make them orbit an axis outside the object, and
  • used the Unity Asset Store to download assets that make our model more realistic.

Next time, we’ll talk about actually deploying to the HoloLens emulator – there are a few tips and gotchas that I want to share to make other people’s journey a little smoother than mine.

.net, HoloLens, Unity, UWP, Visual Studio

Coding for the HoloLens with Unity 5 – Part #1: Setting up your environment

I’m starting a new series of posts on coding for the HoloLens with Unity. There’s relatively few posts on how to code for this device, and the ones that I’ve found so far haven’t been as complete as I’d like them to be, and this series will be part tutorial and part lab-notes for myself.

So far, I’ve planned for the first 3 posts in the series.

  • Part 1 – Getting started – downloading and installing the software;
  • Part 2 – Creating a project in Unity 5, and adding a simple object to a virtual world;
  • Part 3 – Deploying this project to the HoloLens emulator.

I’ll follow these introductory posts with some more complex and interesting projects.

Getting started – downloading and installing the software

You don’t need a physical HoloLens to develop software for the HoloLens (although it would help). You can develop your app using available tools, and test it on an emulator – as long as you have Windows 10 Professional. The reason for this is that the emulator uses Hyper-V, which is available on Window 10 Pro – it’s not available on the home edition.

Hardware requirements

There are detailed hardware requirements at this link – key requirements are a 64-bit CPU with 4 cores, 8GB of RAM, and hardware assisted virtualisation enabled in the BIOS settings – Microsoft provides a guide on how to do this here.

Software downloads

You’ll need to install 4 software components – VS2015.3, the HoloLens emulator, Unity 5, and the UWP runtime for Unity.

The first development tool you’ll need is the IDE – Visual Studio 2015, Update 3. This is freely available as the Community Edition. You can get this here.

Next, you’ll need the HoloLens emulator to test your apps – you can get the emulator here (v10.0.14342.1018). This is a simple executable, which will check to see if your computer is suitable for HoloLens development while it’s installing.

You’ll need two pieces of Unity software to develop and deploy using Unity 5. These are both available from the Unity site at this link.

  • The first item to download and install will be the Unity Editor for the HoloLens Technical Preview. There’s a special edition for the HoloLens, and you can choose to go with either a 32-bit version or a 64-bit version.
  • The second item is the UWP Runtime for Unity – this allows you to build your applications as a Universal Windows Platform application, which is necessary to deploy and run on a HoloLens.

You’ll need to register as a Unity developer, but there’s no charge for this if you’re using the personal edition option.

Next time I’m going to look at writing my first Unity 3d application, where I’ll create a simple object in a virtual world.