16

简单的问题,但似乎很难找到。

我正在构建一个 Android 和 iOS 游戏。我想提取应用程序的版本(即“2.0.1”)(如果 App Store/Google Play 上有更新版本,则显示一个弹出窗口)。

任何人都知道如何以编程方式执行此操作?

4

3 回答 3

17

已过时:虽然此答案在撰写本文时完全有效,但其中包含的信息已过时。现在有更好的方法可以做到这一点,请参阅此答案。由于历史原因,答案被保留了下来。

更新

我大量改进了下面描述的解决方案,并从中制作了一个托管在 github 上的开源项目(MIT 许可证)。一目了然,它不仅提供了对当前运行应用程序捆绑版本的访问,而且还以一种非常方便的方式跟踪以前捆绑版本的历史——至少因为需要安装插件或进行一些手动调整。
所以看看:

GitHub 上的 BundleVersionChecker
用法和更多详细信息


我刚刚找到了另一个非常方便的解决方案。需要2个类:

第一个是检查当前的编辑器类PlayerSettings.bundleVersion。我打电话给它BundleVersionChecker,它必须放在资产/编辑器中。它用作代码生成器,如果捆绑版本已更改,则仅生成一个非常简单的静态类,其中仅包含版本字符串:

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;
    }
}

第二类被称为CurrentBundleVersion。它是上面提到的由您生成的简单类,BundleVersionChecker可以从您的代码中访问它。每当BundleVersionChecker其版本字符串不等于PlayerSettings.

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

因为它是生成的代码,您不必关心它,只需将其提交到您的版本控制系统中。

因此,您可以在代码中的任何位置编写:

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

我目前正在开发一个更复杂的版本。这将包含一些版本跟踪来执行类似的操作

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

于 2013-09-11T00:16:19.027 回答
13

UnityEngine.Application.version是一个静态成员,似乎是 UnityEditor.PlayerSettings.bundleVersion 的运行时等效项

于 2016-06-13T19:21:41.853 回答
7

已过时:虽然此答案在撰写本文时完全有效,但其中包含的信息已过时。现在有更好的方法可以做到这一点,请参阅此答案。由于历史原因,答案已被保留,请在否决之前考虑这一点。

一句话:不。您不能直接从 Unity 获取应用程序包版本。

实际上,有一个名为PlayerSettings.bundleVersion的函数可以读取您在播放器设置中设置的数字,但不幸的是它是一个编辑器类函数,因此您无法在运行时使用它。(实际上你可以在 Xcode 中更改这个数字,所以 Unity 的播放器设置中设置的数字可能是错误的)。

一种简单的方法是在代码中写入版本号,并在每次提交和更新应用时更新版本号。这有点危险,因为您可能会在发布前忘记这样做。因此,您可能需要一份清单。

另一种方法是编写插件。Xcode SDK 有一种从 info plist 获取应用程序版本的方法。您可以出于您的目的将其返回给 Unity。

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

于 2013-06-20T10:46:06.467 回答