I’ve searched the forums for something to allow me to do version numbering on my game, but I couldn’t find anything specific. Does anyone have a nice solution for version numbering?
I’ve been programming a little Android game lately but I’ve been having problems knowing which version my other team mates are commenting on. I added a simple version number string to help, but I kept forgetting to increment it before pushing out a build for others to try so made a little script to do it automagically.
The logic is pretty simple. I have it read, increment and then save a text file that has a string in the format “0.0.0.text”. I control the “0.0” values by modifying the text file, but the last number is incremented any time the game code is compiled, or the game is run in the editor.
Let me know what you think.
Put this in an Editor folder (copy text below or download attached C# file).
//inspired by http://answers.unity3d.com/questions/45186/can-i-auto-run-a-script-when-editor-launches-or-a.html
using UnityEngine;
using UnityEditor;
using System.IO;
[InitializeOnLoad]
public class VersionIncrementor
{
static VersionIncrementor() {
//If you want the scene to be fully loaded before your startup operation,
// for example to be able to use Object.FindObjectsOfType, you can defer your
// logic until the first editor update, like this:
EditorApplication.update += RunOnce;
}
static void RunOnce() {
EditorApplication.update -= RunOnce;
ReadVersionAndIncrement();
}
static void ReadVersionAndIncrement() {
//the file name and path. No path is the base of the Unity project directory (same level as Assets folder)
string versionTextFileNameAndPath = "version.txt";
string versionText = CommonUtils.ReadTextFile(versionTextFileNameAndPath);
if (versionText != null) {
versionText = versionText.Trim(); //clean up whitespace if necessary
string[] lines = versionText.Split('.');
int MajorVersion = int.Parse(lines[0]);
int MinorVersion = int.Parse(lines[1]);
int SubMinorVersion = int.Parse(lines[2]) + 1; //increment here
string SubVersionText = lines[3].Trim();
Debug.Log("Major, Minor, SubMinor, SubVerLetter: " + MajorVersion + " " + MinorVersion + " " + SubMinorVersion + " " + SubVersionText);
versionText = MajorVersion.ToString("0") + "." +
MinorVersion.ToString("0") + "." +
SubMinorVersion.ToString("000") + "." +
SubVersionText;
Debug.Log("Version Incremented " + versionText);
//save the file (overwrite the original) with the new version number
CommonUtils.WriteTextFile(versionTextFileNameAndPath, versionText);
//save the file to the Resources directory so it can be used by Game code
CommonUtils.WriteTextFile("Assets/Resources/version.txt", versionText);
//tell unity the file changed (important if the versionTextFileNameAndPath is in the Assets folder)
AssetDatabase.Refresh();
}
else {
//no file at that path, make it
CommonUtils.WriteTextFile(versionTextFileNameAndPath, "0.0.0.a");
}
}
}
It uses some simple read/write text file logic which I have in a Scripts/Common directory. I’ve simplified it for this and called it CommonUtils.cs
using UnityEngine;
using System.Collections;
using System.IO;
public class CommonUtils
{
public static string ReadTextFile(string sFileName)
{
//Debug.Log("Reading " + sFileName);
//Check to see if the filename specified exists, if not try adding '.txt', otherwise fail
string sFileNameFound = "";
if (File.Exists(sFileName))
{
//Debug.Log("Reading '" + sFileName + "'.");
sFileNameFound = sFileName; //file found
}
else if (File.Exists(sFileName + ".txt"))
{
sFileNameFound = sFileName + ".txt";
}
else
{
Debug.Log("Could not find file '" + sFileName + "'.");
return null;
}
StreamReader sr;
try
{
sr = new StreamReader(sFileNameFound);
}
catch (System.Exception e)
{
Debug.LogWarning("Something went wrong with read. " + e.Message);
return null;
}
string fileContents = sr.ReadToEnd();
sr.Close();
return fileContents;
}
public static void WriteTextFile(string sFilePathAndName, string sTextContents)
{
StreamWriter sw = new StreamWriter(sFilePathAndName);
sw.WriteLine(sTextContents);
sw.Flush();
sw.Close();
}
}
991658–36711–$VersionIncrementor.cs (2.58 KB)
991658–36712–$CommonUtils.cs (1.37 KB)