【问题标题】:Get App Bundle Version in Unity3d在 Unity3d 中获取 App Bundle 版本
【发布时间】:2013-06-20 07:58:50
【问题描述】:

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

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

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

【问题讨论】:

  • 捆绑包,它位于何处:在您自己的服务器上还是托管的应用内购买内容?捆绑包的责任是什么?真实资产或仅触发应用更新的版本控制信息?
  • 如果您仍然想要获得捆绑版本的解决方案,请查看下面我更新的答案。
  • 我已经发布了游戏,但是会在以后的项目中看看它!谢谢!
  • @Sunkas 接受的答案已过时 - 你能更改接受的答案吗?塔。
  • 我已经更改了接受的答案。但是,我还没有验证它是否正确。这是 StackOverflow 上的正确方法吗?

标签: c# unity3d


【解决方案1】:

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

更新

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

BundleVersionChecker at github
Usage and more details


我刚刚找到了另一个非常方便的解决方案。需要 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 生成的简单类,它可以从您的代码中访问。 只要它的版本字符串不等于在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) //...

【讨论】:

  • 效果很好。谢谢!
  • Unity 升级到 5.3.2f1,MonoDevelop 升级到 5.9.6 后,它停止工作。我现在在 BundleVersionChecker.cs 中遇到构建错误:当前上下文中不存在名称“CurrentBundleVersion”。 Unity 在编辑器和游戏类之间的符号暴露方面是否发生了变化?我尝试在定义 CurrentBundleVersion 类之前添加 [InitializeOnLoad](在 UnityEditor 命名空间中),但它没有帮助:(
  • OK 我设法通过不检查 BundleVersionChecker() 构造函数中的版本更改来解决构建错误,并且总是直接调用 CreateNewBuildVersionClassFile(PlayerSettings.bundleVersion)。我没有看到可怕的性能影响,因为每次 Unity 编辑器窗口获得焦点时都会调用它。
  • 抱歉评论中缺少代码格式:static BundleVersionChecker () { string bundleVersion = PlayerSettings.bundleVersion; Debug.Log("替换bundle版本" + bundleVersion + "在文件\"" + TargetCodeFile + "\""); CreateNewBuildVersionClassFile (bundleVersion); }
  • 对于 Unity 2018.x.x,我刚刚创建了两个 CurrentBundleVersion 文件,CurrentBundleVersionEditorCurrentBundleVersion。我更新它们,并从运行时或从我使用的一个或另一个的编辑器中更新。
【解决方案2】:

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

【讨论】:

    【解决方案3】:

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

    一句话:不。你不能直接从 Unity 获取 app bundle 版本。

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

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-02-08
      • 2013-02-17
      • 2020-11-19
      • 1970-01-01
      • 2021-10-10
      • 2014-06-14
      • 1970-01-01
      相关资源
      最近更新 更多