Automatically Set iOS Build Number And Android Version Code In Unity Cloud Build

Did you ever have an experience where you automate the build of your games with Unity Cloud Build, but eventually you realise that you forgot to update the build number for iOS and version code for Android? Then, you have to edit the PlayerSettings and wait for the build again….

If you have a same nightmare, don’t worry, i have a solution for you.

I think many of you already know that if you set Android version code to 0 in PlayerSettings, Unity will help to populate the version code with “build number” from the Unity Cloud Build. However, that is not the case for iOS. Luckily, you can use PreExport(UnityEngine.CloudBuild.BuildManifestObject manifest) method to make some modification before Unity export the build.

First, create a file in Asset > Editor > AutoIncrementVersionCodeInCloudBuild.cs and put the following code in the script

public class AutoIncrementVersionCodeInCloudBuild : MonoBehaviour
{
#if UNITY_CLOUD_BUILD
    public static void PreExport(UnityEngine.CloudBuild.BuildManifestObject manifest)
    {
        string buildNumber = manifest.GetValue("buildNumber", "0");
        Debug.LogWarning("Setting build number to " + buildNumber);
        PlayerSettings.Android.bundleVersionCode = int.Parse(buildNumber);
        PlayerSettings.iOS.buildNumber = buildNumber;
    }
#endif
}

You will notice that I wrap the whole function with #if UNITY_CLOUD_BUILD because the BuildManifestObject can only access in Unity Cloud Build. This is very important if you wish to build the game in your local machine too. You may refer to the Github repo​ for complete scripts.
Next, you need to tell Unity Cloud Build to run the PreExport method. Go to your Unity Cloud Build console > Cloud Build > Config > Advanced Settings and put the value “AutoIncrementVersionCodeInCloudBuild.PreExport” in PreExport Method Name.

Automatically Set iOS Build Number And Android Version Code In Unity Cloud Build

Now if you build again, you should see your APK or iPA will fill the version code or build number with Unity Cloud Build’s build number. Now, you no need to worry about updating the those values every time!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: