---
title: IL2CPP Managed 堆栈跟踪
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-managed-stack-traces
latest_url: https://docs.unity.com/zh-cn/engine/7000.0/manual/scripting/compilation-and-code-reload/script-compilation/backends/il2cpp/il2cpp-managed-stack-traces.md
generated_at: 2026-11-16T08:01:47.598Z
locale: zh-cn
---

# IL2CPP Managed 堆栈跟踪

> Configure IL2CPP 编译器 Settings 可生成更具信息性的 Managed 堆栈跟踪以便于调试。

当托管代码中发生异常时，异常的 [堆栈跟踪](/engine/6000.0/manual/scripting/debugging-and-diagnostics/stack-trace.md) 可以帮助您了解异常的原因。但是，托管 stack trace 在某些情况下可能无法按预期显示。stack trace根据版本配置ON有所不同。

## C++ 编译器配置##c++-compiler-configuration

您可以通过以下方式之一设置 IL2CPP 构建的 C++ 编译器配置：

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

  1. 转到**编辑** > Editor。
  2. 单击 ON **Player Settings** 按钮可在 [**Inspector**](/engine/6000.0/manual/unity-editor/editor-settings-reference/comp-manager-group/class-player-settings.md) 中打开当前平台的 [**Player**](/engine/6000.0/manual/unity-editor/editor-windows-views-reference/using-the-inspector.md) Settings。
  3. 导航到 **Other Settings** 子菜单下的 **Configuration** 部分标题。
  4. 将 **C++ 编译器配置**属性设置为 **调试**、**发布** 或 **master**。

* 从代码调用具有 [`Il2CppCompilerConfiguration`](/engine/6000.0/script-reference/unityeditor/playersettings/setil2cppcompilerconfiguration.md) enum 值的 [`PlayerSettings.SetIl2CppCompilerConfiguration`](/engine/6000.0/script-reference/unityeditor/il2cppcompilerconfiguration.md)。

Settings 对 Managed 堆栈跟踪具有以下影响：

* \--**debug**IL2CPP Reports 报告可靠的 Managed stack trace，并在 Call Stack 中包含每个 Managed 方法。stack trace不包含原始 C# 音频源代码中的线。
* **发布**或 **master**：IL2CPP 可能会生成缺失一个或多个 Managed 方法的 Call Stack。When C++ 编译器器内联缺失的方法时，会发生这种情况。方法内联通常可以提高运行时时性性能，但可能会make调用用栈更难理解。

IL2CPP 始终ONCall Stack提供至少一个Managed方法。对于从 Managed 异常创建的堆栈跟踪，这是发生异常的方法。如果未内联，还包括 Other 方法。

## IL2CPP stack trace信息##il2cpp-stack-trace-information

您可以通过以下方式之一配置 IL2CPP 以在 Managed 堆栈跟踪中包含文件和线数量信息：

* 通过 Editor 中的 Player Settings：**编辑** > **项目设置** > **Player** > **Other Settings**。然后，在 **Configuration** 标题下，将 **IL2CPP Stacktrace Information** 属性设置为 **Method Name**、File Name 和线数量设置。
* 从代码中调用以 [`Il2CppStacktraceInformation.MethodFileLineNumber`](/engine/6000.0/script-reference/unityeditor/playersettings/setil2cppstacktraceinformation.md) 为参数值的[`PlayerSettings.Il2CppStacktraceInformation`](/engine/6000.0/script-reference/unityeditor/il2cppstacktraceinformation.md)。


**The IL2CPP Stacktrace Information property, set to Method Name, File Name, and Line Number:**
![](/api/media?file=/engine/6000.0/media/images/IL2CPP-source-code-line-numbers.png)

此设置指示 IL2CPP 在 Call Stack 中包含 All Managed 堆栈帧。每个堆栈帧还包括适当的 C# 线数量，只要托管符号文件 (.pDB) 可用于存在该代码的 Managed 程序集 (.dll)。

When Enable this function（启用此功能）会略微增加版本时间和构建程序的最终大小。Player 构版本过程包括一个额外步骤，用于处理调试符号文件并生成包含必要符号形成的新建数据文件。Unity 将此数据文件与构建的 Player 一起提供，并在运行时时使用它来确定调用栈中的 C# 线信息。

当您启用此功能时，Unity 会在**发布**或 **master** 配置中生成正确的调用栈，即使方法内联为 Active。

## 脚本调试##script-debugging

要启用**脚本调试**，转到**文件** > **生成配置文件**，然后启用**脚本调试**复选框。启用脚本调试时，IL2CPP 会报告正确的 Managed stack trace 及其方法、文件和线数量。这样做的代价是程序大小增大和性能下降。

> **Tip:**
>
> 如果只想改善堆栈跟踪，请不要Enable脚本调试。请改用如上所述的 Enable [音频源代码线号](#source-code-line-numbers)。

## 其他资源##additional-resources

* [C++ 编译器配置](/engine/6000.0/script-reference/unityeditor/il2cppcompilerconfiguration.md)
