C# 在Unity3d中获取应用程序包版本
这个问题很简单,但似乎很难找到 我正在构建一个Android和iOS游戏。我想提取应用程序的版本(即“2.0.1”)(如果app Store/Google Play上有更新的版本,则显示一个弹出窗口) 有人知道如何通过编程实现这一点吗 过时:虽然本答案在撰写本文时完全有效,但其中包含的信息已经过时。现在有一个更好的方法来执行此操作,请参见。由于历史原因,答案已被保留,请在投票前考虑这个问题。 一句话:不可以。你不能直接从Unity获得应用包版本 事实上,有一个名为的函数可以读取您在播放器设置中设置的数字,但不幸的是,它是一个编辑器类函数,因此您无法在运行时使用它。(事实上,您可以在Xcode中更改此数字,因此Unity的播放器设置中设置的数字可能是错误的) 一个简单的方法是在代码中写入您的版本号,并在每次提交和更新应用程序时更新版本号。这有点危险,因为你可能会忘记在释放前做这件事。所以你可能需要一份清单 另一种方法是编写插件。Xcode SDK提供了一种从info plist获取应用程序版本的方法。为了你的目的,你可以把它回归到统一C# 在Unity3d中获取应用程序包版本,c#,unity3d,C#,Unity3d,这个问题很简单,但似乎很难找到 我正在构建一个Android和iOS游戏。我想提取应用程序的版本(即“2.0.1”)(如果app Store/Google Play上有更新的版本,则显示一个弹出窗口) 有人知道如何通过编程实现这一点吗 过时:虽然本答案在撰写本文时完全有效,但其中包含的信息已经过时。现在有一个更好的方法来执行此操作,请参见。由于历史原因,答案已被保留,请在投票前考虑这个问题。 一句话:不可以。你不能直接从Unity获得应用包版本 事实上,有一个名为的函数可以读取您在播放器设置中设
[[NSBundle mainBundle]infoDictionary]objectForKey:@“CFBundleVersion”]
过时:虽然本答案在撰写本文时完全有效,但其中包含的信息已经过时。现在有一个更好的方法来执行此操作,请参见。由于历史原因,答案被保留了下来
更新
我对下面描述的解决方案进行了大量改进,并利用它在github上创建了一个开源项目(MIT许可证)。乍一看,它不仅可以访问当前运行的应用程序的捆绑版本,还可以非常方便地跟踪以前捆绑版本的历史记录,至少是因为需要安装插件或进行一些手动调整。看看:
我刚刚找到了另一个非常方便的解决方案。需要两门课: 第一个是一个编辑器类,它检查当前的PlayerSettings.bundleVersion。我把它叫做
BundleVersionChecker
,它必须放在Assets/Editor中。如果bundle版本已更改,它的功能就像一个代码生成器,只生成一个非常简单的静态类,其中只包含一个版本字符串:
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;
}
}
因为它是生成的代码,所以您不必关心它,只需将它提交到您的版本控制系统中即可
因此,您可以在代码的任何地方编写:
if (CurrentBundleVersion != "0.8.4") {
// do migration stuff
}
我目前正在开发一个更复杂的版本。这将包含一些版本跟踪,以执行以下操作
if(CurrentBundleVersion.OlderThan(CurrentBundleVersion.Version\u 0\u 8\u 5)//…
是一个静态成员,似乎在运行时相当于UnityEdit.PlayerSettings.bundleVersion。捆绑包,它位于何处:在您自己的服务器上,还是托管了应用程序内购买的内容?捆绑包的责任是什么?实际资产或只是版本信息来触发应用程序更新?Have如果你还想获得捆绑版本的解决方案,请看下面我的预期答案。我已经发布了游戏,但将在未来的项目中查看它!谢谢!@Sunkas接受的答案已经过时-你能更改接受的答案吗?Ta。我已经更改了接受的答案。但是,我还没有验证它是否正确。这是StackOverflow上的正确方法吗?非常好。谢谢!在将Unity升级到5.3.2f1,并将MonoDevelop升级到5.9.6后,它停止工作。我现在在BundleVersionChecker.cs中收到一个生成错误:“CurrentBundleVersion”名称在当前上下文中不存在。Unity中是否有关于符号公开的更改在编辑器类和游戏类之间?在定义类CurrentBundleVersion之前,我尝试添加[InitializeOnLoad](在UnityEdit命名空间中),但没有任何帮助:(好的,我通过在BundleVersionChecker()构造函数中不检查版本更改,而是始终直接调用CreateNewBuildVersionClassFile来解决构建错误(PlayerSettings.bundleVersion)。我没有看到可怕的性能影响,因为每次Unity编辑器窗口获得焦点时都会调用它。很抱歉注释中缺少代码格式:static BundleVersionChecker(){string bundleVersion=PlayerSettings.bundleVersion;Debug.Log(“替换捆绑包版本”+bundleVersion+“在文件\”+TargetCodeFile+“\”;CreateNewBuildVersionClassFile(bundleVersion);}中,对于Unity 2018.x.x,我刚刚创建了两个CurrentBundleVersion
文件,CurrentBundleVersionEditor
和CurrentBundleVersion
。我会从运行时或从我使用的编辑器中更新这两个文件。
if (CurrentBundleVersion != "0.8.4") {
// do migration stuff
}