2
\$\begingroup\$

We have an issue with organizing our builds. They get copied around and moved so much that it's hard to keep track of what is the newest.

I would like to output a simple text file to the build root (same level as exe) that contains info like the build date and time, platform, etc.

Is this possible from within Unity? Right now we have no automated build process (CI/CD).

edit: I found onPreprocessBuild() but unfortunately one of our main projects still uses Unity 5.3.6f1, so this will not work. Is there an alternative?

\$\endgroup\$
2
\$\begingroup\$

In Unity 5.3, there is the PostProcessBuildAttribute. "Add this attribute to a method to get a notification just after building the player." (Source: UnityEditor.Callbacks.PostProcessBuildAttribute documentation.)

Using that attribute, you should be able to set up something like:

[PostProcessBuild]
public static void OnPostprocessBuild(BuildTarget target, string pathToBuiltProject)
{
    CreateBuildTimestampFile(pathToBuiltProject);
}

private static void CreateBuildTimestampFile(string pathToBuiltProject)
{
    string directory = Path.GetDirectoryName(pathToBuiltProject);

    string timestampFile = Path.Combine(directory, "timestamp.txt");

    if (File.Exists(timestampFile))
    {
        File.Delete(timestampFile);
    }

    using (var streamWriter = File.CreateText(timestampFile))
    {
        var timestamp = System.DateTime.UtcNow.ToString(CultureInfo.InvariantCulture);
        streamWriter.WriteLine(timestamp);
    }
}

As a side note, you may find the Unity 5.3 Build Player Pipeline documentation page useful as well.

\$\endgroup\$
5
  • \$\begingroup\$ Thanks for the response, but you seem to be missing a bracket or something? I assume an opening bracket on line 3. Or I'm even worse at C# than I thought. \$\endgroup\$ – JOATMON Apr 9 '18 at 14:25
  • 1
    \$\begingroup\$ Oops sorry about that. I edited my answer. \$\endgroup\$ – sonny Apr 9 '18 at 17:13
  • \$\begingroup\$ Thanks. When I put this in a class (MyBuildPostProcessor), it throws an error on var timestamp saying that var has an "Unknown Resolve Error". Ideas? \$\endgroup\$ – JOATMON Apr 9 '18 at 17:17
  • \$\begingroup\$ Maybe it's a namespace issue with DateTime? Is using System; present at the top of your file? Or try changing the line to use string instead: string timestamp = System.DateTime.UtcNow.ToString(CultureInfo.InvariantCulture);. (Note: I edited the answer to fix a typo on the next line, and to incorporate this comment into the answer.) \$\endgroup\$ – sonny Apr 9 '18 at 17:30
  • 1
    \$\begingroup\$ That was it, thanks. I also see you fixed the timestamp typo. All is working now, thanks so much! \$\endgroup\$ – JOATMON Apr 9 '18 at 17:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.