iTranslated by AI
Some Build Information Obtained via IPostprocessBuildWithReport is Incorrect
What is IPostprocessBuildWithReport
IPostprocessBuildWithReport is an interface that defines a callback function executed immediately after a Unity build completes. By implementing this interface, you can customize post-build processing.
Below is a sample code using IPostprocessBuildWithReport to output a log after a build.
using UnityEngine;
using UnityEditor;
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
public class Postprocessor : IPostprocessBuildWithReport
{
public int callbackOrder => 0;
public void OnPostprocessBuild(BuildReport report) // report contains information about the build
{
Debug.Log("Build completed with " + report.summary.result);
}
}
OnPostprocessBuild() is a callback function executed immediately after the build is completed, and its argument contains information about the build.
callbackOrder allows you to define the execution order, with smaller values being executed first.
If you write the code above and actually run a build, you will see the following output:
Build completed with Unknown
Even when the build is definitely successful, report.summary.result contains BuildResult.Unknown.
However, it should normally contain a value corresponding to the build result, i.e., Succeeded for success, Failed for failure, and Cancelled for cancellation.
Incidentally, the results were the same whether the build was executed from Build Settings > Build in the Unity Editor, executed in batch mode, or when callbackOrder was changed to various values.
As described above, the build information obtained via IPostprocessBuildWithReport contains incorrect information.
Also, as far as I have investigated, the following values were also incorrect in addition to BuildReport.summary.result:
This issue has already been reported in the following issues, but unfortunately, it is marked as "Won't Fix":
- Unity Issue Tracker - [BuildReport] report in IPostProcessBuildWithReport provides incorrect information
- Unity Issue Tracker - [BuildReport] summary.result in IPostprocessBuildWithReport always return "Unknown"
Workarounds
Here are two ways to work around this issue.
1. Get build information from the return value of BuildPipeline.BuildPlayer()
The first workaround is to obtain build information from the return value of BuildPipeline.BuildPlayer().
Below is a sample code.
using UnityEditor;
using UnityEngine;
using UnityEditor.Build.Reporting;
public class BuildPlayerExample
{
[MenuItem("Build/StandaloneOSX")]
public static void Build()
{
var buildPlayerOptions = new BuildPlayerOptions
{
scenes = new[] { "Assets/Scenes/SampleScene.unity" },
locationPathName = "Outputs",
target = BuildTarget.StandaloneOSX,
options = BuildOptions.Development,
};
// Correct build information is returned
var report = BuildPipeline.BuildPlayer(buildPlayerOptions);
// Build completed with Succeeded
Debug.Log("Build completed with " + report.summary.result);
}
}
However, in this case, it is necessary to create an entry point for executing the build, such as using a MenuItem as shown above, so that the execution result of BuildPipeline.BuildPlayer() can be received.
2. Get build information from Library/LastBuild.buildreport
The second method is to read the build information from Library/LastBuild.buildreport, which is output at the end of the build.
Below is a sample code.
using System.IO;
using UnityEditor;
using UnityEditor.Build.Reporting;
using UnityEngine;
public sealed class BuildReportLoader
{
private readonly string _buildReportDir =
$"{Path.Combine(Application.dataPath, LastBuildReportsDirectoryName)}";
private readonly string _lastBuildReportsAssetPath =
$"{Path.Combine("Assets", LastBuildReportsDirectoryName, LastBuildReportFileName)}";
private const string LastBuildReportsDirectoryName = "LastBuildReports";
private const string LibraryDirectoryName = "Library";
private const string LastBuildReportFileName = "LastBuild.buildreport";
public void LoadBuildReport()
{
var projectRootPath = Directory.GetParent(Application.dataPath)?.FullName;
if (string.IsNullOrEmpty(projectRootPath))
{
return;
}
var lastBuildReportPath = $"{Path.Combine(projectRootPath, LibraryDirectoryName, LastBuildReportFileName)}";
if (!File.Exists(lastBuildReportPath))
{
return;
}
if (!Directory.Exists(_buildReportDir))
{
Directory.CreateDirectory(_buildReportDir);
}
File.Copy(lastBuildReportPath, _lastBuildReportsAssetPath, true);
AssetDatabase.ImportAsset(_lastBuildReportsAssetPath);
var report = AssetDatabase.LoadAssetAtPath<BuildReport>(_lastBuildReportsAssetPath);
Debug.Log("Build completed with " + report.summary.result);
}
}
In the code above, to read Library/LastBuild.buildreport, the file is first copied into Assets and then read using AssetDatabase.LoadAssetAtPath().
Incidentally, BuildReportInspector and GhaUnityBuildReporter use this method to obtain build information.
Discussion