Skip to content

Instantly share code, notes, and snippets.

@llamacademy
Last active September 12, 2024 17:06
Show Gist options
  • Save llamacademy/d6326dc6dcd215b66f7f6e4738bbbbf1 to your computer and use it in GitHub Desktop.
Save llamacademy/d6326dc6dcd215b66f7f6e4738bbbbf1 to your computer and use it in GitHub Desktop.
Build Incrementor Scripts from https://www.youtube.com/watch?v=PbFE0m9UMtE. If you get value from LlamAcademy, consider becoming a Patreon supporter at https://www.patreon.com/llamacademy
using TMPro;
using UnityEngine;
[RequireComponent(typeof(TextMeshProUGUI))]
public class BuildDisplayer : MonoBehaviour
{
private TextMeshProUGUI Text;
private void Awake()
{
Text = GetComponent<TextMeshProUGUI>();
ResourceRequest request = Resources.LoadAsync("Build", typeof(BuildScriptableObject));
request.completed += Request_completed;
}
private void Request_completed(AsyncOperation obj)
{
BuildScriptableObject buildScriptableObject = ((ResourceRequest)obj).asset as BuildScriptableObject;
if (buildScriptableObject == null)
{
Debug.LogError("Build scriptable object not found in resources directory! Check build log for errors!");
}
else
{
Text.SetText($"Build: {Application.version}.{buildScriptableObject.BuildNumber}");
}
}
}
using UnityEditor;
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
using UnityEngine;
public class BuildIncrementor : IPreprocessBuildWithReport
{
public int callbackOrder => 1;
public void OnPreprocessBuild(BuildReport report)
{
BuildScriptableObject buildScriptableObject = ScriptableObject.CreateInstance<BuildScriptableObject>();
switch(report.summary.platform)
{
case BuildTarget.StandaloneWindows64:
case BuildTarget.StandaloneLinux64:
case BuildTarget.StandaloneWindows:
case BuildTarget.StandaloneOSX:
PlayerSettings.macOS.buildNumber = IncrementBuildNumber(PlayerSettings.macOS.buildNumber);
buildScriptableObject.BuildNumber = PlayerSettings.macOS.buildNumber;
break;
case BuildTarget.iOS:
PlayerSettings.iOS.buildNumber = IncrementBuildNumber(PlayerSettings.iOS.buildNumber);
buildScriptableObject.BuildNumber = PlayerSettings.iOS.buildNumber;
break;
case BuildTarget.Android:
PlayerSettings.Android.bundleVersionCode++;
buildScriptableObject.BuildNumber = PlayerSettings.Android.bundleVersionCode.ToString();
break;
case BuildTarget.PS4:
PlayerSettings.PS4.appVersion = IncrementBuildNumber(PlayerSettings.PS4.appVersion);
buildScriptableObject.BuildNumber = PlayerSettings.PS4.appVersion;
break;
case BuildTarget.XboxOne:
PlayerSettings.XboxOne.Version = IncrementBuildNumber(PlayerSettings.XboxOne.Version);
buildScriptableObject.BuildNumber = PlayerSettings.XboxOne.Version;
break;
case BuildTarget.WSAPlayer:
PlayerSettings.WSA.packageVersion = new System.Version(PlayerSettings.WSA.packageVersion.Major, PlayerSettings.WSA.packageVersion.Minor, PlayerSettings.WSA.packageVersion.Build + 1);
buildScriptableObject.BuildNumber = PlayerSettings.WSA.packageVersion.Build.ToString();
break;
case BuildTarget.Switch:
PlayerSettings.Switch.displayVersion = IncrementBuildNumber(PlayerSettings.Switch.displayVersion);
PlayerSettings.Switch.releaseVersion = IncrementBuildNumber(PlayerSettings.Switch.releaseVersion);
// which one to use?
buildScriptableObject.BuildNumber = PlayerSettings.Switch.displayVersion;
break;
case BuildTarget.tvOS:
PlayerSettings.tvOS.buildNumber = IncrementBuildNumber(PlayerSettings.tvOS.buildNumber);
buildScriptableObject.BuildNumber = PlayerSettings.tvOS.buildNumber;
break;
}
AssetDatabase.DeleteAsset("Assets/Resources/Build.asset"); // delete any old build.asset
AssetDatabase.CreateAsset(buildScriptableObject, "Assets/Resources/Build.asset"); // create the new one with correct build number before build starts
AssetDatabase.SaveAssets();
}
private string IncrementBuildNumber(string buildNumber)
{
int.TryParse(buildNumber, out int outputBuildNumber);
return (outputBuildNumber + 1).ToString();
}
}
using UnityEngine;
public class BuildScriptableObject : ScriptableObject
{
public string BuildNumber = "1";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment