I’d like to capture the date and time of the build in a script, and store it somewhere so that I can display it on a screen in the game. Is that possible?
I know that I could manually put it in the Version field in the Build/Player settings, but I’d like to automate it so that I don’t have to do that every time I build the project.
Alternatively, if there’s a way of getting the Version to automatically increment on each build, that would suffice.
It’s so irritating that C# doesn’t have a trivial way to do this the way C / C++ does (DATE and TIME).
In Unity, I made a preprocessor build step to write the file/date to a text file, which I then include in the build.
You can stick something in a file or in PlayerPrefs (in the editor) and again in a preprocessor script increment it, such as for the Android Bundle ID.
Thanks, I’ll look into how to do that.
Got back to my computator and dug up my build date script: change the text file path as you please:
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
using UnityEditor.Build;
#endif
public class BuildDate : MonoBehaviour
#if UNITY_EDITOR
, IPreprocessBuild
#endif
{
public TextAsset BuildDateTextAsset;
public string s_BuildDate
{
get
{
return BuildDateTextAsset.text;
}
}
#if UNITY_EDITOR
public int callbackOrder { get { return 0; } }
public void OnPreprocessBuild(BuildTarget target, string path)
{
Debug.Log("MyCustomBuildProcessor.OnPreprocessBuild for target " + target + " at path " + path);
string builddate = System.DateTime.Now.ToString("yyyyMMdd_hhmm");
Debug.Log("builddate:" + builddate);
string outfile = "Assets/0zeroscene/BuildDateTextFile.txt";
Debug.Log("path = '" + outfile + "'");
System.IO.File.WriteAllText(outfile, builddate + "\n");
AssetDatabase.Refresh();
}
#endif
}
If you’re using source control, check the produced text file in at least once so you don’t get nullrefs when you clone your project to a new directory and run, and that text file hasn’t been made (it only gets made on build).
1 Like
Thanks!
I have decades of experience with C/C++. With C#, I’ve only been looking at bits and pieces of the manual.
Thanks for your help. I got it working.
Hi
Is it possible to get the build date and time and show it at run time?
Looking at the code, I should be able to do something like mytext=BuildDate.s_BuildDate, however BuildDateTextAsset doesn’t appear to ever be set with the text asset file.
You’re on the right track. It might be sufficient to add one of these:
onto a static method that loads the created build file using Resources.Load()
Be sure to read how Resources.Load() works in the docs.
Otherwise the scope of availability of the code above is only for itself, so it assumes if it is present, you have dragged the file in.