文档

Unity Engine

Download Unity

Unity Engine

ContentDirectoryLoadInfo

What is loaded from one content directory, which of it Unity holds for the directory's root assets, and the build that produced the directory.
阅读时间2 分钟最后更新于 2 天前

Definition

  • Type: Class
  • Namespace: Unity.Loading
  • Assembly: UnityEngine.ContentLoadModule
public sealed class ContentDirectoryLoadInfo

Remarks

Pass an instance to ContentDirectoryDiagnostics.GetContentDirectoryLoadInfo to fill it, and reuse the instance across calls. The lists stay empty in release Players.

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(); } }}

Properties

Property

Description

BuildManifestHashIdentifies the build that produced the content directory.
LoadStateThe files, scenes and dependencies loaded from the content directory.
RootAssetReferencesFor each entry of ContentLoadState.Files in ContentDirectoryLoadInfo.LoadState, how many of its LoadedContentFileInfo.ReferenceCount are holds that Unity keeps on root assets.