Incrementing a build number in Unity automatically


Before we begin, this doesn't increment the iOS build number. I'm sure that's possible, but this isn't that. 

This code example deals with a generic build number. Because nothing says "I'm working on my game" like an always-increasing, but insignificant number at the end of a file name people ignore anyway. But you are a dedicated developer and that beautiful build number is an infinite XP bar. So let's do it.

The code below reads a file called unity-builder.json in your project's Resources folder in Assets. It'll create one if the file is missing. Then increments a value called build_number by one before saving the file. It'll do this only if you're running the game in the Unity editor and when the game is played in said editor. It will do this even if you've changed nothing in your game. But hey, it was built and you were "testing" something. That counts. Right?

The file is called unity-builder.json because I first used that file in my automatic builder script that deploys here. You can change the filename is you're up to the task. You can also include whatever contents you want in the file. But remember C# will ignore anything not in the class you use to decode the contents. So if you want to keep that extra data, you'll need to modify the BuilderConfig class at the bottom of the file.

How do I use this? Drop the code below into a folder called Editor inside your project's Assets. This should work out of the box on OSX and linux. But I haven't tested it.

No, I meant how do I use this build number? Oh, well, that's a different devlog. This one, actually.

using System;
using System.IO;
using System.Text;
using UnityEngine;
using UnityEditor;
namespace Build {
    [InitializeOnLoadAttribute]
    public static class IncrementBuildNumber {
        static IncrementBuildNumber() {
            if (!Application.isEditor) {
                return;
            }
            EditorApplication.playModeStateChanged += LogPlayModeState;
        }
        static void LogPlayModeState(PlayModeStateChange state) {
            if (
                !Application.isEditor ||
                state != PlayModeStateChange.EnteredPlayMode
            ) {
                return;
            }
            //
            string builderConfigPath = Path.Combine(
                Directory.GetCurrentDirectory(),
                "Assets",
                "Resources",
                "unity-builder.json"
            );
            BuilderConfig builderConfig = ReadBuilderConfigFile(builderConfigPath);
            builderConfig.build_number += 1;
            WriteBuilderConfigFile(builderConfigPath, builderConfig);
        }
        static BuilderConfig ReadBuilderConfigFile(string path) {
            string json = null;
            try {
                Debug.Log($"Reading builder config at {path}");
                json = File.ReadAllText(path, Encoding.UTF8);
                return JsonUtility.FromJson<builderconfig>(json);
            } catch (FileNotFoundException) {
                // swallow file not found error
                return new BuilderConfig();
            } catch (System.Exception error) {
                Debug.LogError($"Error reading builder config file: {error.Message}");
                Debug.LogWarning($"Builder Config: {json}");
                return new BuilderConfig();
            }
        }
        static void WriteBuilderConfigFile(string path, BuilderConfig builderConfig) {
            try {
                Debug.Log($"Writing builder config at {path}");
                string json = JsonUtility.ToJson(builderConfig);
                Debug.Log($"Build Config: {json}");
                File.WriteAllText(path, json);
            } catch (System.Exception error) {
                Debug.LogError($"Error writing builder config file: {error.Message}");
            }
        }
    }
    // the C# equivalent of unity-builder.json
    [Serializable]
    public class BuilderConfig {
        public int build_number = 0;
    }
}

Get Against the Tide

Download NowName your own price

Leave a comment

Log in with itch.io to leave a comment.