Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

How to load a Unity Binary File? (LastBuild.buildreport)

Discussion in 'Scripting' started by Maeslezo, Feb 17, 2020.

  1. Maeslezo

    Maeslezo

    Joined:
    Jun 16, 2015
    Posts:
    304
    I am trying to load the build report that Unity generates.

    It is saved at "Library/LastBuild.buildreport"

    I opened it and I reckon that it is saved in a Binary Format.

    I have tried:
    Code (CSharp):
    1.         string path = "Library/LastBuild.buildreport";
    2.         FileStream s = File.OpenRead(path);
    3.         BinaryFormatter bf = new BinaryFormatter();
    4.         BuildReport r = bf.Deserialize(s) as BuildReport;
    But I get this exception:
    SerializationException: The input stream is not a valid binary format. The starting contents (in bytes) are:  00-00-18-06-00-01-17-80-00-00- ...


    Probably, the file is serialized with a custom Unity Binary Formatter, but I can not find any documentation about it.

    For example, in the Unity Tool, Unity Build Report, they copy the file to the project, import it and then they load the imported file with AssetDatabase.LoadAssetAtPath<BuildReport>

    https://github.com/Unity-Technologies/BuildReportInspector

    Code (CSharp):
    1.     [MenuItem("Window/Open Last Build Report")]
    2.     static void OpenLastBuild()
    3.     {
    4.         var buildReportDir = "Assets/BuildReports";
    5.         if (!Directory.Exists(buildReportDir))
    6.             Directory.CreateDirectory(buildReportDir);
    7.  
    8.         var date = File.GetLastWriteTime("Library/LastBuild.buildreport");
    9.         var assetPath = buildReportDir + "/Build_" + date.ToString("yyyy-dd-MMM-HH-mm-ss") + ".buildreport";
    10.         File.Copy("Library/LastBuild.buildreport", assetPath, true);
    11.         AssetDatabase.ImportAsset(assetPath);
    12.         Selection.objects = new[] { AssetDatabase.LoadAssetAtPath<BuildReport>(assetPath) };
    13.     }
    Known possibles workarounds:
    1. Do the same. Copy the report to the project, import, read the info and then delete the imported file
    2. Use my custom build method and get there the Build Report Info
    3. Use IPostprocessBuildWithReport and save BuildReport in a custom format
    I know I could use one of these workarounds, but I really would like to know if it is possible to load the report directly.

    Thank you
     
    Last edited: Feb 17, 2020
  2. Maeslezo

    Maeslezo

    Joined:
    Jun 16, 2015
    Posts:
    304
    If someone is struggling with the same problem, I recommend the following solution:

    UnityEngine.Object[] objects = UnityEditorInternal.InternalEditorUtility.LoadSerializedFileAndForget("Library/LastBuild.buildreport");


    After this, you would have to do some processing, but it works ok.

    I should remember that this is an undocumented/internal API, and therefore it is a bit risky to rely on it because the way it behaves might change in a future Unity version.

    Kudos to @Wilfrid_unity, who suggested this solution to me
     
  3. AlbertoGandulloScopely

    AlbertoGandulloScopely

    Joined:
    Feb 23, 2017
    Posts:
    1
    hey, @Maeslezo or @Wilfrid_unity how could I convert that array of objects to an object of type BuildReport?
    thanks in advance
     
  4. Maeslezo

    Maeslezo

    Joined:
    Jun 16, 2015
    Posts:
    304
    It's been a while but maybe you can try casting them

    BuildReport [] reports = objects.Cast<BuildReport>().ToArray();