文档

Unity Engine

Unity Engine

Windows 运行时支持

Call native Windows Runtime APIs and custom .winmd files from managed code using IL2CPP on Universal Windows Platform.
阅读时间2 分钟最后更新于 1 个月前

Unity 包含对通用 Windows 平台上的 IL2CPP 的 Windows 运行时支持。通过使用 Windows 运行时支持,可直接从托管代码(脚本和 DLL)调用本机系统 Windows 运行时 API 以及自定义的 .winmd 文件。
要在 IL2CPP 中自动启用 Windows 运行时支持,请访问播放器 (Player) 设置(编辑 (Edit) > 项目设置 (Project Settings),然后选择__播放器__ (Player) 类别),导航到__配置__ (Configuration) 部分,并将 Api 兼容性级别 (Api Compatibility Level) 设置为 .NET 4.6 或 ..NET Standard 2.0。

**播放器 (Player) **设置的配置 (Configuration) 部分。上面显示的选项会根据选择的构建平台而变化。

启用 Windows 运行时支持后,Unity 会自动引用 Windows 运行时 API(例如,通用 Windows 平台上的 Windows.winmd)。要使用自定义 .winmd 文件,请将它们(以及所有附带的 DLL)导入 Unity 项目文件夹。然后,使用 Plugin Inspector 为目标平台配置这些文件。

使用 Plugin Inspector 为特定平台配置自定义的 .winmd 文件

在 Unity 项目的脚本中,可使用
ENABLE_WINMD_SUPPORT
#define 指令来检查项目是否启用了 Windows 运行时支持。应在调用 .winmd Windows API 或自定义 .winmd 脚本之前使用此选项,从而确保它们可以运行并确保与 Windows 无关的所有脚本都会忽略它们。请注意,这仅在 C# 脚本中受支持。请参阅以下示例。
示例
C#
void Start() { #if ENABLE_WINMD_SUPPORT Debug.Log("Windows Runtime Support enabled"); // Put calls to your custom .winmd API here #endif}

其他资源