- Home /
How to increment a Version string everytime Unity is built?
Hello!
Im currently facing a problem where I need to have a string with the current version of my application and this needs to change automatically every time Unity is built. After some research Ive come across the following tag:
[assembly:AssemblyVersion ("1.0.*.*")]
But this only seems to work at the AssemblyInfo.cs file, which my .csproj isnt making, so my next step was to manually change this .csproj to include the creation of the .cs, following this article. This didnt work since everytime I run Unity this file will just get rewritten.
As a side note, Im using Mono for coding and GIT for version control, so the perfect world here would be to use GIT Revision as part of the version string I need, which should be something similar to what I found here, which uses Hudson and SVN.
So, how can I make this work? What am I missing?
Thank you!
Answer by BMayne · Dec 16, 2014 at 09:23 PM
Hey There,
It's not that hard to do this since your script are just a text file to Unity.
1) Use AssetDatabase to find the script
2) Read the file into a string.
3) Use string functions to replace the Version number.
4) Use File.WriteAll() to write the content back to the file you started off with.
5) AssetDatabase.Refresh();
Regards,
I havent tested this out yet, although I do believe this is gonna work, but I have to say.. this looks nasty haha
Ill give it a try! Thanks!
Don't worry. It's basically just a super simple version of a T4 Template (a code generator). It can be very useful in a project to have code that generates code. It makes it much harder to make a mistake.
take a look at this thread, or for something incredibly naive (if you don't need the version number accessible within the build); probably not what you want, but maybe others stumbling on this page might find it useful.
[$$anonymous$$enuItem("SSG/Build iOS Development", false, 10)]
public static void CustomBuild()
{
BumpBundleVersion();
BuildPipeline.BuildPlayer(null, "iOS", BuildTarget.iPhone, BuildOptions.Development);
}
// Bump version number in PlayerSettings.bundleVersion
private static void BumpBundleVersion()
{
float versionFloat;
if (float.TryParse(PlayerSettings.bundleVersion, out versionFloat))
{
versionFloat += 0.01f;
PlayerSettings.bundleVersion = versionFloat.ToString();
}
}
Doesnt this only works for IOS/Android?
Btw, the solution @B$$anonymous$$ayne described worked like a charm!
Thanks!
You can also just hook up to the PostProcessBuild
[PostProcessBuild]
public static void OnPostprocessBuild(BuildTarget target, string pathToBuiltProject)
{
//A new build has happened so lets increase our version number
}
The advantage here is the fact that you know this will be called even if you don't have a custom menu entry. This happens after a build but should be fine.
Answer by pdwitte · Nov 08, 2016 at 07:55 AM
As of the latest version most applications above no longer work.
Wrote and tested a script that does it for you for Android and iOS (free). Leave a star if it works :-)
Answer by SilentSin · Feb 17, 2016 at 08:01 AM
Weaver (https://www.assetstore.unity3d.com/en/#!/content/60305) has an AppDetails script which gives you automatic build numbering (as an int), as well as a build time stamp (as a DateTime).