Unity includes Windows Runtime support for IL2CPP on Windows Store and Xbox One platforms. Use Windows Runtime support to call into both native system Windows Runtime APIs as well as custom .winmd files directly from managed code (scripts and DLLs).
要在 IL2CPP 中自动启用 Windows 运行时支持,请访问 PlayerSettings (Edit > Project Settings > Player),导航到 Configuration 部分,并将 Api Compatibility Level 设置为 .NET 4.6。
Unity automatically references Windows Runtime APIs (such as Windows.winmd on Windows Store) when it has Windows Runtime support enabled. To use custom .winmd files, import them (together with any accompanying DLLs) into your Unity project folder. Then use the Plugin Inspector to configure the files for your target platform.
In your Unity project’s scripts you can use the ENABLE_WINMD_SUPPORT
#define directive to check that your project has Windows Runtime support enabled. Use this before a call to .winmd Windows APIs or custom .winmd scripts to ensure they can run and to ensure any scripts not relevant to Windows ignore them. See the examples below.
示例
C#
void Start() {
#if ENABLE_WINMD_SUPPORT
Debug.Log("Windows Runtime Support enabled");
// 在此处放置对自定义 .winmd API 的调用
#endif
}
JS
function Awake() {
#if ENABLE_WINMD_SUPPORT
Debug.Log("Windows Runtime Support enabled");
// Put calls to your custom .winmd API here
#endif
}
除了在 IL2CPP 中启用 Windows 运行时支持时进行定义,也可在 .NET 中将 Compilation Overrides 设置为 Use Net Core 时进行定义。