Version: Unity 6.1 Beta (6000.1)
LanguageEnglish
  • C#

PackedAssetInfo

struct in UnityEditor.Build.Reporting

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Description

Contains information about a range of bytes in a file in the build output.

A Packed Asset contains either the serialized binary representation of a Unity Object, or the binary data of a texture, mesh, audio or video that belongs to a Unity object.

Note: the "Packed Asset" name is somewhat misleading because the data is associated with a specific object within an Asset, not an entire Asset. Some Assets contain just a single object, but in many cases it may contain an entire hierarchy of objects, each with its own PackedAssetInfo entry.

Additional resources: BuildReport, PackedAssets.contents

using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using UnityEditor;
using UnityEditor.Build.Reporting;
using UnityEngine;

public struct ContentEntry { public ulong size; // Bytes public int objectCount; // Number of objects from the same source }

public class BuildReportPackedAssetInfoExample { [MenuItem("Example/Build and Analyze AssetBundle")] static public void Build() { string buildOutputDirectory = "BuildOutput"; if (!Directory.Exists(buildOutputDirectory)) Directory.CreateDirectory(buildOutputDirectory);

var bundleDefinitions = new AssetBundleBuild[] { new AssetBundleBuild() { assetBundleName = "MyBundle",

// Tip: Adjust this list to builds scenes or assets from your project assetNames = new string[] { "Assets/Scenes/TestScene.unity" } } };

BuildPipeline.BuildAssetBundles( buildOutputDirectory, bundleDefinitions, BuildAssetBundleOptions.ForceRebuildAssetBundle, EditorUserBuildSettings.activeBuildTarget);

BuildReport report = BuildReport.GetLatestReport(); if (report != null) { var sb = new StringBuilder(); sb.AppendLine("Build result : " + report.summary.result); sb.AppendLine("Build size : " + report.summary.totalSize + " bytes"); sb.Append(ClassifyBuildOutputBySourceAsset(report)); Debug.Log(sb.ToString()); } else { Debug.Log("AssetBundle build failed"); } }

public static string ClassifyBuildOutputBySourceAsset(BuildReport buildReport) { var sb = new StringBuilder();

var sourceAssetSize = new Dictionary<string, ContentEntry>();

var packedAssets = buildReport.packedAssets; foreach(var packedAsset in packedAssets) { sb.AppendLine("Analyzing " + packedAsset.shortPath + "....");

var contents = packedAsset.contents; foreach(var packedAssetInfo in contents) { // Path of the asset that contains this object var path = packedAssetInfo.sourceAssetPath;

if (string.IsNullOrEmpty(path)) path = "Internal";

if (sourceAssetSize.ContainsKey(path)) { var existingEntry = sourceAssetSize[path]; existingEntry.size += packedAssetInfo.packedSize; existingEntry.objectCount++; sourceAssetSize[path] = existingEntry; } else { sourceAssetSize[path] = new ContentEntry { size = packedAssetInfo.packedSize, objectCount = 1 }; } } }

sb.AppendLine("The Build contains the content from the following source assets:\n");

// Sort biggest to smallest var sortedSourceAssetSize = sourceAssetSize.OrderByDescending(x => x.Value.size);

// Note: for large builds there could be thousands or more different source assets, // in which case it could be prudent to only show the top 10 or top 100 results. for (int i = 0; i < sortedSourceAssetSize.Count(); i++) { var entry = sortedSourceAssetSize.ElementAt(i); sb.AppendLine(" Asset: \"" + entry.Key + "\" Object Count: " + entry.Value.objectCount + " Size of Objects: " + entry.Value.size); }

return sb.ToString(); } }

Properties

idLocal file id of the object
offsetThe offset from the start of the file to the first byte of the range belonging to the Packed Asset.
packedSizeThe size in bytes of the Packed Asset.
sourceAssetGUIDThe Global Unique Identifier (GUID) of the source Asset that the build process used to generate the packed Asset.
sourceAssetPathThe file path to the source Asset that the build process used to generate the Packed Asset, relative to the Project directory.
typeThe type of the object whose serialized data is represented by the Packed Asset, such as GameObject, Mesh or AudioClip.