Documentation

Unity Engine

Download Unity

Unity Engine

GetContentDirectoryLoadInfo(ContentDirectoryHandle, ContentDirectoryLoadInfo)

Fills a ContentDirectoryLoadInfo for one content directory.
Read time 3 minutesLast updated 18 hours ago

Definition

  • Type: Method
  • Namespace: Unity.Loading
  • Assembly: UnityEngine.ContentLoadModule
public static void GetContentDirectoryLoadInfo(ContentDirectoryHandle directory, ContentDirectoryLoadInfo info)

Parameters

The content directory to describe.

The information to fill. Reuse one instance across calls.

Remarks

The info lists the content files of the directory that are in memory, the scenes loaded from it, and the dependencies between them, all taken at the same moment. The entries of each list are in no particular order.
Use this to find Loadables that were loaded and not released. The state reports content files, not Loadables: a loaded Loadable<T> holds the content file of its object, so a Loadable that was not released shows as a hold on that file. Fill one info before a level loads and another after it unloads. A file whose LoadedContentFileInfo.ReferenceCount, minus its ContentDirectoryLoadInfo.RootAssetReferences entry, grew in between is held by a Loadable that the level loaded and did not release.
Only content loaded from the content directory is listed. In Editor Play mode, Loadables that the AssetDatabase serves are not listed. To check them, load the content from a content directory build. To describe every registered directory, call this method for each handle that ContentLoadManager.GetContentDirectories returns.
The lists are filled in the Editor and in development Players. In release Players they stay empty. Use Debug.isDebugBuild to tell an empty info in a release Player from an info with nothing loaded. When
directory
is not registered, the lists stay empty and ContentDirectoryLoadInfo.BuildManifestHash is a default Hash128.
This method clears the lists of
info
before it fills them, and only allocates managed memory when a list needs more capacity. Call it from the main thread.
Additional Resources: ContentLoadState, Loadable<T>.Release

Examples

using System.Collections.Generic;using Unity.Loading;using UnityEditor;using UnityEngine;namespace BuildDocExamples{ // Example: find what a level loaded and did not release. Count the holds that game code keeps on // each content file before the level loads, and again after it unloads. A file that game code // holds more often afterwards is held by a Loadable that the level loaded and did not release. // The same asset built into two content directories has the same identity in both, so the counts // are kept per directory. public class ContentDirectoryDiagnostics_GetContentDirectoryLoadInfoExample { readonly ContentDirectoryLoadInfo m_Info = new ContentDirectoryLoadInfo(); readonly Dictionary<(ContentDirectoryHandle, ContentFileIdentity), int> m_HoldsBeforeLevel = new Dictionary<(ContentDirectoryHandle, ContentFileIdentity), int>(); public void OnBeforeLevelLoads() { m_HoldsBeforeLevel.Clear(); foreach (ContentDirectoryHandle directory in ContentLoadManager.GetContentDirectories()) { ContentDirectoryDiagnostics.GetContentDirectoryLoadInfo(directory, m_Info); for (int i = 0; i < m_Info.LoadState.Files.Count; i++) m_HoldsBeforeLevel[(directory, m_Info.LoadState.Files[i].Identity)] = GameCodeHolds(i); } } public void OnAfterLevelUnloads() { foreach (ContentDirectoryHandle directory in ContentLoadManager.GetContentDirectories()) { ContentDirectoryDiagnostics.GetContentDirectoryLoadInfo(directory, m_Info); ContentLoadState state = m_Info.LoadState; for (int i = 0; i < state.Files.Count; i++) { LoadedContentFileInfo file = state.Files[i]; m_HoldsBeforeLevel.TryGetValue((directory, file.Identity), out int holdsBefore); int notReleased = GameCodeHolds(i) - holdsBefore; if (notReleased > 0) Debug.LogWarning($"{notReleased} Loadable(s) not released after the level unloaded, in {NameOf(file.Identity)}"); } } } // Unity holds the files of root assets for the content directory. The other holds come from game code. int GameCodeHolds(int fileIndex) { return m_Info.LoadState.Files[fileIndex].ReferenceCount - m_Info.RootAssetReferences[fileIndex]; } // In the Editor, name the asset that the file was built from. Elsewhere, the identity is the // file's StableId in the ContentLayout.json file of the build that ContentDirectoryLoadInfo.BuildManifestHash names. static string NameOf(ContentFileIdentity file) { return ContentLoadEditorUtility.TryGetSourceAssetGUID(file, out GUID guid) ? AssetDatabase.GUIDToAssetPath(guid) : file.ToString(); } }}