How can I get "Bundle Version" and "Bundle Version Code" through script?

It’s been 12 years. Can you expose the field already? It can’t be that hard can it? :slight_smile:

2 Likes

Here’s a version that does not need to have a GameVerison.cs script in the folder first :
(Otherwise it throws a file not found exception)

string currentText = "";

            if (File.Exists(finalPath))
            {
                currentText = File.ReadAllText(finalPath);
            }

Also, it might be good to add a warning :

newText += "\n // This code is automatically generated by VersionWriter. Do NOT edit!";

Hi guys, I think I have an easy solution, this fixes the issue on the editor and shows correctly in the devices.

using TMPro;
using UnityEngine;

#if UNITY_EDITOR
using UnityEditor;
#endif

public class VersionText : MonoBehaviour
{
    [SerializeField] private TMP_Text _text;

    private void OnValidate()
    {
#if UNITY_EDITOR
        _text.text = "v" + Application.version + "." + PlayerSettings.Android.bundleVersionCode;
#endif
    }
}
8 Likes

Please read the thread. 20 people have suggested this exact solution already and I’ll be the 21st person to say: this. does. not. work. in. Builds. :slight_smile:

2 Likes

I wrote a Unity plugin that lets you get the version code or bundle version natively on Android and iOS.
https://github.com/natewilliford/unity-mobile-version-code

2 Likes

Please read the code. Its actually viable and easy solution. :slight_smile:

3 Likes

Yeah, Peecha is right, @xWiggle 's solution will work (if you attach this script to some prefab or scene object), and is darn simple. Use OnValidate to get the correct values at editor time, but serialize them on the object so you can retrieve them at runtime. Clever!

1 Like

Sorry you are right! Sorry @xWiggle - my eyes glazed over when I read bundleVersionCode inside #if UNITY_EDITOR block. I just tested it and very clever! @hungryish 's solution also works for me.

Still seems like Unity should be responsible and just expose this field instead of having people come up with all sorts of hacks that have appeared in this thread. It shouldn’t need to be clever, it should be standard, easy to retrieve value. I can see the Version string in the Player settings and read it in code… so why not the Bundle Version Code?

3 Likes

Can you show an example on how you did?

You can do it only manually. The best way it’s scriptable object, look at my solution.

For use you need:

  • download scripts in project(from message or zip file from attachment)
  • create scriptable object “BuildRuntimeInfoSettings” in “Resource” folder by project context action ''Assets/Create/Build runtime info settings"
  • (preffered) add this scriptable object in project setting → other settings → ‘preload assets’ list
  • before build set versions to PlayerSettings.bundleVersion and PlayerSettings.Android.bundleVersionCode or PlayerSettings.iOS.buildNumber
  • just build
  • in runtime get values from BuidRuntimeInfo.Instance.Version or BuidRuntimeInfo.Instance.BuildId
  • example BuildIdView.cs

BuildInfoUtility/BuildRuntimeInfo.cs

using UnityEngine;

namespace BuildInfoUtility
{
   [CreateAssetMenu(fileName = "BuildRuntimeInfoSettings", menuName = "Build runtime info settings")]
   public class BuidRuntimeInfo : ScriptableObject
   {
      public static BuidRuntimeInfo Instance => _instance ??= LoadInfo();
      private static BuidRuntimeInfo _instance;

      public string Version;
      public string BuildId;

      private static BuidRuntimeInfo LoadInfo()
      {
         BuidRuntimeInfo result = Resources.Load<BuidRuntimeInfo>("BuildRuntimeInfoSettings");
         if (result == null)
         {
            Debug.LogError("You need create asset file before build from context menu \"Assets/Create/Build runtime info settings\"");
         }

         return result;
      }
   }
}

BuildInfoUtility/Editor/BuidRuntimeInfoPreprocess.cs

using UnityEditor;
using UnityEditor.Build;
using UnityEditor.Build.Reporting;

namespace BuildInfoUtility
{
   public class BuidRuntimeInfoPreprocess : IPreprocessBuildWithReport
   {
      public int callbackOrder => default;

      public void OnPreprocessBuild(BuildReport report)
      {
         BuidRuntimeInfo settings = BuidRuntimeInfo.Instance;
         if (settings != null)
         {
            settings.Version = PlayerSettings.bundleVersion;
            settings.BuildId = GetBuildId();
            EditorUtility.SetDirty(settings);
            AssetDatabase.SaveAssetIfDirty(settings);
         }
      }

      private string GetBuildId()
      {
#if UNITY_ANDROID
         return PlayerSettings.Android.bundleVersionCode.ToString();
#elif UNITY_IOS
         return PlayerSettings.iOS.buildNumber.ToString();
#else
         return default;
#endif
      }
   }
}

BuildInfoUtility/BuildIdView.cs

using UnityEngine;
using UnityEngine.UI;

namespace BuildInfoUtility
{
   public class BuildIdView : MonoBehaviour
   {
      [SerializeField]
      private Text _text;

      private void Reset()
      {
         _text = GetComponent<Text>();
         _text.text = $"Build ID: ***";
      }

      private void Start()
      {
         if (BuidRuntimeInfo.Instance != null)
         {
            _text.text = $"Build ID: {BuidRuntimeInfo.Instance.BuildId}";
         }
      }
   }
}

8787451–1193659–BuildInfoUtility.zip (3.02 KB)

1 Like

It’s not flexible solution, because:

It’s stupid but this is how Meta does it when creating new code versions:

    public void OnPostprocessBuild(BuildReport report)
    {
#if UNITY_ANDROID
        if (autoIncrementVersion)
        {
            if ((report.summary.options & BuildOptions.Development) == 0)
            {
                PlayerSettings.Android.bundleVersionCode++;
                UnityEngine.Debug.Log("Incrementing version code to " + PlayerSettings.Android.bundleVersionCode);
            }
        }
       // ...
}

2023 and still can’t access build info (sorry I just had to bump this)

Just bumping the sh$t out of it, I think we’d might have the longest time record for a feature request ignorance from Unity.

No. That trophy belongs to the resizable editor font.