Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 64 additions & 6 deletions Documentation/contentdirectory-format.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ The output can be written as loose files or packed into a Unity Archive, so Unit
same way it opens Player and AssetBundle content.

An example build, used for testing, can be found in `TestCommon/Data/LeadingEdgeBuilds/ContentDirectory`.
The `ContentLayout.json` excerpts on this page come from its layout in
`TestCommon/Data/LeadingEdgeBuilds/BuildReport-ContentDirectory`, which uses the version 3 schema
written by Unity 6.7. Unity 6.6 writes version 2, which names some fields differently — see
[Schema versioning](contentlayout.md#schema-versioning).

The files use technical, content-hash-based names, rather than the familiar `level0` / `sharedassets`
names of a Player build or the user-specified names of AssetBundles. You do not need to understand the
Expand Down Expand Up @@ -109,12 +113,9 @@ few grouping rules shape the result:
File, just as they are represented in the Editor (see [Scenes](#scenes) below).
- **Assets with sub-assets** are split across multiple Content Files. For example an FBX is separated
so that the component hierarchy and the meshes land in different files.
- **Scripts** are grouped: the `MonoScript` objects for several source scripts can share one Content
File.
- **Circular direct references are best avoided.** Currently a cycle forces all the affected Unity
objects to be clustered together into a single Content File, and the build prints a warning. The way
to break a cycle is to convert one of the references into a `Loadable` (an on-demand reference)
instead of a direct reference.
- **Scripts** are grouped: all the `MonoScript` objects in the build share a single Content File.
- **Circular direct references are best avoided.** How a cycle shows up in the build output depends
on the Unity version — see [Circular references](#circular-references) below.

Unity's two built-in resource files are handled in different ways:

Expand All @@ -130,6 +131,63 @@ Unity's two built-in resource files are handled in different ways:
contains only the project's "Always Included Shaders" — copying the referenced objects into the
content directory makes it independent of what a particular Player build happens to contain.

### Circular references

A circular reference is a loop of direct references between objects that the rules above would place
in different Content Files — for example two ScriptableObject assets that reference each other, or a
Prefab whose component references a ScriptableObject that references the Prefab back. Only direct
references count: a `Loadable` or `LoadableSceneId` reference does not create a dependency between
files, so converting one of the references in the loop into a `Loadable` breaks the cycle. Scenes
are never part of a cycle, because Unity does not support direct references to objects inside a
scene.

**Unity 6.6** merges each cycle at build time. All the objects that would have formed the loop are
written into a single Content File, so the dependencies between Content Files never contain a cycle.
In [`ContentLayout.json`](contentlayout.md) the merged file is one `SerializedFiles` entry whose
`SourceAssets` lists every asset in the loop. The build prints a warning that it detected circular
reference loops, and the Editor log lists the assets involved in each one. A long chain of references
can pull many assets into one unexpectedly large Content File, and loading any of them loads them all.

**Unity 6.7** keeps the normal granular layout. Each asset in the loop stays in its own Content File
and the files reference each other in a cycle, which is visible in `ContentLayout.json` as
`SerializedFileDependencies` that lead back to the starting file. At runtime the files in a cycle are
loaded together as a single unit: loading any one of them loads all of them, and this is more
resource-intensive than loading files individually. The build prints a warning when it finds a cycle,
and fails if the combined size of the Content Files in a single cycle exceeds a limit (currently
64 MB).

Unity 6.7 also writes a `circularrefs.json` file to the build's directory in the
[build history](#build-history), listing each cycle with its files. A common pattern is a Prefab
that references a definition asset which references the Prefab back:

```json
{
"clusters": [
{
"fileCount": 2,
"combinedSizeBytes": 22656,
"files": [
{
"fileId": "3c9e1f0a7b2d48e6a15f0c8d9e2b4a71",
"guid": "3c9e1f0a7b2d48e6a15f0c8d9e2b4a71",
"path": "Assets/Items/Crate.prefab"
},
{
"fileId": "8d4a62b0e9f1475c9b3e1a0f6c7d2e58",
"guid": "8d4a62b0e9f1475c9b3e1a0f6c7d2e58",
"path": "Assets/Items/Definitions/Crate.asset"
}
]
}
]
}
```

The file is written for every build, with an empty `clusters` array when there are no cycles. Each
`fileId` is the file's `StableId` in `ContentLayout.json`, and `path` is the source asset the Content
File was built from. In a large project this file is worth checking: it can list hundreds of cycles,
and a single cycle can span hundreds of files.

## Scenes

Content directory builds can include Unity Scenes. The build parameters only accept ScriptableObject
Expand Down
4 changes: 3 additions & 1 deletion Documentation/contentlayout.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ resource path (`Library/unity default resources`) as its stable id.
* `BinaryArtifacts` also reports the additional data files that hold audio, video, texture, and mesh data (the `.resource` and `.resS` files).
* BinaryArtifacts are identified by the hash of their content. When saved as a file, the filename is the hash and the file extension is based on the category.

`ArtifactReferences` lists only direct dependencies. Dependencies that go through a loadable or loadable scene are not included, and the dependency graph is never cyclical. Together, this makes it possible to see every artifact required to load a particular serialized file, excluding data that is loaded on demand through a loadable or loadable scene.
`ArtifactReferences` does not record dependencies between serialized files. The dependencies that come from objects with direct (PPtr) references to objects in other serialized files are listed only in the `SerializedFiles` entries, in `SerializedFileDependencies`. A `contentfile` artifact's `ArtifactReferences` lists only the `.resS` and `.resource` data files its objects use. It never lists another `contentfile`, and the data files are leaf nodes with no dependencies of their own. By design the `ArtifactReferences` graph never contains cycles, and this will stay true if more categories of data are added in the future.

To find every artifact required to load a serialized file, follow its `SerializedFileDependencies` recursively to the other serialized files it needs, and collect each file's artifact (through `ArtifactIndex`) together with that artifact's `ArtifactReferences`. Dependencies that go through a loadable or loadable scene are not included in either list, because that data is loaded on demand. Unlike `ArtifactReferences`, `SerializedFileDependencies` can contain cycles in builds from Unity 6.7, where assets that reference each other in a loop keep their own serialized files. See [Circular references](contentdirectory-format.md#circular-references).

## Schema versioning

Expand Down
Loading