NB: The usual blog disclaimer for this site applies to posts around HoloLens. I am not on the HoloLens team. I have no details on HoloLens other than what is on the public web and so what I post here is just from my own experience experimenting with pieces that are publicly available and you should always check out the official developer site for the product documentation.
It’s testament to my fairly newbie status with Unity that I’m writing this post but I thought I’d jot it down regardless as I’ve been doing bits and pieces with Unity for holographic apps in the past few weeks and I keep coming back to the incredibly simple topic of;
“Ok, how do I add a reference to a library that makes calls into UWP APIs?”
Unity turns out to have an elegant approach to this which involves writing plugins as discussed in the docs over here;
and that includes options for code that can have the same ‘shape’ but different implementations across different players and even a different implementation for Unity’s editor so that the code can appear to be used in the editor when, in reality, different code is brought in at runtime. It reminds me a little of the plugin approach that I’ve seen in the MvvmCross framework and others.
My (newbie) recipe for setting that up is as below (using Visual Studio 2015 Update 3 and Unity 5.4.0f3-HTP). Note that in my case this has always been done as part of making a holographic app but I don’t think there’s anything specifically about holographic apps here.
1. Make a New UWP Class Library Project in Visual Studio
I called mine “MyGreatClassLibrary” as below;
2. Write some code
I wrote what seemed like a fairly minimal thing;
namespace MyGreatClassLibrary { using Windows.System.Profile; public static class MyGreatClass { public static string MyGreatMethod() { return (AnalyticsInfo.VersionInfo.DeviceFamily); } } }
3. Make a new Unity Project
Mine was 3D and I made it as below;
4. Make a Scene
I made a very simple scene with a 3D cube in it and I changed a few properties about the camera that Unity puts into the scene (i.e. the location, the clear flags, the near clipping plane).
5. Add the UWP Library
I’m not 100% sure on this but I made a Plugins folder and I dropped my (debug) assembly into it;
6. Set Properties on the UWP Library
I switched some properties on the Inspector for the library;
I think the “don’t process” part isn’t necessary here but I think it’s ok for my particular library based on what I read in the docs.
7. Write Some Code
I attached a new script to my Cube and wrote some code as below;
using UnityEngine; using System.Collections; public class MyGreatScript : MonoBehaviour { // Use this for initialization void Start () { #if !UNITY_EDITOR var platform = MyGreatClassLibrary.MyGreatClass.MyGreatMethod(); #endif } // Update is called once per frame void Update () { } }
8. Set up the Build Settings
I only changed what I thought was a minimal set of pieces here;
9. Build from Unity, Open in Visual Studio, Debug on Emulator/Device
And I see my UWP code running as I’d expect it to;
Although I’d admit that I haven’t given a lot of thought to symbols here and I didn’t specify a debug build in my Unity settings.
That’s it – I can now do the equivalent of ‘Add Reference’ in Unity and, of course, feel free to feedback and tell me there’s a much easier way of doing it!
Pingback: Windows 10, UWP, HoloLens and Switching 2D/3D Views – Mike Taulty