x


How can a game automatically display the date/time of when it was built, and/or an incremented revision string?

Sometimes it is useful for a game to display version information, or a revision number such as is assigned by version control or continuous integration solution. What's a good way to do this for a Unity game?

more ▼

asked Nov 30, 2009 at 02:14 AM

Bampf gravatar image

Bampf
5.7k 14 58 71

Jaap Kreijkamp already has a solution for build-time; I'd like him to move that answer over here. But I'd also welcome solutions for embedding information such as the SubVersion revision.

Nov 30, 2009 at 02:16 AM Bampf
(comments are locked)
10|3000 characters needed characters left

7 answers: sort voted first

The usual way how this is done is by having keywords which are expanded by the version control on checkin. You can then use that keyword in a string in your game and simply display that.

Something like:

const string GAME_VERSION = "$Date: $"; // that's how it would be done with CVS

public void OnGUI() { GUI.Label(new Rect (10, 10, 100, 20), GAME_VERSION); }

Now, as far as I know, Asset Server doesn't have such keywords (that would be a feature-request ;-) ). Examples of keywords used by other version control systems:

Of course, you might want to be doing some string-cleanup to remove the "cryptic" parts of the version control keywords.

Personally, I prefer to use labels I manually assign for releases (you could do that in a property in one of your "core management" prefabs that you can easily change in the Unity editor and that is accessible from where ever you have the GUI code to display that information). Usually, that creates more meaningful information for actual users and seems like a small extra-step to take while creating my builds. However, using the automatic assignment of dates through version control might be helpful in "hot development" (when you push out releases every few days). Keep in mind, though: That value will only change when you actually made a change to that file.

Finally, there's yet another solution: If you have a custom build-system (which can be implemented Unity's batch mode features), you might do your own replacement in the code during the build; and you'd increase the number for each build (or include the build-date, or whichever information you find useful). You could also keep that number / text in a little text-file that your game-code can access (might be easier to set up than doing the string replacement in your code but adds a little complexity in your game-code).

In my opinion, that's the optimal solution (meaningful values + no manual work involved once it's set up + totally customizable) - but also the most complex one to set up. But when you know how to set up a build-system, that should be one of your smaller concerns ;-)

more ▼

answered Nov 30, 2009 at 10:18 AM

jashan gravatar image

jashan
11.6k 46 107 194

I like the build system solution. A simple way to do that is to put some constants in a class (AppStuff.BUILD_DATE or whatever). Have those be empty string in your project code then use scripting to create the class with the constants' values and overwrite the class at build time. Then the rest of your code need only access the constant to display the right build date, version, or whatever info you desire.

Feb 04, 2010 at 09:21 PM Ben 2
(comments are locked)
10|3000 characters needed characters left

EDIT: updated code to make copy of string before returning to C#

Okay here it is:

To be able to show date/time of a build (useful to make sure your testers are using the right version) I made a very simple plugin that adds two functions to retrieve date and time string of the build process. To use it, add the Test.h and Test.mm to the xcode project and JBuildInfo.cs to your unity project. Shouldn't be hard to figure out how to use this I guess...

BuildInfo.h:

// BuildInfo.h

import <Foundation/Foundation.h>

@interface BuildInfo : NSObject { }

@end

BuildInfo.mm:

// BuildInfo.mm

import "BuildInfo.h"

@implementation BuildInfo

@end

// Helper method to create C string copy static char* MakeStringCopy (const char* string) { if (string == NULL) return NULL;

 char* res = (char*)malloc(strlen(string) + 1);
 strcpy(res, string);
 return res;

}

extern "C" { const char* getBuildDate() { return MakeStringCopy(DATE); }

 const char* getBuildTime() {
     return MakeStringCopy(__TIME__);
 }   

}

JBuildInfo.cs:

using UnityEngine; using System.Collections; using System.Runtime.InteropServices;

public class JBuildInfo {

 [DllImport("__Internal")] private static extern string getBuildDate();
 [DllImport("__Internal")] private static extern string getBuildTime();

 public static string GetBuildDate() {
     if (Application.isEditor) {
         return "&lt;not build&gt;";
     }
     else {
         return getBuildDate();
     }
 }


 public static string GetBuildTime() {
     if (Application.isEditor) {
         return "--:--:--";
     }
     else {
         return getBuildTime();
     }
 }

}

more ▼

answered Nov 30, 2009 at 02:26 AM

Jaap Kreijkamp gravatar image

Jaap Kreijkamp
7.1k 44 58 108

Has anyone tried to use this recently? In Xcode 3.2.5 / SDK 4.2, I receive this runtime error when calling either getBuildDate() or getBuildTime()

malloc: *** error for object 0xce35b0: pointer being freed was not allocated

Dec 04, 2010 at 01:08 AM CapnCromulent

You're right, code was wrong, but never gave problems with Unity iPhone 1.7.x, fixed it :-)

Dec 31, 2010 at 07:13 AM Jaap Kreijkamp

I think it should be noted that this is a iPhone specific solution.

Nov 30, 2009 at 10:03 AM jashan
(comments are locked)
10|3000 characters needed characters left

