Version: Unity 6.7 Beta (6000.7)
Language : English
Load content directories
Modify source assets from code

Avoiding asset duplication

Asset duplication happens when Unity recursively follows direct references to make sure that all required objects are included in the build. Assets and scenes aren’t shared between different builds, so the same content might be duplicated both in the Player build and a content build. For small objects this might not be a problem, but for large assets such as textures or meshes this can lead to large build sizes.

Create content builds to minimize asset duplication

To avoid or minimize asset duplication, you can minimize the amount of content that goes into the Player build and put all the content in your project into a content build.

The content directory system automatically removes any duplicated content in a build and handles dependencies because they’re tracked per asset. You can also use the AssetBundle system if you have projects that use remote content. The Addressables package supports both build systems.

To create a minimal Player build, include only a single starting scene containing a MonoBehavior that bootstraps the rest of the content. Avoid putting any assets into Resources folders that are also referenced by a content build. You can ship the output of content builds with the Player build, using the StreamingAssets folder, or hosted on a CDN (AssetBundle builds only).

Duplication of assets in content directories

Unity removes duplicated content within a single build, but each build is self-contained. If two content directory builds both reference the same asset, through a direct reference, a Loadable<T>, or a LoadableSceneId, then each build includes its own copy of that asset. The same applies between a content directory build and the content that Unity builds into a Player build or an AssetBundle.

Because Unity names the output files with a hash of their content, a file name that appears in more than one content directory build identifies content that’s duplicated across those builds. To analyze the content of a build in more detail, use UnityDataTools.

To avoid unnecessary duplication, reference each asset from a single build, then use untracked references to reach content in another build at runtime. For more information, refer to Reference content in a content directory.

Duplication of assets in AssetBundles

Unity uses the Asset Database to discover all dependencies of an object when it’s built into an AssetBundle. Unity uses this dependency information to determine the set of objects to include in an AssetBundle.

AssetBundle assignment happens at the asset level. Objects in an asset explicitly assigned to an AssetBundle are included only in that specific AssetBundle. Depending on the BuildPipeline.BuildAssetBundles method used, an asset is explicitly assigned either by setting its AssetImporter.assetBundleName property to a non-empty string, or by listing it in AssetBundleBuild.assetNames.

Assets can be duplicated in AssetBundles if the same asset is referenced from different AssetBundles. If that asset isn’t explicitly assigned to an AssetBundle then Unity copies its content inside each AssetBundle that references it.

For example, if two objects are assigned to separate AssetBundles but share a reference to the same dependency object, Unity duplicates the dependency object into both AssetBundles. These duplicated dependencies are also instanced, meaning the two copies are treated as distinct objects with unique identifiers. This increases the overall size of the application’s AssetBundles and results in two separate copies of the object being loaded into memory if both parent AssetBundles are loaded.

To prevent this from happening, make sure that shared assets are explicitly assigned to an AssetBundle. For more information, refer to Handling dependencies between AssetBundles. UnityDataTools is a useful tool to identify duplication in AssetBundles.

Addressables has the same issue with duplication of implicitly referenced assets. In that case, you can avoid it by assigning shared assets to a group. The Addressables Analyze tool helps diagnose and resolve asset duplication.

Additional resources

Load content directories
Modify source assets from code