Here is a little tip for how to load a BuildReport file from any path.
By default, the BuildReport is written in Unity’s binary SerializedFile format.
You can load the most recent one using this API:
BuildReport.GetLatestReport. That loads if from the hardcoded path where Player and AssetBundle builds write the file (Library/LastBuild.buildreport).
However you might want to keep more than one buildreport, e.g. move and rename that file to establish a history of build. That can make it possible to consult back and compare between builds.
One approach is to copy the BuildReport file into your Project Assets folder. That would make it loadable using AssetDatabase APIs. It also makes it inspectable using the BuildReportInspector package. This also will also typically converts the file into YAML format which makes it possible to parse it as a text file. However the downsize is that you may not want these files inside your Project. You might not want to check them in to source control, or to pay the cost of Unity “importing” and tracking these files as “assets”.
Writing code to load the file from other locations has two challenging aspects:
- It relies on an undocumented API InternalEditorUtility.LoadSerializedFileAndForget
- The order of objects inside the file is not deterministic (so you have to search for the actual BuildReport object)
But this method takes care of those details and should be easy to incorporate into your projects. It should work for all recent versions of the Unity Editor.
static BuildReport LoadBuildReport(string buildReportPath)
{
if (!File.Exists(buildReportPath))
return null;
try
{
// This API works to load BuildReport SerializedFiles that are in the Library
// or other locations outside the regular Assets or Package folders.
var objs = InternalEditorUtility.LoadSerializedFileAndForget(buildReportPath);
foreach(UnityEngine.Object obj in objs)
{
if (obj is BuildReport)
return obj as BuildReport;
}
}
catch (Exception ex)
{
Debug.LogWarning($"Failed to load build report from {buildMetadataDirectory}: {ex.Message}");
}
return null;
}
Please let me know if you find it useful, or if you hit any issues!