You can use a compile-time language to create a string. At least I know that Boo has this.

 import UnityEngine

 

 macro build_buildtime_info():

     dateString = System.DateTime.Now.ToString()

     yield [|

         class BuildtimeInfo:

             static def DateTimeString() as string:

                 return "${$(dateString)}"

     |]

 

 build_buildtime_info

 

 # # Now you can do this:

 # Debug.Log( BuildtimeInfo.DateTimeString() )






Edit:

Unity actually opens every scene during the build process: This can be exploited as follows:

 using UnityEngine;
 
 [ExecuteInEditMode]
 public class RefreshTestCS: MonoBehaviour
 {
     public string buildName;
 
     public void OnGUI() {
         GUILayout.Label("Build name: " + buildName);
     }
 
 #if UNITY_EDITOR
     public void Awake() {
         if (Application.isEditor && !Application.isPlaying) {
             buildName = System.DateTime.Now.ToString() + " (" + (UnityEditor.BuildPipeline.isBuildingPlayer ? "during build)" : "during edit)");
         }
     }
 #endif
 }


more ▼

answered Jan 16, 2013 at 04:13 PM

steinbitglis gravatar image

steinbitglis
429 43 44 51

Has anyone done the above in C#?

Feb 12, 2013 at 09:11 PM wrxmarcus

I don't think C# can do this, but you can use this class from C# if you place the script under /Assets/Plugins, and the C# code somewhere else.

Feb 12, 2013 at 09:27 PM steinbitglis

Thanks. Still being quite new to this, do you know how I would access this info from C#? Does it require an extern - and if so how would I set that up for Boo? I assume I should put this in a file called "BuildtimeInfo.BOO" UPDATE: I find you can call this directly from C#. Too bad Visual Studio is sure this is an error.

Feb 12, 2013 at 10:57 PM wrxmarcus

My .unity3d web player binary increased by over 500K simply by adding this .boo file to my project :( . Maybe Unity is including a large Boo runtime.

May 25, 2013 at 10:17 PM darrinm

maybe it's debug symbols for boo? As far as I know, there is no "boo" runtime, only ordinary mono.

May 25, 2013 at 10:37 PM steinbitglis
(comments are locked)
10|3000 characters needed characters left

For iPhone projects, you can use Apple's built-in tool, agvtool, described here. You can probably even use Jaap's answer to provide the generated strings to Unity.

more ▼

answered Nov 24, 2010 at 07:14 PM

CapnCromulent gravatar image

CapnCromulent
306 2 4 24

(comments are locked)
10|3000 characters needed characters left

There is another way for those who don't want to use source control keywords or a custom build pipeline. The idea is to read the COFF header as described here ("The new way"): http://stackoverflow.com/a/1601079/786656

Now, it's Unity so there is no main assembly. What I check instead is the DLL built from my C# scripts which is in "/Managed/Assembly-CSharp.dll". Note that it only works outside of the Unity Editor, with an actual built game.

To summarize (C#):

 public static string GetScriptBuildDateTime()
 {
     string scripDllPath = Path.Combine(Application.dataPath, "Managed/Assembly-CSharp.dll");
     return GetBuildDateTime(scripDllPath).ToString("yyyy-MM-dd HH:mm");
 }
 
 #pragma warning disable 0649
 struct _IMAGE_FILE_HEADER
 {
     public ushort Machine;
     public ushort NumberOfSections;
     public uint TimeDateStamp;
     public uint PointerToSymbolTable;
     public uint NumberOfSymbols;
     public ushort SizeOfOptionalHeader;
     public ushort Characteristics;
 };
 
 public static DateTime GetBuildDateTime(string objectPath)
 {
     if (File.Exists(objectPath))
     {
         var buffer = new byte[Math.Max(Marshal.SizeOf(typeof(_IMAGE_FILE_HEADER)), 4)];
         using (var fileStream = new FileStream(objectPath, FileMode.Open, FileAccess.Read))
         {
             fileStream.Position = 0x3C;
             fileStream.Read(buffer, 0, 4);
             fileStream.Position = BitConverter.ToUInt32(buffer, 0); // COFF header offset
             fileStream.Read(buffer, 0, 4); // "PE\0\0"
             fileStream.Read(buffer, 0, buffer.Length);
         }
         var pinnedBuffer = GCHandle.Alloc(buffer, GCHandleType.Pinned);
         try
         {
             var coffHeader = (_IMAGE_FILE_HEADER)Marshal.PtrToStructure(pinnedBuffer.AddrOfPinnedObject(), typeof(_IMAGE_FILE_HEADER));
 
             return TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1) + new TimeSpan(coffHeader.TimeDateStamp * TimeSpan.TicksPerSecond));
         }
         finally
         {
             pinnedBuffer.Free();
         }
     }
     return new DateTime();
 }
more ▼

answered Nov 23, 2012 at 08:32 AM

ThibaultD gravatar image

ThibaultD
117 8 13 15

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x77

asked: Nov 30, 2009 at 02:14 AM

Seen: 4283 times

Last Updated: Apr 25 at 12:17 PM