Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Automated Unity Build from git repository to apk, xcode projects for use in conjunction with Jenkins. Contains Unity Editor script to collect the scenes, set the build settings, set the build location and an ant script to build the Unity project on the command line.
<?xml version="1.0" encoding="UTF-8"?>
<project name="${APP_NAME}" default="main" basedir=".">
<property file="settings.properties"/>
<property name="dev.dir.absolute" location="${dev.dir}"/>
<property name="build.dir.absolute" location="${build.dir}"/>
<property name="upload.dir.absolute" location="${upload.dir}"/>
<tstamp>
<format property="current.time" pattern="yyyy-MM-dd_HH-mm-ss" locale="en,SG"/>
</tstamp>
<property name="VERSION_NUM" value="${current.time}"/>
<property name="VERSION_STRING" value="${APP_NAME}_${VERSION_NUM}"/>
<target name="main" depends="ios, android" />
<target name="android" depends="copy-android"/>
<target name="ios" depends="compile-ios" />
<target name="clean">
<echo message="Cleaning Build Space ${build.dir.absolute}"/>
<delete dir="${build.dir.absolute}"/>
<mkdir dir="${build.dir.absolute}"/>
</target>
<target name="compile-ios" depends="clean">
<echo message="Compiling ${APP_NAME} Unity Project for iOS version ${VERSION_STRING}"/>
<echo message="${unity.bin} -batchmode -projectPath ${dev.dir.absolute} -executeMethod PerformBuild.CommandLineBuildiOS +buildlocation ${build.dir.absolute}/${xcode.name} -quit"/>
<exec executable="${unity.bin}" failonerror="true">
<arg value="-batchmode"/>
<arg value="-projectPath"/>
<arg value="${dev.dir.absolute}"/>
<arg value="-executeMethod"/>
<arg value="PerformBuild.CommandLineBuildiOS"/>
<arg value="+buildlocation"/>
<arg value="${build.dir.absolute}/${xcode.name}"/>
<arg value="-quit"/>
</exec>
</target>
<target name="compile-android" depends="clean">
<echo message="Compiling ${APP_NAME} Unity Project for android version ${VERSION_STRING}"/>
<echo message="${unity.bin} -batchmode -projectPath ${dev.dir.absolute} -executeMethod PerformBuild.CommandLineBuildAndroid +buildlocation ${build.dir.absolute}/${android.name} -quit"/>
<exec executable="${unity.bin}" failonerror="true">
<arg value="-batchmode"/>
<arg value="-projectPath"/>
<arg value="${dev.dir.absolute}"/>
<arg value="-executeMethod"/>
<arg value="PerformBuild.CommandLineBuildAndroid"/>
<arg value="+buildlocation"/>
<arg value="${build.dir.absolute}/${android.name}"/>
<arg value="-quit"/>
</exec>
</target>
<target name="copy-android" depends="compile-android">
<echo message="Copying from ${build.dir.absolute}/${android.name} to ${upload.dir.absolute}/${VERSION_STRING}.apk"/>
<copy file="${build.dir.absolute}/${android.name}" tofile="${upload.dir.absolute}/${VERSION_STRING}.apk"/>
</target>
</project>
#!/usr/bin/ruby
require 'xcodeproj'
xcproj = Xcodeproj::Project.open("build/xcode/Unity-iPhone.xcodeproj")
xcproj.recreate_user_schemes
xcproj.save
using UnityEditor;
using System.IO;
using System.Collections;
using UnityEngine;
using System.Collections.Generic;
class PerformBuild
{
private static string BUILD_LOCATION = "+buildlocation";
static string GetBuildLocation(BuildTarget buildTarget)
{
string[] args = System.Environment.GetCommandLineArgs();
int indexOfBuildLocation = System.Array.IndexOf(args, BUILD_LOCATION);
if (indexOfBuildLocation >= 0)
{
indexOfBuildLocation++;
Debug.Log(string.Format("Build Location for {0} set to {1}", buildTarget.ToString(), args[indexOfBuildLocation]));
return args[indexOfBuildLocation];
}
else
{
Debug.Log(string.Format("Build Location for {0} not set. Defaulting to {1}",buildTarget.ToString(),
EditorUserBuildSettings.GetBuildLocation(buildTarget)));
return EditorUserBuildSettings.GetBuildLocation(buildTarget);
}
}
static string[] GetBuildScenes()
{
List<string> names = new List<string>();
foreach(EditorBuildSettingsScene e in EditorBuildSettings.scenes)
{
if(e==null)
continue;
if(e.enabled)
names.Add(e.path);
}
return names.ToArray();
}
[UnityEditor.MenuItem("Perform Build/iOS Command Line Build")]
static void CommandLineBuildiOS ()
{
Debug.Log("Command line build\n------------------\n------------------");
string[] scenes = GetBuildScenes();
string path = GetBuildLocation(BuildTarget.iPhone);
if(scenes == null || scenes.Length==0 || path == null)
return;
Debug.Log(string.Format("Path: \"{0}\"", path));
for(int i=0; i<scenes.Length; ++i)
{
Debug.Log(string.Format("Scene[{0}]: \"{1}\"", i, scenes[i]));
}
Debug.Log(string.Format("Creating Directory \"{0}\" if it does not exist", path));
(new FileInfo(path)).Directory.Create();
Debug.Log(string.Format("Switching Build Target to {0}", "iOS"));
EditorUserBuildSettings.SwitchActiveBuildTarget(BuildTarget.iPhone);
Debug.Log("Starting Build!");
BuildPipeline.BuildPlayer(scenes, path, BuildTarget.iPhone, BuildOptions.None);
}
[UnityEditor.MenuItem("Perform Build/Android Command Line Build")]
static void CommandLineBuildAndroid ()
{
Debug.Log("Command line build android version\n------------------\n------------------");
string[] scenes = GetBuildScenes();
string path = GetBuildLocation(BuildTarget.Android);
if(scenes == null || scenes.Length==0 || path == null)
return;
Debug.Log(string.Format("Path: \"{0}\"", path));
for(int i=0; i<scenes.Length; ++i)
{
Debug.Log(string.Format("Scene[{0}]: \"{1}\"", i, scenes[i]));
}
Debug.Log(string.Format("Creating Directory \"{0}\" if it does not exist", path));
(new FileInfo(path)).Directory.Create();
Debug.Log(string.Format("Switching Build Target to {0}", "Android"));
EditorUserBuildSettings.SwitchActiveBuildTarget(BuildTarget.Android);
Debug.Log("Starting Android Build!");
BuildPipeline.BuildPlayer(scenes, path, BuildTarget.Android, BuildOptions.None);
}
}
unity.bin = /Applications/Unity/Unity.app/Contents/MacOS/Unity
xcode.bin = /usr/bin/xcodebuild
xcrun.bin = /usr/bin/xcrun
security.bin = /usr/bin/security
build.dir = build
dev.dir = unity
APP_NAME=Project Name
android.name=android.apk
xcode.name=xcode
upload.dir = upload
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment