# BuildHistory

> Provides access to the build history generated during builds.

## Definition

* **Type:** Class
* **Namespace:** [UnityEditor.Build](/engine/6000.6/script-reference/unityeditor/build.md)
* **Assembly:** UnityEditor.CoreModule

```csharp
public static class BuildHistory
```

## Remarks

This class provides programmatic access to the build history: for each Player or Content Directory build, Unity creates a "build report directory" that holds the [BuildReport](/engine/6000.6/script-reference/unityeditor/build/reporting/buildreport.md) file and the supporting data captured during that build. Use this class to enumerate builds, query their summaries, and locate the files in their build report directories. For a conceptual overview of the build history and a description of each file it contains, refer to [Build history](/engine/6000.6/manual/building-and-publishing/build-analyze-builds/build-history.md).

Build report directories are self-contained and portable. The `BuildHistory` API can read a build report directory even if a different machine or a different Unity project produced it, so you can consolidate build reports from multiple build servers into a single build history folder.

Unity assigns each build a unique GUID, which the `BuildHistory` API uses to precisely identify each build. For more information, refer to [BuildReportSummary.BuildSessionGUID](/engine/6000.6/script-reference/unityeditor/build/buildreportsummary/buildsessionguid.md).

**Retention policy**

To prevent unbounded growth of the build history folder, Unity applies a retention policy at the start of each Player and Content Directory build. [BuildHistory.BuildHistoryLimit](/engine/6000.6/script-reference/unityeditor/build/buildhistory/buildhistorylimit.md) sets the maximum number of builds to retain. When a new build pushes the count over the limit, the oldest entries are deleted. Set the limit to 0 to disable automatic deletion. You can also invoke the policy manually with [BuildHistory.ApplyRetentionPolicy](/engine/6000.6/script-reference/unityeditor/build/buildhistory/applyretentionpolicy.md).

**Build lifecycle**

For Player builds, [BuildPlayerProcessor.PrepareForBuild](/engine/6000.6/script-reference/unityeditor/build/buildplayerprocessor/prepareforbuild.md) runs before the Player build is added to the history. This allows any content builds triggered during that callback to appear in the history before the Player build itself, resulting in a chronological ordering.

Unity adds a build to the history early in the build process (but after [BuildPlayerProcessor.PrepareForBuild](/engine/6000.6/script-reference/unityeditor/build/buildplayerprocessor/prepareforbuild.md)) and sets the result to [BuildResult.Pending](/engine/6000.6/script-reference/unityeditor/build/reporting/buildresult/pending.md). If the Editor process terminates during a build, the build remains in the history with its initial `Pending` result.

Some BuildHistory methods, for example [BuildHistory.TryGetBuildReportDirectory](/engine/6000.6/script-reference/unityeditor/build/buildhistory/trygetbuildreportdirectory.md) and [BuildHistory.TryGetFilePath](/engine/6000.6/script-reference/unityeditor/build/buildhistory/trygetfilepath.md), are available for use in the [IPreprocessBuildWithReport](/engine/6000.6/script-reference/unityeditor/build/ipreprocessbuildwithreport.md) and [IPostprocessBuildWithReport](/engine/6000.6/script-reference/unityeditor/build/ipostprocessbuildwithreport.md) build callbacks.

When the build completes, the [BuildReportSummary](/engine/6000.6/script-reference/unityeditor/build/buildreportsummary.md) is updated with the final result ([BuildResult.Succeeded](/engine/6000.6/script-reference/unityeditor/build/reporting/buildresult/succeeded.md), [BuildResult.Failed](/engine/6000.6/script-reference/unityeditor/build/reporting/buildresult/failed.md), or [BuildResult.Cancelled](/engine/6000.6/script-reference/unityeditor/build/reporting/buildresult/cancelled.md)).

Additional Resources: [BuildReportSummary](/engine/6000.6/script-reference/unityeditor/build/buildreportsummary.md), [BuildReport](/engine/6000.6/script-reference/unityeditor/build/reporting/buildreport.md)

## Examples

```csharp
using System;
using System.Text;
using UnityEditor;
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
using UnityEngine;

/// <summary>
/// Example showing how to scan build history and generate a statistics report.
/// </summary>
public class BuildHistoryStatsExample
{
    [MenuItem("Example/BuildHistory/Print Build Statistics")]
    static public void PrintBuildStatistics()
    {
        int buildCount = BuildHistory.GetBuildCount();

        if (buildCount == 0)
        {
            Debug.Log("No builds found in build history.");
            return;
        }

        // Collect statistics by scanning the summary of all builds
        long totalTimeMs = 0;
        int succeededCount = 0;

        GUID[] allBuilds = BuildHistory.GetAllBuilds();
        foreach (var buildGuid in allBuilds)
        {
            BuildReportSummary summary = BuildHistory.GetBuildSummary(buildGuid);
            totalTimeMs += summary.TotalTimeMs;
            if (summary.BuildResult == BuildResult.Succeeded)
                succeededCount++;
        }

        var sb = new StringBuilder();
        sb.AppendLine($"=== Build History Statistics ===");
        sb.AppendLine($"Total Builds: {buildCount}");

        double successRate = (double)succeededCount / buildCount * 100;
        sb.AppendLine($"  Success rate: {successRate}%");

        TimeSpan totalTime = TimeSpan.FromMilliseconds(totalTimeMs);
        sb.AppendLine($"Total Build Time: {totalTime:g}");
        TimeSpan averageTime = TimeSpan.FromMilliseconds(totalTimeMs / buildCount);
        sb.AppendLine($"Average Build Time: {averageTime:g}");

        Debug.Log(sb.ToString());
    }
}
```

