Skip to content

Instantly share code, notes, and snippets.

@Youngchangoon
Created June 21, 2020 13:26
Show Gist options
  • Save Youngchangoon/5d1e5a048e3001fae9bef9acd5f068b6 to your computer and use it in GitHub Desktop.
Save Youngchangoon/5d1e5a048e3001fae9bef9acd5f068b6 to your computer and use it in GitHub Desktop.
Unity + iOS, Android Build Processor for Jenkins
using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
public static class JenkinsBuildProcessor
{
[MenuItem("Tools/CI/Build iOS Debug")]
public static void BuildIOS()
{
// 1. Build Number Automatically Up
AddBuildVersion(BuildTarget.iOS);
GenericBuild(FindEnabledEditorScenes(), $"./Build/IOS/", BuildTarget.iOS, BuildOptions.None);
}
[MenuItem("Tools/CI/Build And Debug")]
public static void BuildAnd()
{
// 1. Build Number Automatically Up
var newBuildVersionCode = AddBuildVersion(BuildTarget.Android);
PlayerSettings.Android.useCustomKeystore = true;
// PlayerSettings.Android.keystoreName = "AndroidKey.keystore";
PlayerSettings.Android.keystorePass = "**KeyStore Password**";
PlayerSettings.Android.keyaliasName = "**Alias name**";
PlayerSettings.Android.keyaliasPass = "**Alias password**";
EditorUserBuildSettings.exportAsGoogleAndroidProject = true;
GenericBuild(FindEnabledEditorScenes(), $"./Build/Android/", BuildTarget.Android, BuildOptions.AcceptExternalModificationsToPlayer);
}
private static string AddBuildVersion(BuildTarget buildTarget)
{
CheckBuildTarget(buildTarget);
string oldBuildNumber;
switch (buildTarget)
{
case BuildTarget.Android:
oldBuildNumber = PlayerSettings.Android.bundleVersionCode.ToString();
PlayerSettings.Android.bundleVersionCode = (int.Parse(oldBuildNumber) + 1);
return PlayerSettings.Android.bundleVersionCode.ToString();
case BuildTarget.iOS:
oldBuildNumber = PlayerSettings.iOS.buildNumber;
PlayerSettings.iOS.buildNumber = (int.Parse(oldBuildNumber) + 1).ToString();
return PlayerSettings.iOS.buildNumber;
break;
}
return "null";
}
private static void CheckBuildTarget(BuildTarget buildTarget)
{
if (buildTarget == BuildTarget.Android || buildTarget == BuildTarget.iOS)
return;
throw new Exception($"Is not supported Platform, {buildTarget}");
}
private static void GenericBuild(string[] scenes, string targetPath, BuildTarget buildTarget, BuildOptions buildOptions)
{
var buildTargetGroup = BuildPipeline.GetBuildTargetGroup(buildTarget);
EditorUserBuildSettings.SwitchActiveBuildTarget(buildTargetGroup, buildTarget);
BuildPipeline.BuildPlayer(scenes, targetPath, buildTarget, buildOptions);
}
private static string[] FindEnabledEditorScenes()
{
List<string> EditorScenes = new List<string>();
foreach(var scene in EditorBuildSettings.scenes)
{
if (!scene.enabled)
continue;
EditorScenes.Add(scene.path);
}
return EditorScenes.ToArray();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment