title | description | author | ms.author | ms.date | ms.topic | keywords |
---|---|---|---|---|---|---|
WinRT APIs with Unity for HoloLens |
Leanr how to make use of WinRT APIs and the Windows namespace in your Unity mixed reality projects for HoloLens. |
mikeriches |
mriches |
03/21/2018 |
article |
Unity, WinRT, windows mixed reality, API, walkthrough, mixed reality headset, windows mixed reality headset, virtual reality headset, Mixed Reality APIs |
This page describes how to make use of WinRT APIs in your Unity project for HoloLens.
A Mixed Reality focused subset of the Windows SDK has been made available in a .NET Standard 2.0 compatible projection, which you can use in your project without preprocessor directives. Most APIs in the Windows. Perception and Windows.UI.Input.Spatial namespaces are included and may expand to include additional APIs in the future. The projected APIs can be used while running in the Editor, which enables the use of Play Mode. To use this projection, make the following modifications to your project:
- Add a reference to the Microsoft.Windows.MixedReality.DotNetWinRT NuGet package using NuGet for Unity.
- Prefix references to the
Windows
namespace withMicrosoft.
:
using namespace Microsoft.Windows.Perception.Spatial;
- Replace native pointer casts with
FromNativePtr
:
var worldOrigin = SpatialCoordinateSystem.FromNativePtr(unityWorldOriginPtr);
You can also use the WinRT APIs in Unity projects built for the Universal Windows Platform and Xbox One platform by using preprocessor directives. Any code that you write in Unity scripts that target WinRT APIs must be conditionally included for only those builds.
This can be done via two steps in Unity:
- API compatibility level must be set to .NET 4.6 or .NET Standard 2.0 in the player settings
- Edit > Project Settings > Player > Configuration > Api Compatibility Level to .NET 4.6 or .NET Standard 2.0
- The preprocessor directive ENABLE_WINMD_SUPPORT must be wrapped around any WinRT-leveraged code
The following code snippet is from the Unity manual page for Universal Windows Platform: WinRT API in C# scripts. In this example, an advertising ID is returned, but only on UWP and Xbox One builds:
using UnityEngine;
public class WinRTAPI : MonoBehaviour {
void Update() {
auto adId = GetAdvertisingId();
// ...
}
string GetAdvertisingId() {
#if ENABLE_WINMD_SUPPORT
return Windows.System.UserProfile.AdvertisingManager.AdvertisingId;
#else
return "";
#endif
}
}
When you double-click a script in the Unity editor, it will by default launch your script in an editor project. The WinRT APIs will appear to be unknown because the Visual Studio project doesn't reference the Windows Runtime. The ENABLE_WINMD_SUPPORT directive is undefined and any #if wrapped code is ignored until you build your project into a UWP Visual Studio solution.