How code stripping affects content
Understand how your managed code stripping configuration can affect content builds and how to preserve code your content depends on.
Read time 3 minutesLast updated 13 days ago
When you build a Player in Unity, the UnityLinker process runs to reduce the size of the assemblies by removing unused types. This process can strip the following:
- Entire C# Unity objects such as and
MonoBehaviourinstances. Unity can strip these from core Unity assemblies likeScriptableObjectandUnityEngine.dll, but preserves them in custom assemblies once those assemblies are marked as used.Unity.dll - Regular C# structures and classes.
- Portions of classes (for example, methods that aren't used).
- Entire Unity modules, such as AI or Physics, if no type from that module is referenced.
The affect that code stripping has on content appears in the Player as null reference exceptions, missing type errors, or crashes. Additionally, AssetBundle or Addressables content-only build can't use types that have been stripped out of a Player build.
You can use the Build Report Inspector's Stripping tab to inspect which modules and types have been preserved in a Player build.
You can prevent the linker from stripping code in the following circumstances:
- Prevent code stripping of types used in AssetBundles
- Prevent code stripping of types used in Addressables
- Prevent module code stripping
Prevent code stripping of types used in AssetBundles
Unity might strip a Unity object type that's present in AssetBundles, but not in any scene or asset included in a Player build. This means that content from the AssetBundle might work when testing in Play mode, or on builds with minimal code stripping, but errors or crashes occur in a Player built with code stripping enabled.
To resolve this, you can create a custom build script that directly includes the AssetBundle's manifest file, as follows:
- Build the AssetBundle before building the Player. The output folder includes one file for each AssetBundle, and a root
.manifestfile with the same name as the output folder..manifest - Pass the path of the root file into the Player build process with the
.manifestAPI.BuildPipeline.BuildPlayer
For more information, refer to AssetBundle file format reference and .
BuildPlayerOptions.assetBundleManifestPathYou can also use the Preserve attribute, or manually author a file in your project, to further influence the code stripping behavior.
link.xmlPreventing stripping of types used in Addressables
If a type of Unity object is present in content built with Addressables, but not in any scene or asset built into the Player, then Unity might strip that type.
Addressables automatically generates a file inside the folder describing types used in the content. The Player build takes this file into account, provided the Addressables content has been built before, or at the same time as, the Player build.
link.xmlLibrary/com.unity.addressableslink.xmlYou can also use the Preserve attribute, or manually author a file in your project, to further influence the code stripping behavior.
link.xmlPreventing module stripping
You can't use a file to prevent entire Unity modules (for example AI or Physics) from being stripped.
link.xmlIf you need to use types from a module in an Addressables build, make sure the Player build includes some content that references that module. For example, to keep the Physics module, add a component such as a Rigidbody or Collider to a GameObject in a scene included in your build. This creates a dependency that the Player build process recognizes, and preserves the module.
Script-only builds
Player builds record the used types in a file inside the folder, which you can look at to diagnose any code stripping issues. This file is used to support the Incremental build pipeline which can reuse the content from a previous Player build. In that case the Player build passes the cached type usage from that file to the Unity linker.
ScriptsOnlyCache.yamlLibrary/PlayerDataCache