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. Dismiss Notice

Question Display date/time of build

Discussion in 'Editor & General Support' started by DaveTheCoder, Dec 11, 2020.

  1. DaveTheCoder

    DaveTheCoder

    Joined:
    Feb 26, 2020
    Posts:
    10
    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.
     
    Last edited: Dec 11, 2020
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    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.
     
  3. DaveTheCoder

    DaveTheCoder

    Joined:
    Feb 26, 2020
    Posts:
    10
    Thanks, I'll look into how to do that.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    Got back to my computator and dug up my build date script: change the text file path as you please:

    Code (csharp):
    1. using UnityEngine;
    2.  
    3. #if UNITY_EDITOR
    4. using UnityEditor;
    5. using UnityEditor.Build;
    6. #endif
    7.  
    8. public class BuildDate : MonoBehaviour
    9.  
    10. #if UNITY_EDITOR
    11. , IPreprocessBuild
    12. #endif
    13.  
    14. {
    15.     public TextAsset BuildDateTextAsset;
    16.  
    17.     public string s_BuildDate
    18.     {
    19.         get
    20.         {
    21.             return BuildDateTextAsset.text;
    22.         }
    23.     }
    24.  
    25. #if UNITY_EDITOR
    26.     public int callbackOrder { get { return 0; } }
    27.  
    28.     public void OnPreprocessBuild(BuildTarget target, string path)
    29.     {
    30.         Debug.Log("MyCustomBuildProcessor.OnPreprocessBuild for target " + target + " at path " + path);
    31.  
    32.         string builddate = System.DateTime.Now.ToString("yyyyMMdd_hhmm");
    33.         Debug.Log("builddate:" + builddate);
    34.  
    35.         string outfile = "Assets/0zeroscene/BuildDateTextFile.txt";
    36.  
    37.         Debug.Log("path = '" + outfile + "'");
    38.  
    39.         System.IO.File.WriteAllText(outfile, builddate + "\n");
    40.  
    41.         AssetDatabase.Refresh();
    42.     }
    43. #endif
    44. }
    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).
     
    DaveTheCoder likes this.
  5. DaveTheCoder

    DaveTheCoder

    Joined:
    Feb 26, 2020
    Posts:
    10
    Thanks!

    I have decades of experience with C/C++. With C#, I've only been looking at bits and pieces of the manual.
     
  6. DaveTheCoder

    DaveTheCoder

    Joined:
    Feb 26, 2020
    Posts:
    10
    Thanks for your help. I got it working.
     
  7. oliver_unity892

    oliver_unity892

    Joined:
    Oct 28, 2019
    Posts:
    91
    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.
     
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    You're on the right track. It might be sufficient to add one of these:

    https://docs.unity3d.com/ScriptReference/RuntimeInitializeOnLoadMethodAttribute.html

    onto a static method that loads the created build file using Resources.Load<TextAsset>()

    Be sure to read how Resources.Load<T>() 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.