Current code is something like this
#if UNITY_EDITOR
if (!Application.isPlaying)
{
System.DateTime date = System.DateTime.Now.ToLocalTime();
buildDateStamp = date.Year + date.Month.ToString("00") + date.Day.ToString("00") + "." + date.Hour.ToString("00");
}
#endif
with [ExecuteAlways] in front of the class.
What’s the easiest way to inject that buildDateStamp at build time or even play time?
I made a pre-build step that fires and writes a new text file with the build date string in it.
You also have to reimport that asset (doing so under build script control) to make sure the change is noted as the build continues.
Then at authoring time you only have to drag that text file into a script that “pushes” its value to the UnityEngine.UI.Text object you’re using to display it.
I miss the good old __DATE__
and __TIME__
predefines that are so globally available in C/C++ compilers, as well as a lot of assemblers.
1 Like
I ended up hacking together this beauty:
using System;
using System.IO;
using UnityEngine;
#if UNITY_EDITOR
public class UpdateBuildDateTXTFile : UnityEditor.Build.IPreprocessBuildWithReport
{
public int callbackOrder => 0;
public void OnPreprocessBuild(UnityEditor.Build.Reporting.BuildReport report)
{
using (BinaryWriter Writer = new BinaryWriter(File.Open("Assets/Data/Resources/BuildDate.txt", FileMode.OpenOrCreate, FileAccess.Write)))
{
var dt = DateTime.Now.ToLocalTime();
Writer.Write(dt.ToString("yyyyMMdd.HH"));
}
}
}
#endif
3 Likes
here is the secret sauce to read the text file easily:
GetComponent<UILabel>().text = Resources.Load<TextAsset>("BuildDate").text;
1 Like