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 Customizing csproj files to autogenerate AssemblyInfo.cs - CodeProject. 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, .net - How can I auto increment the C# assembly version via our CI platform (Hudson)? - Stack Overflow, which uses Hudson and SVN.

So, how can I make this work? What am I missing?

Thank you!

There is no completely automatic solution for this.

The easier way would be to implement your own build method using BuildPipeline.BuildPlayer and before calling it just write your version number to the location you want it (Using the Process class you could also call git for the current version).

This has the disadvantage that somebody could still try to build using the normal unity interface and not get the correct build number.

You might also be able to use PostProcessBuild to get the version number after a build and then inject it into it (either as an external file or by rewriting assemblies if you really have to).

To use the methods you found for csproj files you could roll your own csproj/sln instead of auto generating them and only add the final built assemblies to your project.

1 Like

I actually came across a really simple solution, which is to create a file with only the version number on it and a class with [InitializeOnLoad] tag, which its constructor is static and will parse the version file, increment it and write it.

Its somewhat similar to the post seen here c# - Get App Bundle Version in Unity3d - Stack Overflow and here How to increment a Version string everytime Unity is built? - Questions & Answers - Unity Discussions

assembly:AssemblyVersion solution throws an error because of the deterministic flag (google it if you want). I share my simple one script solution since this post is number 1 result on google and I wasted too much time implementing this.

Checkout https://epicnerf.com/unity-auto-increase-build-number/

1 Like