Skip to content

Instantly share code, notes, and snippets.

@guneyozsan
Created February 16, 2023 12:34
Show Gist options
  • Save guneyozsan/d5c38030349cba8361f7b7e1832bf066 to your computer and use it in GitHub Desktop.
Save guneyozsan/d5c38030349cba8361f7b7e1832bf066 to your computer and use it in GitHub Desktop.
Sets mobile build number to Unix time without last digit before building with Unity
using UnityEditor;
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
using UnityEngine;
public class BuildProcessorBuildNumberSetter : IPreprocessBuildWithReport
{
public int callbackOrder => 0;
public void OnPreprocessBuild(BuildReport report)
{
string caller = GetType().FullName + "." + nameof(OnPreprocessBuild);
SetBuildNumber();
void SetBuildNumber()
{
// Set build number (iOS) and bundle version code (Android).
//
// NOTE: We drop last digit in order to prevent Google Play Store bundle version code upper limit of 100000
// from preventing pushing new versions of the app.
int unixTimeWithoutLastDigit = GetUnixTime() / 10;
#if UNITY_IOS
PlayerSettings.iOS.buildNumber = unixTimeWithoutLastDigit.ToString();
Debug.Log(caller + ": Modified iOS build number to: " + unixTimeWithoutLastDigit + " (Unix time without last digit)");
#elif UNITY_ANDROID
PlayerSettings.Android.bundleVersionCode = unixTimeWithoutLastDigit;
Debug.Log(caller + ": Modified Android bundle version code to: " + unixTimeWithoutLastDigit + " (Unix time without last digit)");
#endif
int GetUnixTime()
{
// From: http://answers.unity.com/answers/508931/view.html
var epochStart = new System.DateTime(1970, 1, 1, 0, 0, 0, System.DateTimeKind.Utc);
int unixTime = (int)(System.DateTime.UtcNow - epochStart).TotalSeconds;
return unixTime;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment