c# - 在 Unity3d 中获取 App Bundle 版本

标签 c# unity3d

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

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

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

最佳答案

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.

更新

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

BundleVersionChecker at github
Usage and more details


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

第一个是检查当前 PlayerSettings.bundleVersion 的编辑器类。我将其命名为 BundleVersionChecker,它必须放在 Assets/Editor 中。它用作代码生成器,如果包版本发生变化,它会简单地生成一个非常简单的静态类,其中只包含一个版本字符串:

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 生成的上面提到的简单类,可以从您的代码中访问它。 只要它的版本字符串不等于 PlayerSettings 中找到的版本,它就会由 BundleVersionChecker 自动重新生成。

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

关于c# - 在 Unity3d 中获取 App Bundle 版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17208261/

相关文章:

c# - 从母版代码隐藏的子页面获取信息

c# - 为什么这里不能隐含类型参数的泛型(有两个类型参数)?

c# - 设计一个应用程序,但我可以在应用程序运行时创建新的 MySQL 字段吗?

c# - 击退敌人

c# - 我使用rigidbody2D.velocity.y时出现语法错误

unity3d - Unity Editor 检测构建失败的时间

c# - 如何将事件从用户控件冒泡到包含该事件的窗口?

c# - 为什么调用 ClearSelected 并附加数据源后 SelectedIndex 可能为 0?

unity3d - Unity Line Renderer Color 在移动设备上始终为黑色

c# - 当 EventSystem 在场景中时,AudioSource 未在 Unity3d 中播放