Search Unity

Is it possible to update Version and Bundle number automatically?

Discussion in 'Scripting' started by oliver_unity892, Apr 30, 2021.

  1. oliver_unity892

    oliver_unity892

    Joined:
    Oct 28, 2019
    Posts:
    91
    Hi all

    Is there anyway to increase the version and bundle numbers of an Android project automatically with each build run?

    I seem to spend a lot of time tweaking the build number and really I'm only ever +1'ing it. I'm a sole dev so there isn't any real scheme behind the version numbering.

    I've found some old scripts online but most seem to require Unity cloud build or just no-longer work.

    Any input welcome.

    Olly
     
  2. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,089
    I did something similar recently (for cloud build but should work locally too). It calls a function in 'pre-export' that will set your build number before the build begins. It gets the build number from a free web service called https://increment.build. Just put your game code below (in the part marked ***INSERT_ID_HERE***). The web service just gives you back an ever increasing number each time you call it.

    Code (CSharp):
    1.  
    2. #if UNITY_EDITOR
    3. using UnityEditor;
    4. using UnityEngine;
    5. using UnityEditor.Callbacks;
    6. using UnityEditor.Build;
    7. using System;
    8. using System.IO;
    9. using UnityEngine.Networking;
    10.  
    11. /*     This class will increment the iOS build number on each build - this is useful for uploading builds
    12.     to TestFlight (iOS) as it requires a more recent build number on each iteration */
    13. public class IncrementBuildNumber : IPreprocessBuild
    14. {
    15.     public int callbackOrder { get { return 0; } }
    16.     public void OnPreprocessBuild(BuildTarget target, string path)
    17.     {
    18.         string build = PlayerSettings.Android.bundleVersionCode.ToString();
    19.        
    20.         // Retrieve auto-incrementing build number from server service
    21.         UnityWebRequest www = UnityWebRequest.Get( "https://increment.build/***INSERT_ID_HERE***" );
    22.         www.SendWebRequest();
    23.         while( www.isDone == false )
    24.         {
    25.         }
    26.         if( (www.isNetworkError) || (string.IsNullOrEmpty( www.downloadHandler.text )) )
    27.         {
    28.             Debug.LogErrorFormat( "Error retrieving auto-increment build number:" + www.error );
    29.         }
    30.         else
    31.         {
    32.             Debug.LogFormat( "Retrieved auto-increment build number:" + www.downloadHandler.text );
    33.             //Debug.Log( www.downloadHandler.text );
    34.             build = www.downloadHandler.text;
    35.         }
    36.         PlayerSettings.bundleVersion = "1.0";
    37.         PlayerSettings.iOS.buildNumber = build;
    38.         try
    39.         {
    40.             PlayerSettings.Android.bundleVersionCode = Convert.ToInt32( build );
    41.         }
    42.         catch
    43.         {
    44.             Debug.LogErrorFormat( "Error converting auto-increment build number to an integer: {0}" , build );
    45.         }
    46.     }
    47. }
    48. #endif
     
    Last edited: Apr 30, 2021
  3. oliver_unity892

    oliver_unity892

    Joined:
    Oct 28, 2019
    Posts:
    91
    Thanks Tone

    I'm getting an error that CloudBuild doesn't exist in the current namespace. Any ideas how I can resolve that or adjust for local building on a sole PC?

    Olly
     
  4. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,089
    Hi Olly,

    Sorry - didn't notice that it was still setup for cloud build. There's some info here on how to do a pre-build script locally: https://answers.unity.com/questions/495007/editor-script-that-executes-before-build.html

    I've updated my code in the post above to use that system so hopefully it should work now if you copy it again - I'm not at my PC just now so can't really test it. Let me know if it doesn't work for you and I'll have a look next week.

    Tone
     
  5. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,089
    p.s. I've just realised that since you're doing it locally you could probably simplify the script a lot. You can just retrieve the current android build number from PlayerSettings and increment it by one (and then set it back again). The advantage of getting it from that online service is that you can have multiple people doing builds and it'll still work - you also don't need to check in the changes to any source control.
     
  6. ccfoo242

    ccfoo242

    Joined:
    Oct 9, 2014
    Posts:
    85
    Hey I know this is from a while back but would you happen to have that original code for cloud build?
     
  7. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,089
    Sure, have posted that below for you:

    Code (CSharp):
    1. using UnityEditor;
    2. using UnityEngine;
    3. using UnityEditor.Callbacks;
    4.  
    5. using System;
    6. using System.IO;
    7. using UnityEngine.Networking;
    8.  
    9. /*     This class will increment the iOS build number on each build - this is useful for uploading builds
    10.     to TestFlight (iOS) as it requires a more recent build number on each iteration */
    11. public class IncrementBuildNumber : MonoBehaviour
    12. {
    13.     #if UNITY_CLOUD_BUILD
    14.     public static void PreExport( UnityEngine.CloudBuild.BuildManifestObject manifest )
    15.     {
    16.         string build = manifest.GetValue( "buildNumber" , null );
    17.        
    18.         // Retrieve auto-incrementing build number from server service
    19.         UnityWebRequest www = UnityWebRequest.Get( "https://increment.build/NAMEOFAPPGOESHERE" );
    20.         www.SendWebRequest();
    21.         while( www.isDone == false )
    22.         {
    23.         }
    24.        
    25.         if( (www.isNetworkError) || (string.IsNullOrEmpty( www.downloadHandler.text )) )
    26.         {
    27.             Debug.LogErrorFormat( "Error retrieving auto-increment build number:" + www.error );
    28.         }
    29.         else
    30.         {
    31.             Debug.LogFormat( "Retrieved auto-increment build number:" + www.downloadHandler.text );
    32.             //Debug.Log( www.downloadHandler.text );
    33.             build = www.downloadHandler.text;
    34.            
    35.             // Update manifest with the build number so the 'auto update app' still works
    36.             // when checking the current app build number
    37.             manifest.SetValue( "buildNumber" , build );
    38.         }
    39.        
    40.         PlayerSettings.bundleVersion = "1.0";
    41.         PlayerSettings.iOS.buildNumber = build;
    42.        
    43.         try
    44.         {
    45.             string androidBuild = Convert.ToInt32(build).ToString("D3");
    46.             PlayerSettings.Android.bundleVersionCode = Convert.ToInt32( androidBuild );
    47.         }
    48.         catch
    49.         {
    50.             Debug.LogErrorFormat( "Error converting auto-increment build number to an integer: {0}" , build );
    51.         }
    52.     }
    53.     #endif
    54. }
     
    ccfoo242 likes this.
  8. ccfoo242

    ccfoo242

    Joined:
    Oct 9, 2014
    Posts:
    85
    Awesome! Thanks!
     
  9. MxHush

    MxHush

    Joined:
    Oct 3, 2022
    Posts:
    11
    Thanks tone for the script, but im getting an error when the cloud build finished it says "
    The type or namespace name 'Callbacks' does not exist in the namespace 'UnityEditor' (are you missing an assembly reference?)" any idea on how to solve it ?

     
  10. Max-om

    Max-om

    Joined:
    Aug 9, 2017
    Posts:
    499
    I use the git commit counter for our game. For example. 1.2.698, major.minor.gitcommitcount
     
  11. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,089
    Hi, it might happen if you haven't put that script in an 'Editor' folder. If it's definitely in an Editor folder then I'm not certain, I'll give it a go when I'm working after the weekend.
     
  12. MxHush

    MxHush

    Joined:
    Oct 3, 2022
    Posts:
    11
    yup moving it to Editor folder solved the issue, Thanks alot Tone
     
    tonemcbride likes this.
  13. srujitha_unity

    srujitha_unity

    Joined:
    Mar 22, 2024
    Posts:
    1
    how do we submit the changes to player settings back to version control from unity cloud?
     
  14. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,089
    You don't really need to do that, if you use the auto incrementing build number then local builds will also run that script too.

    I guess you could setup a shell script to run when the build finishes but it might be difficult to commit the changes to source control because the build machine would need to have your credentials setup.