Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get App Bundle Version in Unity3d

Tags:

c#

unity3d

Simple question, but seems very hard to find.

I am building an Android and iOS game. And I want to extract the version (i.e. "2.0.1") of the app (to display a popup if there is a newer version on App Store/Google Play).

Anyone know how to do this programmatically?

like image 724
Sunkas Avatar asked Jun 20 '13 07:06

Sunkas


People also ask

How do I check the version of an app in Unity?

Returns application version number (Read Only). This function returns the current version of the Application. This is read-only. To set the version number in Unity, go to Edit > Project Settings > Player.

What is bundle version code Unity?

Bundle Version Code: An internal version number. This number is used only to determine whether one version is more recent than another, with higher numbers indicating more recent versions.

How do I get AAB in Unity?

If you select Build Unity will generate the AAB file which can be published directly to Google Play. If you select Build and Run, Unity will generate the AAB file, then it will generate temporary APK file(s) specific for the attached device, install them on your device and run your application.

How do I find the version of a game in Unity?

Open the file globalgamemanagers (newer Unity) or mainData (older Unity) Sort the table by Type and find the one 'Unnamed asset' that has the type BuildSettings. View the data for that, then expand 'BuildSettings Base', the Unity build version is in m_Version.


2 Answers

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) //...

like image 183
Kay Avatar answered Oct 12 '22 07:10

Kay


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

like image 23
2manyprojects Avatar answered Oct 12 '22 07:10

2manyprojects