適用于 HoloLens 的 WinRT Api 搭配 UnityWinRT APIs with Unity for HoloLens

此頁面說明如何在您的 Unity 專案中,將 WinRT Api 用於 HoloLens。This page describes how to make use of WinRT APIs in your Unity project for HoloLens.

混合現實 ApiMixed Reality APIs

Windows SDK 的混合現實焦點子集已在 .NET Standard 2.0 相容投射中提供,您可以在專案中使用,而不需要預處理器指示詞。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. Windows 中大部分的 Api。Most APIs in the Windows. 認知和 Windows. 輸入. 空間命名空間包含在內,未來可能會擴充以包含其他 Api。Perception and Windows.UI.Input.Spatial namespaces are included and may expand to include additional APIs in the future. 在編輯器中執行時,可以使用所投射的 Api,以啟用 播放模式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:

  1. 使用適用于 Unity 的 nuget來新增MixedReality DotNetWinRT nuget 套件的參考。Add a reference to the Microsoft.Windows.MixedReality.DotNetWinRT NuGet package using NuGet for Unity.
  2. 命名空間的前置詞參考 Windows Microsoft.Prefix references to the Windows namespace with Microsoft.:
using namespace Microsoft.Windows.Perception.Spatial;
  1. 以下列內容取代原生指標轉換 FromNativePtrReplace native pointer casts with FromNativePtr:
var worldOrigin = SpatialCoordinateSystem.FromNativePtr(unityWorldOriginPtr);

有條件地包含 WinRT API 呼叫Conditionally include WinRT API calls

您也可以使用預處理器指示詞,在針對通用 Windows 平臺和 Xbox One 平臺建立的 Unity 專案中使用 WinRT Api。You can also use the WinRT APIs in Unity projects built for the Universal Windows Platform and Xbox One platform by using preprocessor directives. 您在以 WinRT Api 為目標的 Unity 腳本中撰寫的任何程式碼,都必須有條件地包含在這些組建中。Any code that you write in Unity scripts that target WinRT APIs must be conditionally included for only those builds.

這可以透過 Unity 中的兩個步驟來完成:This can be done via two steps in Unity:

  1. API 相容性層級必須設定為 [播放機] 設定中的 [ .net 4.6 ] 或 [ .NET Standard 2.0 ]API compatibility level must be set to .NET 4.6 or .NET Standard 2.0 in the player settings
    • 編輯 > 專案設定 > 播放機 > 設定 > 將 Api 相容性層級 設為 .net 4.6.NET Standard 2.0Edit > Project Settings > Player > Configuration > Api Compatibility Level to .NET 4.6 or .NET Standard 2.0
  2. 預處理器指示詞 ENABLE_WINMD_SUPPORT 必須包裝在任何 WinRT 的程式碼周圍The preprocessor directive ENABLE_WINMD_SUPPORT must be wrapped around any WinRT-leveraged code

下列程式碼片段來自 Unity manual 頁面,適用于 c # 腳本中的通用 Windows 平臺: WINRT APIThe following code snippet is from the Unity manual page for Universal Windows Platform: WinRT API in C# scripts. 在此範例中,會傳回廣告識別碼,但只會傳回 UWP 和 Xbox One 組建: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
    }
}

在 Unity c # 專案中編輯您的腳本Edit your scripts in a Unity C# project

當您按兩下 Unity 編輯器中的腳本時,它預設會在編輯器專案中啟動您的腳本。When you double-click a script in the Unity editor, it will by default launch your script in an editor project. WinRT Api 會顯示為未知,因為 Visual Studio 專案未參考 Windows 執行階段。The WinRT APIs will appear to be unknown because the Visual Studio project doesn't reference the Windows Runtime. ENABLE_WINMD_SUPPORT 指示詞未定義,而且會忽略任何 #if 包裝程式碼,直到您將專案建立到 UWP Visual Studio 解決方案為止。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.

另請參閱See also