Version: 5.6
编译器选项
IL2CPP 的工作原理

Windows 运行时支持

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

PlayerSettings 窗口的 Configuration 部分。上面显示的选项会根据选择的构建平台而变化。
PlayerSettings 窗口的 Configuration 部分。上面显示的选项会根据选择的构建平台而变化。

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.

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

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 时进行定义。

PlayerSettings Inspector 窗口的 Publishing Settings 部分,其中以红色突出显示了 Compilation Overrides
PlayerSettings Inspector 窗口的 Publishing Settings 部分,其中以红色突出显示了 Compilation Overrides
编译器选项
IL2CPP 的工作原理