---
title: IL2CPP 简介
product: Unity Editor
version: '6000.0'
latest_version: '7000.0'
status: This page documents version 6000.0. The latest version is 7000.0.
canonical: https://docs.unity.com/zh-cn/engine/6000.0/manual/scripting/compilation-and-code-reload/script-compilation/backends/il2cpp/il2cpp-introduction
latest_url: https://docs.unity.com/zh-cn/engine/7000.0/manual/scripting/compilation-and-code-reload/script-compilation/backends/il2cpp/il2cpp-introduction.md
generated_at: 2026-11-20T21:04:22.718Z
locale: zh-cn
---

# IL2CPP 简介

> 了解 IL2CPP 在 Unity 脚本编译中的角色及其工作原理。

IL2CPP(C++ 中级语言)是 Unity 的自定义提前 (AOT) Scripting Backend，最初是为不支持 [Mono](/engine/6000.0/manual/scripting/compilation-and-code-reload/script-compilation/backends/mono.md) 和即时 (JIT) 编译的平台（例如 iOS）开发的。IL2CPP 将 IL（[中级语言](https://learn.microsoft.com/en-us/dotnet/standard/managed-code#intermediate-language--execution)）转换为 C++，并将其编译为平台指定的原生代码，然后以 Target 平台的标准 Format 与应用程序打包，例如 APK/AAB、iOS 应用程序捆绑包或 Windows 可执行文件和 DLL。

IL2CPP ONAll平台上都受支持，并且与 Mono Scripting Backend相比具有多种优势，包括改进性能能和缩短启动时间。但是，需要在构建的Applications程序中包含机器代码，通常会增加版本时间和最终构建的应用程序程序的大小。

IL2CPP 支持以与 Mono 相同的方式调试Managed代码。有关更多信息，请参阅[在 Unity 中调试](/engine/6000.0/manual/scripting/debugging-and-diagnostics/managed-code-debugging.md) C# 代码。

## 使用 IL2CPP 构构建Project##building-a-project-with-il2cpp

要使用 IL2CPP 构版本项Project，必须在 Unity 安装过程中安装 IL2CPP。首次安装 Unity 版本When，可以选择 IL2CPP 作为可选模块，也可以通过 Unity Hub 将 IL2CPP Support添加到现有安装中。有关更多信息，请参阅[安装 Unity Hub](https://docs.unity.com/hub/install-hub.html.md) 并向 Unity Editor [添加模块](https://docs.unity.com/hub/add-moduless.html.md)。

IL2CPP 还需要 Target 平台原生的一些系统来 Generate C++ 代码。这意味着通常不支持交叉编译，要针对特定Target平台生成 IL2CPP Player，必须从ON同一平台running的 Editor 进行生成。例如，要使用 IL2CPP 生成 macOS Player，必须ON macOS 上生成。Linux 是异常，它支持ONOther桌面平台上构建 Linux 玩家器。有关更多信息，请参阅 [Linux IL2CPP 交叉编译器](/engine/6000.0/manual/scripting/compilation-and-code-reload/script-compilation/backends/il2cpp/linux-il2cpp-crosscompiler.md)。

您可以通过两种方式之一Change Unity 用于版本应用程序程序的Scripting Backend：

* 通过 Editor 中的 Editor 菜单。执行以下步骤可通过 **Player Settings** 菜单更改 Scripting Backend：

  1. 转到**编辑** > Editor。
  2. 单击 ON **Player Settings** 按钮可在 [**Inspector**](/engine/6000.0/manual/unity-editor/editor-windows-views-reference/using-the-inspector.md) 中打开当前平台的 [**Player**](/engine/6000.0/manual/unity-editor/editor-settings-reference/comp-manager-group/class-player-settings.md) Settings。
  3. 导航到 **Other Settings** 子菜单下的 **Configuration** 部分标题。
  4. 单击 ON Editor 下拉菜单，然后选择 Editor。

  还可以从 **版本配置文件**窗口打开 **Player Settings** 菜单；转到 **文件** > **版本配置文件**，然后单击 ON **Player Settings** 选项卡。

* 通过 Editor 脚本 API。使用 [`PlayerSettings.SetScriptingBackend`](/engine/6000.0/script-reference/unityeditor/playersettings/setscriptingbackend.md) 属性可更改 Unity 使用的 Scripting Backend。


**The Configuration section of the Player settings:**
![](/api/media?file=/engine/6000.0/media/images/IL2CPP-build-settings.png)

有关桌面平台的 System 要求（包括各个平台的 IL2CPP 要求）的更多信息，请参阅 [System Requirements for Unity](https://docs.unity3d.com/Manual/system-requirements.html#desktop) 的 Desktop 部分。

## IL2CPP 的工作原理##how-il2cpp-works

使用 IL2CPP 进行版本When，Unity 会自动执行以下步骤：

1. Roslyn C# 编译器器将应用程序程序的 C# 代码和Any必需的包代码译为 .NET DLL（Managed程序集）。
2. Unity 应用[托管代码剥离](/engine/6000.0/manual/scripting/compilation-and-code-reload/script-compilation/managed-code-stripping.md)。此步骤可以显著减小构建的应用程序程序的大小。
3. IL2CPP 后端将AllManaged程序集转换为标准 C++ 代码。
4. C++ 编译器器使用原生平台编译器器编译生成的 C++ 代码 IL2CPP 的运行时时部分。
5. Unity 根据 Target 的平台创建可执行文件或 DLL。

## 优化 IL2CPP 版本时间##optimizing-il2cpp-build-times

使用 IL2CPP 的Project版本时间可能明显长于使用 Mono。但是，您可以执行以下几项操作来减少版本时间：

* 在生成Project之前，从反恶意软件扫描中排除 Unity Project和Target生成文件夹。
* 将Project和Target版本文件Folder存储ON可用的最快磁盘驱动器上。将 IL 代码转换为 C++ 并进行编译涉及数量读/写操作，因此更快的存储设备会加快此过程。
* 在 Player Settings 中Change IL2CPP 代码 生成 选项。要更改 IL2CPP 生成代码的方式，请打开 [Player Settings](/engine/6000.0/manual/unity-editor/editor-settings-reference/comp-manager-group/class-player-settings.md) 并配置 IL2CPP 代码生成选项。默认情况下，已启用 **Optimization for运行时速度** 选项，这将产生更多机器代码，从而减少 IL2CPP 在运行时的影响。为了减少生成时间，可以设置此选项以**优化代码大小和生成时间**。此方法在 Binary 可执行文件中生成并包含更少的机器代码，因此可能会在运行时降低性能，但也会显著减少版本时间和 Binary 大小。

## 使用 爆发 优化运行时性能##optimizing-runtime-performance-with-burst

在合适的Projects中，可以通过将 爆发 编译器与 IL2CPP 结合使用来提高Project的运行时性能，从而将兼容的代码部分编译为高度优化的机器代码。有关更多信息，请参阅[爆发编译](/engine/6000.0/manual/scripting/compilation-and-code-reload/script-compilation/burst.md)。

## 其他资源##additional-resources

* ��� **文档**： [IL2CPP 编译器选项参考](/engine/6000.0/manual/il2cpp-options-reference.md)
* ��� **文档**： [Linux IL2CPP 交叉编译器](/engine/6000.0/manual/scripting/compilation-and-code-reload/script-compilation/backends/il2cpp/linux-il2cpp-crosscompiler.md)
* ��� **文档**： [减小版本的文件大小](/engine/6000.0/manual/building-and-publishing/reducing-filesize.md)
* 👥 **社区**： IL2CPP 内部组件[简介。](https://blog.unity.com/technology/an-introduction-to-ilcpp-internals)
