How to Resolve "The type or namespace name 'Windows' could not be found" Error in Unity for UWP/HoloLens Development?

Background:
I am developing an application for HoloLens 2 using Unity 2022.3.30f1, and I need to use the       `Windows. Media` API to capture depth and image data from the HoloLens Research Mode. My project is set up to use Universal Windows Platform (UWP) as the target platform, and I have the `Mixed Reality Toolkit (MRTK) Foundation 2.8.3` and `MRTK OpenXR Plugin 1.10.1` installed.

When I try to use the Windows namespace in my script, I encounter the following error in Unity:

The type or namespace name 'Windows' could not be found (are you missing a using directive or an assembly reference?)

Steps Taken:
  1. Installed Universal Windows Platform Workload: I ensured that the "Universal Windows Platform development" workload is installed in Visual Studio.

  2. Switched Platform: Switched the target platform to UWP in Unity.

  3. API Compatibility Level: Set the API Compatibility Level to .NET Standard 2.1.

  4. Added References: Added references to Windows.winmd and System.Runtime.WindowsRuntime.dll in the 5) Unity project: Windows.winmd from C:\Program Files (x86)\Windows Kits\10\UnionMetadata
    System.Runtime.WindowsRuntime.dll from C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETPortable\v4.6

    Code1 Code2 Code3

    Questions:

1 - Have I missed any steps or configurations that are necessary to resolve this issue? 2 - Are there specific settings or additional steps required to make Unity recognize the Windows namespace? 3 - Is there a better way to integrate the Windows.Media API in a Unity project targeting HoloLens 2? Additional Context:

Unity Version: 2022.3.30f1 Visual Studio: 2019/2022 with UWP workload installed HoloLens SDKs: Installed Scripting Backend: IL2CPP Target Device: HoloLens 2 Any insights or suggestions would be greatly appreciated!.png)

Upvotes: 0

Views: 178

Answers (1)

Ying Li - MSFT
Ying Li - MSFT

Reputation: 179

You should wrap around any WinRT-leveraged code in the ENABLE_WINMD_SUPPORT compiler directive to only use certain code when compiling against UWP. For more details, please see:

https://docs.unity3d.com/Manual/IL2CPP-WindowsRuntimeSupport.html

https://learn.microsoft.com/en-us/windows/mixed-reality/develop/unity/using-the-windows-namespace-with-unity-apps-for-hololens

Upvotes: 1

Related Questions