-
-
Save ophilbert/1d8fa55c53ccdccd309bd2503727c0a8 to your computer and use it in GitHub Desktop.
Change iOS build number so it matches Unity Cloud Build build version
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEditor.Callbacks; | |
using System; | |
using System.Collections; | |
using UnityEditor; | |
using UnityEngine.Networking; | |
#if UNITY_IOS | |
using UnityEditor.iOS.Xcode; | |
#endif | |
using System.IO; | |
public class BuildVersionPostProcess | |
{ | |
private static string projectName = "yourProjectName"; | |
private static string companyName = "yourCompanyName"; | |
private static string androidBuildTargetName = "yourAndroidBuildTarget"; | |
private static string iOSBuildTargetName = "youriOSBuildTarget"; | |
private static string AuthorizationToken = "Basic [APIKEY]";//[APIKEY] can be found at https://developer.cloud.unity3d.com/preferences/ | |
[PostProcessBuild(101)] | |
public static void OnPostprocessBuild(BuildTarget buildTarget, string buildPath) | |
{ | |
if (buildTarget != BuildTarget.Android && buildTarget != BuildTarget.iOS) | |
{ | |
Debug.LogWarning("Can't change buildVersion, only iOS and Android are supported. Not " + buildTarget.ToString()); | |
return; | |
} | |
int buildNumber = FindLatestBuildNumber(buildTarget == BuildTarget.Android ? androidBuildTargetName : iOSBuildTargetName); | |
AssignBuildNumber(buildNumber, buildPath); | |
} | |
private static int FindLatestBuildNumber(string buildTarget) | |
{ | |
string url = string.Format("https://build-api.cloud.unity3d.com/api/v1/orgs/{0}/projects/{1}/buildtargets/{2}/builds", companyName, projectName, buildTarget); | |
UnityWebRequest www = UnityWebRequest.Get(url); | |
www.SetRequestHeader("Content-Type", "application/json"); | |
www.SetRequestHeader("Authorization", AuthorizationToken); | |
AsyncOperation op = www.Send(); | |
while (!op.isDone); | |
if(www.isError) | |
{ | |
Debug.Log(www.error); | |
return 1; | |
} | |
string dlText = www.downloadHandler.text; | |
int end = dlText.IndexOf(','); | |
string begin = dlText.Substring(0, end); | |
int correctVersion = int.Parse(begin.Replace("[{\"build\":", "")); | |
return correctVersion; | |
} | |
private static void AssignBuildNumber(int correctBuildNumber, string buildPath) | |
{ | |
Debug.Log("Assigning build number: " + correctBuildNumber); | |
#if UNITY_IOS | |
ChangeXCodeBuildNumber(correctBuildNumber, buildPath); | |
#elif UNITY_ANDROID | |
ChangeAndroidBuildNumber(correctBuildNumber, buildPath); | |
#endif | |
} | |
#if UNITY_IOS | |
private static void ChangeXCodeBuildNumber(int correctBuildNumber, string buildPath) | |
{ | |
string projPath = Path.Combine(buildPath, "Info.plist"); | |
PlistDocument plist = new PlistDocument(); | |
plist.ReadFromString(File.ReadAllText(projPath)); | |
PlistElementDict rootDict = plist.root; | |
string buildKey = "CFBundleVersion"; | |
rootDict.SetString(buildKey, correctBuildNumber.ToString()); | |
File.WriteAllText(projPath, plist.WriteToString()); | |
} | |
#endif | |
private static void ChangeAndroidBuildNumber(int correctBuildNumber, string buildPath) | |
{ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment