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.


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.


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.


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).


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.


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.


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”.


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.


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.


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.


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.


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.


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.


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.


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.


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.


  • 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.


  • 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.


  • 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.


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 – for free renderings of Earth, and found the resource shown below (and linked to here).


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.


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.


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.