Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save SiarheiPilat/50835f6537935c24a67865c8244e978b to your computer and use it in GitHub Desktop.
Save SiarheiPilat/50835f6537935c24a67865c8244e978b to your computer and use it in GitHub Desktop.
Automatically increment the iOS build number in Unity conforming to Apple's guidelines before every build. Be sure to drop into a folder name "Editor". Worked in autumn 2020.
using UnityEngine;
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
using UnityEditor;
public class IncrementBuildNumber : IPreprocessBuildWithReport {
public int callbackOrder { get { return 0; } } // Part of the IPreprocessBuildWithReport interface
public void OnPreprocessBuild(BuildReport report) {
if (report.summary.platform == BuildTarget.iOS) {
// to update major or minor version, manually set it in Edit>Project Settings>Player>Other Settings>Version
string[] versionParts = PlayerSettings.iOS.buildNumber.Split('.');
int buildNumber = 0;
if (versionParts.Length != 3 || !int.TryParse(versionParts[2], out buildNumber) ) {
Debug.LogError("BuildPostprocessor failed to update version " + PlayerSettings.bundleVersion + " with build number: " + PlayerSettings.iOS.buildNumber);
return;
}
// major-minor-build
versionParts[2] = (buildNumber + 1).ToString();
PlayerSettings.iOS.buildNumber = PlayerSettings.bundleVersion + "." + versionParts[2];
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment