文档

Unity 引擎

Unity 引擎

适用于 UWP 的 C# 脚本中的 WinRT API

直接在 Unity 脚本使用 WinRT API for UWP。
阅读时间1 分钟最后更新于 6 天前

使用 IL2CPP Scripting Backend 构建通用 Windows 平台时,Unity 包含 Windows 运行时 (WinRT) Support。使用 Windows 运行时支持可以直接从Managed C# 脚本和Plugins调用原生System的 Windows 运行时 API 以及自定义 .winmd 文件。
Unity 自动引用 Windows 运行时 API,例如 ON 通用 Windows 平台的
Windows.winmd
。要使用自定义定义 .winmd 文件,请将它们与Any随附的 DLL 全体学员导入 Unity ProjectFolder并进行配置。有关更多信息,请参阅导入和配置插件。
要在 Unity 脚本使用 WinRT API,请执行以下操作:
  • 脚本必须用 C# 编写。
  • 使用 WinRT API 的所有代码都必须在
    ENABLE_WINMD_SUPPORT
    指令下。这是必要的,因为 Editor 使用不支持 WinRT API 的 Mono。
以下代码示例直接使用 WinRT API 获取广告 ID:
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 }}

其他资源