We have been exploring Hololens in terms of Direct X usage (trust me there is lot to cover when we go back to Direct X) now we will change focus to Unity. The structuring of an app is similar to adding game objects in Unity scene and then we take it forward using Holographic features.
The ground rules of development
Relax and follow how you have been developing apps for Unity on a long term basis and you don't have to do a fancy thing that is out of the blue. When you are developing for Hololens, you will have to keep a tap and the build should be targeted for UWP.
Things that are required
- Unity Microsoft Hololens technical review.
- UWP tools for Unity technical review.
- Visual Studio 2015 Update 2.
Let's get started,
Shoot up Unity
Open the project Origami or a new project as I had been using this.
Open a new Scene,
Things that we need to change.
Make sure in the main Camera Settings at the inspector we put in position values as
X=0,Y=0,Z=0,
On the camera options we choose
Clear flags to solid color and
Background as RGBA all set to 0.
Now we create an empty object, name it CSharpCorner as more of our projection will happen in this empty object, we need to adjust where this CSharpCorner object is lying.
We position at X = 0, Y=-0.5,Z=2
We create a new game object Plane and put it as child of CSharpCorner,
It looks like this,
We will use 3d Object creation and make 3 spheres,
Now we create a C# Script for rotation, The script looks like this it allows rotation around y axis.
- using UnityEngine;
- using System.Collections;
- public class Rotate : MonoBehaviour {
-
- void Start () {
- }
-
- void Update () {
-
- transform.Rotate(Vector3.right * Time.deltaTime);
-
-
- transform.Rotate(Vector3.up * Time.deltaTime, Space.World);
-
- }
- }
Now we drag the rotate script to all the sphere objects, We will use World Cursor it allows us to Gaze through the objects and now we add a C# script to it.
We drag World Cursor Script to the Cursor object,
- using UnityEngine;
- using System.Collections;
-
- public class WorldCursor : MonoBehaviour {
- private MeshRenderer meshRenderer;
-
-
- void Start () {
-
- meshRenderer = this.gameObject.GetComponentInChildren<MeshRenderer>();
-
- }
-
-
- void Update () {
-
-
- var headPosition = Camera.main.transform.position;
- var gazeDirection = Camera.main.transform.forward;
-
- RaycastHit hitInfo;
-
- if (Physics.Raycast(headPosition, gazeDirection, out hitInfo))
- {
-
-
- meshRenderer.enabled = true;
-
-
- this.transform.position = hitInfo.point;
-
-
- this.transform.rotation = Quaternion.FromToRotation(Vector3.up, hitInfo.normal);
- }
- else
- {
-
- meshRenderer.enabled = false;
- }
-
- }
- }
The script creates the interaction technique with the cursor for our orientation,
After the changes we create the build for UWP platform,
With successful build, we open the solution in Visual Studio,
Compile it in Hololens Emulator
Let's see how it looks,
See the world cursor changes gaze as you move around the objects.
Conclusion
In this tutorial we have covered the
Gaze gesture in Unity from scratch and also checked on how we can create a scene for Hololens using Unity.