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?
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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With