Unity 包含对通用 Windows 平台和 Xbox One 平台上的 IL2CPP 的 Windows 运行时支持。通过使用 Windows 运行时支持,可直接从托管代码(脚本和 DLL)调用本机系统 Windows 运行时 API 以及自定义的 .winmd 文件。
要在 IL2CPP 中自动启用 Windows 运行时支持,请访问 Player 设置(__Edit__ > Project Settings__,然后选择 Player__ 类别),导航到 Configuration 部分,并将 Api Compatibility Level 设置为 .NET 4.6 或 .NET Standard 2.0。
启用 Windows 运行时支持后,Unity 会自动引用 Windows 运行时 API(例如,通用 Windows 平台上的 _Windows.winmd_)。要使用自定义 .winmd 文件,请将它们(以及所有附带的 DLL)导入 Unity 项目文件夹。然后,使用 Plugin Inspector 为目标平台配置这些文件。
在 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");
// 在此处放置对自定义 .winmd API 的调用
#endif
}
• 2017–05–16 页面已修订
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.