Saving build number to a file inside build data folder

Hello,

I hope I’ve posted this in the right section and all, I’m new to this Unity Answers Thing, so forgive me any mistakes I could have done. :slight_smile:

But now to my problem, I’ve been stuck with this a couple days now, but I just can’t get it to work.
So basically, I want to, whenever I build my game, to increment a variable containing information about the version of the product and the current build number, save a binary file containing that information INTO the GAME DATA folder (not %appdata% or anything, but pack it right withing the game itself) and then anytime the game is played (!Application.isEditor), load that file, deserialize it, get the build and print it onto an UI, so the user knows exactly which version and build he is using.

Saving and loading works and incrementing the build number and all that stuff, the problem comes now - when I have to decide how and where to save the file.

I’ve found method of return type string called Application.dataPath, which gives me the path to the data folder. When I print this while inside the editor, this gives me the path to Assets/ folder.
So I thought I’ve suceeded now.

But when the game is build, that assets folder gets packed into various files and folders and I have no idea where it at the end lands. When I print this Application.dataPath string while playing the built .exe though, it leads to the folder next to my .exe called _Data, but this one doesn’t contain the files from Assets/, so even my file that contained the build number and version is gone. It exists, but it is somewhere packed inside the .assets files or something like that.

Anyone got some idea how I could fix this? Or even a complete different, better way of implementing such build and version tracking?

Thanks in advance,
Skrypt

After 7 hours of trying, I finally found out on my own.

For anyone who has the same problem, here is how I proceeded:

I’ve created a script that handles saving / loading data and assigned it to a gameobject. This script has also the build number variable assigned. And using Singleton it is added in each scene and only one at a time.
On the Start() Method I create a file with the build number set to 1.

Next, I created an editorScript with an PostProcessBuildAttribute assigned to a function, that gets the final builded project data folder. Into that folder I create a a new file with the data, that I load from the previously saved file inside the assets folder, incremented by 1. And save both files. That way, anytime I build the project, it gets the the value from the version info file in the assets, increment it by 1 and save the new incremented value into both files, one inside the assets for later use and one pasted directly inside the final game data folder.

Anytime I now want to access this build value, I use Application.dataPath to get access to the data folder, if run in editor this will be assets, if run standalone this will be _Data. Then just read the file, deserialize it (if you save it as binary files) and print the content whereever you want.

I hope this makes the whole thing clear, if not, feel free to spam me with any questions you might have about this, I will be more than happy to try and help you as best as I can. :slight_smile:

regards,
Skrypt

You don’t need to do all of that…

This may only work if you’re using Visual Studio, but you can use the AssemblyVersion attribute and you’ll get auto-incrementing version information that you can extract directly from the assembly.

using System.Reflection;

[assembly: AssemblyVersion("0.1.*")]
public class AssetManager
{
    static string version = null;

    public static string GetVersion()
    {
        if (version == null)
        {
            try
            {
                version = Assembly.GetExecutingAssembly().GetName().Version.ToString();
            }
            catch
            {
                version = "Unknown";
            }
        }

        return version;
    }
}