Skip to content

Instantly share code, notes, and snippets.

@darktable
Created September 8, 2012 21:43
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save darktable/3679999 to your computer and use it in GitHub Desktop.
Save darktable/3679999 to your computer and use it in GitHub Desktop.
Unity3d: Post-process script that increments revision number of iPhone and Android builds.
/* **************************************************************************
Copyright 2012 Calvin Rien
(http://the.darktable.com)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
************************************************************************** */
// if you've got PlistCS (https://github.com/darktable/PlistCS/downloads)
// this script will automatically update the Info.plist version number
// #define PLIST_CS
using System.IO;
using System.Text.RegularExpressions;
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEngine;
using System.Collections.Generic;
#if PLIST_CS
using PlistCS;
#endif
public class IncrementBuildVersion : ScriptableObject {
[MenuItem("Tools/Increment Build Number")]
static void Init() {
IncrementBuild("");
}
[PostProcessBuild] // Requires Unity 3.5.2+
public static void OnPostProcessBuild(BuildTarget target, string buildPath) {
if (!(target == BuildTarget.iPhone)) {
return;
}
IncrementBuild(buildPath);
}
public static void IncrementBuild(string buildPath) {
var settingsPath = Path.GetDirectoryName(Application.dataPath);
settingsPath = Path.Combine(settingsPath, "ProjectSettings");
settingsPath = Path.Combine(settingsPath, "ProjectSettings.asset");
if (!File.Exists(settingsPath)) {
Debug.LogError("Couldn't find project settings file.");
return;
}
var lines = File.ReadAllLines(settingsPath);
if (!lines[0].StartsWith("%YAML")) {
Debug.LogError("Project settings file needs to be serialized as a text asset. (Check 'Project Settings->Editor')");
return;
}
string pattern = @"^(\s*iPhoneBundleVersion:\s*)([\d\.]+)$";
bool success = false;
System.Version version = null;
for (int i=0; i<lines.Length; i++) {
var line = lines[i];
if (!Regex.IsMatch(line, pattern)) {
continue;
}
var match = Regex.Match(line, pattern);
version = new System.Version(match.Groups[2].Value);
var major = version.Major < 0 ? 0 : version.Major;
var minor = version.Minor < 0 ? 0 : version.Minor;
var build = version.Build < 0 ? 0 : version.Build;
var revision = version.Revision < 0 ? 0 : version.Revision;
version = new System.Version(major, minor, build, revision + 1);
line = match.Groups[1].Value + version;
lines[i] = line;
success = true;
break;
}
if (!success) {
Debug.LogError("Couldn't find bundle version in ProjectSettings.asset");
return;
}
File.WriteAllLines(settingsPath, lines);
Debug.Log("Build version: " + version);
#if PLIST_CS
var plistPath = Path.Combine(buildPath, "Info.plist");
if (!File.Exists(plistPath)) {
Debug.LogWarning("Couldn't find Info.plist in build output.");
return;
}
// modify plist
var plist = (Dictionary<string,object>) Plist.readPlist(plistPath);
plist["CFBundleVersion"] = version.ToString();
Plist.writeXml(plist, plistPath);
#endif
}
}
@yourpalmark
Copy link

Created a fork and added usage of CFBundleShortVersionString as well as CFBundleVersion.
CFBundleShortVersionString (Version) is set to {Major}.{Minor}.{Build}.
CFBundleVersion (Build) is set to {Revision}.

https://gist.github.com/yourpalmark/6231952

I think this uses the Version and Build in Xcode more appropriately. Feel free to merge the changes if you like.

Thanks!

@PimDeWitte
Copy link

PimDeWitte commented Nov 8, 2016

Wrote a simplified version that works n the current version of unity (5.4) for both Android & iOS

https://github.com/PimDeWitte/Unity-auto-version-increment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment