Unity小技巧-通过Unity脚本获取安卓端VersionCode
csharp
public static int GetVersionCode()
{
#if !UNITY_EDITOR && UNITY_ANDROID
AndroidJavaClass contextCls = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject context = contextCls.GetStatic<AndroidJavaObject>("currentActivity");
AndroidJavaObject packageMngr = context.Call<AndroidJavaObject>("getPackageManager");
string packageName = context.Call<string>("getPackageName");
AndroidJavaObject packageInfo = packageMngr.Call<AndroidJavaObject>("getPackageInfo", packageName, 0);
return packageInfo.Get<int>("versionCode");
#elif UNITY_EDITOR
return UnityEditor.PlayerSettings.Android.bundleVersionCode;
#else
return 0;
#endif
}
csharp
txtTitle.text = "myapp " + Application.version+"."+ GetVersionCode();