启用 Windows 运行时支持后,可直接在 Unity 脚本中使用 WinRT API。有关如何使用 WinRT API 和启用 Windows 运行时支持的信息,请参阅 Windows 运行时支持。
您需要满足以下要求才能在 Unity 脚本中使用 WinRT API:
ENABLE_WINMD_SUPPORT 指令下。这是必要的,因为编辑器使用 Mono,它不支持 WinRT API。以下代码示例演示了如何使用 WinRT API 直接投放广告:
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
}
}