---
title: 代码剥离如何影响内容
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/content
latest_url: https://docs.unity.com/zh-cn/engine/7000.0/manual/scripting/compilation-and-code-reload/script-compilation/managed-code-stripping/content.md
generated_at: 2026-11-16T12:18:33.896Z
locale: zh-cn
---

# 代码剥离如何影响内容

> 了解Managed代码剥离配置如何影响内内容建以及如何保留内容ON依赖的代码。

当您在 Unity 中发布 Player 版本时，[UnityLinker](/engine/6000.0/manual/scripting/compilation-and-code-reload/script-compilation/managed-code-stripping/unity-linker.md) 进程会运行，通过删除未使用的类型来减小程序集的大小。此过程可以剥离以下内容：

* 整个 C# Unity 对象，例如 `MonoBehaviour` 和 `ScriptableObject` 实例。Unity 可以从核心 Unity 程序集（如 `UnityEngine.dll` 和 `Unity.dll`）中剥离这些程序集，但一旦这些程序集标记为已使用，就会将它们保留在自定义程序集内。
* 常规 C# 结构和类。
* 类的一部分（例如，未使用的方法）。
* 整个 Unity 模块，例如 AI 或 Physics，如果未引用该模块的类型。

代码剥离ON内容的影响在播放Player中显示为 null 引用异常、缺失类型错误或崩溃。此外，[AssetBundle](/engine/6000.0/manual/assets-and-media/assets-managing-runtime/assetbundles-section/asset-bundles-intro.md) 或 Addressables 的纯内容生成不能使用已从 Player 生成中删除的类型。

您可以使用[生成上报 Inspector 的](https://github.com/Unity-Technologies/BuildReportInspector) Stripping 选项卡来检查 Player 生成中保留了哪些模块和类型。

在以下情况下可以防止链接器剥离代码：

* [防止 AssetBundles 中使用的类型的代码剥离](#prevent-code-stripping-assetbundles)
* [防止 Addressables 中使用的类型的代码剥离](#prevent-code-stripping-addressables)
* [防止模块代码剥离](#prevent-code-stripping-modules)

## 防止 AssetBundles 中使用的类型的代码剥离##prevent-code-stripping-of-types-used-in-assetbundles

Unity 可能会剥离 AssetBundles 中存在的 Unity Object类型，但不会剥离 Player 构版本中包含的AnyScene或资源。这意味着，在播放模式下进行测试When，或者ON代码剥离最少的构建上进行测试时，AssetBundle 中的内容可能会有效，但在已启用了代码剥离的播放Player中会发生错误或崩溃。

要解决此问题，可以创建一个自定义义构版本脚本，直接包含 AssetBundle 的清单文件，如下所示：

1. 在构建 Player 之前[生成 AssetBundle](/engine/6000.0/manual/assets-and-media/assets-managing-runtime/assetbundles-section/creating/asset-bundles-building.md)。输出 Folder 为每个 AssetBundle 包含一个`.manifest`文件，以及一个与输出 Folder 同名的根`.manifest`文件。
2. 使用 [`BuildPipeline.BuildPlayer`](/engine/6000.0/script-reference/unityeditor/buildpipeline/buildplayer.md) API 将根`.manifest`文件的路径通道到 Player 版本进程中。

有关更多信息，请参阅 [AssetBundle File Format 参考](/engine/6000.0/manual/assets-and-media/assets-managing-runtime/assetbundles-section/creating/file-format.md)和[`BuildPlayerOptions.assetBundleManifestPath`](/engine/6000.0/script-reference/unityeditor/buildplayeroptions/assetbundlemanifestpath.md)。

还可以使用 [Preserve 属性](/engine/6000.0/manual/scripting/compilation-and-code-reload/script-compilation/managed-code-stripping/preserving.md)，或在项目中手动编写[`link.xml`](/engine/6000.0/manual/scripting/compilation-and-code-reload/script-compilation/managed-code-stripping/xml-formatting.md)文件，以进一步影响代码剥离行为。

## 防止 Addressables 中使用的类型剥离##preventing-stripping-of-types-used-in-addressables

如果某种类型的 Unity Object 存在于使用 [Addressables](https://docs.unity3d.com/Packages/com.unity.addressables@latest) 构建的内容中，但不存在于 Player 构建的任何 Scene 或资源中，则 Unity 可能会剥离该类型。

Addressables 在 `Library/com.unity.addressables` Folder 中自动生成一个[`link.xml`](/engine/6000.0/manual/scripting/compilation-and-code-reload/script-compilation/managed-code-stripping/xml-formatting.md)文件，描述内容中使用的类型。Player 生成会考虑此`link.xml`文件，前提是 Addressables 内容已在 Player 生成之前或同时生成。

还可以使用 [Preserve 属性](/engine/6000.0/manual/scripting/compilation-and-code-reload/script-compilation/managed-code-stripping/preserving.md)，或在项目中手动编写[`link.xml`](/engine/6000.0/manual/scripting/compilation-and-code-reload/script-compilation/managed-code-stripping/xml-formatting.md)文件，以进一步影响代码剥离行为。

## 防止模块剥离##preventing-module-stripping

不能使用`link.xml`文件来防止剥离整个 Unity 模块（例如 AI 或 Physics）。

如果需要在 Addressables 生成中使用模块中的类型，请make Player 生成中包含一些引用该模块的内容。例如，要保持 Physics 模块，请将Rigidbody或碰撞体体等组件添加到版本中包含的Scene中的游戏对象对象。这会创建 Player 版本进程识别的依赖关系并保留模块。

## 纯脚本构建##script-only-builds

Player 构建将使用的类型记录在 `Library/PlayerDataCache` Folder 内的`ScriptsOnlyCache.yaml`文件中，您可以查看该文件来诊断 Any 代码剥离问题。此文件用于支持[增量构建管线](/engine/6000.0/manual/building-and-publishing/building-introduction.md#incremental-build-pipeline)，该管线可以重用以前 Player 版本的内容。在这种情况下，Player 版本会将缓存的类型用法从该文件传递给 Unity 链接器。

> **Note:**
>
> `ScriptsOnlyCache.yaml` 文件使用在将来 Unity 版本中可能会更改的内部 Format。要控制代码剥离，请在 Project 中使用 `link.xml` 文件，而不是直接修改此文件。

## 其他资源##additional-resources

* [Link XML 格式参考](/engine/6000.0/manual/scripting/compilation-and-code-reload/script-compilation/managed-code-stripping/xml-formatting.md)
* [Addressables 包](https://docs.unity3d.com/Packages/com.unity.addressables@latest)
* [将 Assets 生成 AssetBundle](/engine/6000.0/manual/assets-and-media/assets-managing-runtime/assetbundles-section/creating/asset-bundles-building.md)
