---
title: Managed 代码剥离和 Unity 链接器
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/managed-code-stripping/unity-linker
latest_url: https://docs.unity.com/zh-cn/engine/7000.0/manual/scripting/compilation-and-code-reload/script-compilation/managed-code-stripping/unity-linker.md
generated_at: 2026-11-16T12:18:33.848Z
locale: zh-cn
---

# Managed 代码剥离和 Unity 链接器

> 了解 Unity 链接器如何在剥离之前分析和标记代码元素。

Unity 版本过程使用一个名为 Unity 链接器的工具剥离 Managed 代码。Unity 链接器是为与 Unity 配合使用的 [IL Linker](https://github.com/mono/linker) 版本。为 Unity 引擎指定的 Unity 链接器的自定义部分不公开。

Unity 链接器负责 Managed 代码剥离和引擎代码剥离过程的一部分，这是通过 IL2CPP Scripting Backend 提供的单独过程，用于删除未使用的引擎代码。有关更多信息，请参阅[`PlayerSettings.StripEngineCode`](/engine/6000.0/script-reference/unityeditor/playersettings/stripenginecode.md)。

## Unity 链接器如何剥离程序集##how-the-unity-linker-strips-assemblies

生成 Unity ProjectWhen，生成过程会将 C# 代码编译为称为通用中级语言 (CIL) 的 .NET 字节码Format式。Unity 将此 CIL 字节码打包为称为程序集的文件。.NET 框架库以及您在Project中使用的插件中的Any C# 库也预先打包为 CIL 字节码的程序集。

出于Managed代码剥离的目的，程序集按如下方式分类：

* **.NET 类库程序集**：包括 Mono 类库（如 `mscorlib.dll` 和 `System.dll`）以及 .NET 类库外观程序集（如 `netstandard.dll`）。

* **平台 SDK 程序集**：包括平台 SDK 指定的 Managed 程序集。例如，作为通用 Windows 平台 SDK 一部分的`windows.winmd`程序集。

* **Unity 引擎模块程序集**：包括组成 Unity 引擎的 Managed 程序集，例如 `UnityEngine.Core.dll`。

* **Project 程序集**：包括 Project 指定的程序集，例如：

  * 脚本程序集，例如 `Assembly-CSharp.dll`。
  * 预编译的程序集。
  * [Assembly Definition 程序集](https://docs.unity3d.com/Manual/assembly-definition-files.html)。
  * 包序集。

General，Project中不属于这些类别之一的Any程序集都不会由 Unity 链接器进行处理，也不会从 Player 构版本排除。

在版本期间，Unity 链接器会处理All适用类型的程序集并执行以下操作：

1. 根据适用于程序集类型和 Configured 代码剥离关卡的[根标记规则](/engine/6000.0/manual/scripting/compilation-and-code-reload/script-compilation/managed-code-stripping/marking-rules.md#root-marking-rules)标记根类型、方法、Properties 和字段。
2. 分析已标记的根以进行识别，并根据适用的[依赖关系标记规则](/engine/6000.0/manual/scripting/compilation-and-code-reload/script-compilation/managed-code-stripping/marking-rules.md#dependency-marking-rules)标记这些根所依赖的任何 Managed 代码。
3. 从程序程序集中删除Any剩余的未标记为无法通过应用程序程序代码的Any执行路径访问的代码。

## 方法体的编辑##editing-of-method-bodies

在**高**[剥离关卡](/engine/6000.0/manual/scripting/compilation-and-code-reload/script-compilation/managed-code-stripping/configure.md)中，Unity 链接器编辑方法体以进一步减小代码大小。Unity 链接器仅编辑 .NET 库序集的方法体。

Unity 链接器可以通过以下方式编辑方方法体：

* 移除无法访问的树枝：`if` 语句块，用于检查`System.Environment.OSVersion.Platform`且当前目标平台无法访问。
* 仅访问字段的内联方法：将获取或设置 Field 的方法的 Calls 替换为直接访问 Field。这通常使完全剥离方法成为可能。当您使用 Mono Scripting Backend 时，Unity 链接器仅在允许该方法的调用者根据 Field 的可见性直接访问 Field 时才会进行此 Change。对于 IL2CPP，可见性性Rules不Apply，因此 Unity 链接器会酌情进行此Change。
* 仅返回 `const` 值的内联方法。
* 移除对空且具有`void` return type.的方法的 Calls
* 移除`try-finally`代码块 When the `finally` code块为空。删除空 Calls 可以创建空对象`finally`块。在方法编辑期间发生这种情况时，Unity 链接器会删除整个`try-finally`代码块。发生这种情况的一种情况是 When the compiler generated `try-finally` blocks as part of `foreach` loops, order to call `Dispose`.

> **Note:**
>
> 编辑方法体后，程序集的音频源代码不再与程序集中的编译代码匹配，因此会使调试更加困难。

## 其他资源##additional-resources

* [使用 Annotations 保留代码](/engine/6000.0/manual/scripting/compilation-and-code-reload/script-compilation/managed-code-stripping/preserving.md)
* [标记规则参考](/engine/6000.0/manual/scripting/compilation-and-code-reload/script-compilation/managed-code-stripping/marking-rules.md)