## Static Properties

| Property                                                                                                                   | Description                                                                                       |
| -------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------- |
| [BuildHistoryDirectory](/engine/6000.6/script-reference/unityeditor/build/buildhistory/buildhistorydirectory.md)           | Gets or sets the path where the build history will be stored.                                     |
| [BuildHistoryLimit](/engine/6000.6/script-reference/unityeditor/build/buildhistory/buildhistorylimit.md)                   | Maximum number of builds to retain in the build history.                                          |
| [DefaultRootDirectory](/engine/6000.6/script-reference/unityeditor/build/buildhistory/defaultrootdirectory.md)             | The default build history root directory path, regardless of the current build history directory. |
| [LatestBuildReportDirectory](/engine/6000.6/script-reference/unityeditor/build/buildhistory/latestbuildreportdirectory.md) | Returns the build report directory of the most recent build.                                      |

## Static Methods

| Method                                                                                                                                   | Description                                                                                                                                                                                       |
| ---------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [ApplyRetentionPolicy](/engine/6000.6/script-reference/unityeditor/build/buildhistory/applyretentionpolicy.md)                           | Deletes the oldest builds in the build history so that no more than [BuildHistory.BuildHistoryLimit](/engine/6000.6/script-reference/unityeditor/build/buildhistory/buildhistorylimit.md) remain. |
| [DeleteHistory](/engine/6000.6/script-reference/unityeditor/build/buildhistory/deletehistory.md)                                         | Deletes all recorded build history.                                                                                                                                                               |
| [GetAllBuilds](/engine/6000.6/script-reference/unityeditor/build/buildhistory/getallbuilds.md)                                           | Returns the session GUIDs for all builds in the build history.                                                                                                                                    |
| [GetBuildCount](/engine/6000.6/script-reference/unityeditor/build/buildhistory/getbuildcount.md)                                         | Gets the total number of builds in the build history.                                                                                                                                             |
| [GetBuildSummary](/engine/6000.6/script-reference/unityeditor/build/buildhistory/getbuildsummary.md)                                     | Gets the build summary for a specific build.                                                                                                                                                      |
| [GetRevision](/engine/6000.6/script-reference/unityeditor/build/buildhistory/getrevision.md)                                             | Gets the current revision number of the build history.                                                                                                                                            |
| [LoadBuildReport](/engine/6000.6/script-reference/unityeditor/build/buildhistory/loadbuildreport.md)                                     | Loads the [BuildReport](/engine/6000.6/script-reference/unityeditor/build/reporting/buildreport.md) for a specific build from its metadata folder.                                                |
| [Refresh](/engine/6000.6/script-reference/unityeditor/build/buildhistory/refresh.md)                                                     | Updates the BuildHistory incrementally, detecting build directories that have been added or removed on disk.                                                                                      |
| [RefreshFull](/engine/6000.6/script-reference/unityeditor/build/buildhistory/refreshfull.md)                                             | Performs a full reload of the BuildHistory from disk, discarding all cached state.                                                                                                                |
| [TryGetBuildReportDirectory](/engine/6000.6/script-reference/unityeditor/build/buildhistory/trygetbuildreportdirectory.md)               | Attempts to get the build report directory for a specific build.                                                                                                                                  |
| [TryGetBuildSummaryForManifestHash](/engine/6000.6/script-reference/unityeditor/build/buildhistory/trygetbuildsummaryformanifesthash.md) | Attempts to get the build summary for the most recent build that produced a specific manifest hash.                                                                                               |
| [TryGetBuildSummaryForOutputPath](/engine/6000.6/script-reference/unityeditor/build/buildhistory/trygetbuildsummaryforoutputpath.md)     | Attempts to get the build summary for the most recent build that was made to a specific output path.                                                                                              |
| [TryGetFilePath](/engine/6000.6/script-reference/unityeditor/build/buildhistory/trygetfilepath.md)                                       | Attempts to get the path to a specific file within a build's metadata folder.                                                                                                                     |
| [TryGetLatestBuild](/engine/6000.6/script-reference/unityeditor/build/buildhistory/trygetlatestbuild.md)                                 | Attempts to get the GUID of the most recent build.                                                                                                                                                |
