Showing the build date in a Unity app

When doing rapid iterations of an app (a cornerstone of agile development), testers sometimes find themselves with an older version of the app.  This could be because they didn't get the update email, or they clicked the wrong link, or DropBox didn't sync like it's supposed to, or something got cached along the way... whatever the reason, there are few things less helpful than getting a bug report for something you've already fixed.

In C/C++, it's easy enough to use the standard __DATE__ macro, which the C preprocessor fills in with the actual build date.  You can just display this on the title screen or wherever, and testers can easily see whether they're running the latest build.  But, surprisingly, C# doesn't have any such feature.  This makes displaying the build date in a Unity app a bit harder.

Fortunately, it can be done.  Here is the arcane magic you need.

 

First, here's the script.  Save this in a file called BuildDate.cs:

using System.Reflection;
using UnityEngine;
using System.Collections;

[assembly:AssemblyVersion( "1.0.*.*" )]

[RequireComponent(typeof(UnityEngine.UI.Text))]

public class BuildDate : MonoBehaviour {
    [Tooltip("Date/time format.")]
    public string format = "g";    // see: https://msdn.microsoft.com/en-us/library/az4se3k1%28v=vs.110%29.aspx
   
    void Start() {
        GetComponent<UnityEngine.UI.Text>().text = BuildDate.ToString(format);
    }
   
    public static System.Version Version() {
        return Assembly.GetExecutingAssembly().GetName().Version;
    }
   
    public static System.DateTime Date() {
        System.Version version = Version();
        System.DateTime startDate = new System.DateTime( 2000, 1, 1, 0, 0, 0 );
        System.TimeSpan span = new System.TimeSpan( version.Build, 0, 0, version.Revision * 2 );
        System.DateTime buildDate = startDate.Add( span );
        return buildDate;
    }
   
    public static string ToString(string format=null) {
        System.DateTime date = Date();
        return date.ToString(format);
    }
}

This can be used two ways.  First, it provides three static methods — Version, Date, and ToString — that you can call from anywhere, at any time.  So if you just want to log the build date for example, you can simply do

Debug.Log(BuildDate.ToString());

(Note that the ToString call is actually needed, since BuildDate here is a class and not an object, so you can't rely on the usual implicit conversion to string.)

Second, the script is also a MonoBehaviour, which you can attach to a UI.Text component.  In this case it will automatically set the text to the build date, using the format string you specify.  The default ("g") produces a short date/time format, which on my system comes out to something like "10/29/15 08:54".

So how does it work?

C# (or any .NET) applications are built out of "assemblies" which are similar to libraries or object libraries.  That [assembly:AssemblyVersion] directive at the top of the script tells the build system what you want for the version number of the assembly containing that code.  The four fields are major version, minor version, build number, and revision.

We've set the major and minor version numbers (which really don't matter, unless you're doing something fancy) to 1.0.  But we set both the build number and the revision number to "*".  This is a bit of magic (documented here) that causes the build number to be automatically set to the number of days since January 1, 2000, and the revision number to be set to the number of seconds since midnight, divided by two (don't ask me, it's not my fault).

So, with the assembly version set that way at build time, we can use a bit of reflection to get the version number back out at runtime, and do a bit of math to convert this back into a proper date.

It's roundabout, but it works — and now you can easily display the build date in your Unity apps.