Get App Bundle Version in Unity3d

OUTDATED: While this answer was perfectly valid at time of writing, the information it contains is outdated. There is a better way to do this now, see this answer instead. The answer has been preserved for historic reasons.

Update

I improved the solution described below massively and made an open source project (MIT license) hosted at github out of it. At a glance it does not only provides access to the bundle version of the currently running app but also tracks the history of previous bundle version in a pretty convienient way - at least since installation of the plugin or some manual adjustments are required.
So look at:

BundleVersionChecker at github
Usage and more details


I just found another and pretty convenient solution. 2 classes are needed:

The first one is an editor class checking the current PlayerSettings.bundleVersion. I called it BundleVersionChecker and it has to be placed in Assets/Editor. It functions as a code generator that simply generates a very simple static class containing just a version string, if the bundle version has changed:

using UnityEngine;
using UnityEditor;
using System.IO;

[InitializeOnLoad]
public class BundleVersionChecker
{
    /// <summary>
    /// Class name to use when referencing from code.
    /// </summary>
    const string ClassName = "CurrentBundleVersion";

    const string TargetCodeFile = "Assets/Scripts/Config/" + ClassName + ".cs";

    static BundleVersionChecker () {
        string bundleVersion = PlayerSettings.bundleVersion;
        string lastVersion = CurrentBundleVersion.version;
        if (lastVersion != bundleVersion) {
            Debug.Log ("Found new bundle version " + bundleVersion + " replacing code from previous version " + lastVersion +" in file \"" + TargetCodeFile + "\"");
            CreateNewBuildVersionClassFile (bundleVersion);
        }
    }

    static string CreateNewBuildVersionClassFile (string bundleVersion) {
        using (StreamWriter writer = new StreamWriter (TargetCodeFile, false)) {
            try {
                string code = GenerateCode (bundleVersion);
                writer.WriteLine ("{0}", code);
            } catch (System.Exception ex) {
                string msg = " threw:\n" + ex.ToString ();
                Debug.LogError (msg);
                EditorUtility.DisplayDialog ("Error when trying to regenerate class", msg, "OK");
            }
        }
        return TargetCodeFile;
    }

    /// <summary>
    /// Regenerates (and replaces) the code for ClassName with new bundle version id.
    /// </summary>
    /// <returns>
    /// Code to write to file.
    /// </returns>
    /// <param name='bundleVersion'>
    /// New bundle version.
    /// </param>
    static string GenerateCode (string bundleVersion) {
        string code = "public static class " + ClassName + "\n{\n";
        code += System.String.Format ("\tpublic static readonly string version = \"{0}\";", bundleVersion);
        code += "\n}\n";
        return code;
    }
}

The 2nd class is called CurrentBundleVersion. It's the above mentioned simple class generated by BundleVersionChecker and it is accessible from your code. It will be regenerated by BundleVersionChecker automatically whenever its version string is not equal to the one found in PlayerSettings.

public static class CurrentBundleVersion
{
    public static readonly string version = "0.8.5";
}

Because it is generated code you don't have to care about it, just commit it into your version control system.

So anywhere in your code you can just write:

if (CurrentBundleVersion != "0.8.4") {
    // do migration stuff
}

I am currently working on a more sophisticated version. This will contain some version tracking to do something like

if (CurrentBundleVersion.OlderThan (CurrentBundleVersion.Version_0_8_5) //...


The UnityEngine.Application.version is a static member that seems to be the runtime equivalent of UnityEditor.PlayerSettings.bundleVersion.


OUTDATED: While this answer was perfectly valid at time of writing, the information it contains is outdated. There is a better way to do this now, see this answer instead. The answer has been preserved for historic reasons, please consider this before down-voting.

In a word: No. You can not get the app bundle version directly from Unity.

In fact, there is a function called PlayerSettings.bundleVersion which can read what the number you set in the player setting, but unfortunately it is an editor class function so you are not able to use it in runtime. (In fact you can change this number in Xcode, so the number set in Unity's player setting might be wrong).

An easy way is writing your version number in the code, and update the number every time you submit and update your app. It is a little dangerous because you could forget to do it before release. So you may need a check list for it.

Another way is write a plugin. Xcode SDK has a method to get the app's version from info plist. You can just return this to Unity for your purpose.

[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"]

Tags:

C#

Unity3D