Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[Unity] C# editor script containing static methods to build a project for Android and iOS
// Usage: set the projectName you want, then run:
// [Unity executable path] [custom arguments] -executeMethod CommandLineBuild.Build[AndRun][Platform][Development] [-version VERSION]
using UnityEngine;
using UnityEditor;
using System;
using System.Collections;
using System.Linq;
public class CommandLineBuild
{
const string projectName = "My Project";
static void BuildAndroid ()
{
string suffix = GetProjectNameSuffix();
BuildPipeline.BuildPlayer(GetScenes(), string.Format("Build/Android/{0}{1}.apk", projectName, suffix), BuildTarget.Android, BuildOptions.None);
}
static void BuildAndRunDevelopment ()
{
string suffix = GetProjectNameSuffix();
BuildPipeline.BuildPlayer(GetScenes(), string.Format("Build/Android/{0}{1}.apk", projectName, suffix), BuildTarget.Android, BuildOptions.Development | BuildOptions.AllowDebugging);
}
static void BuildAndRunAndroid ()
{
string suffix = GetProjectNameSuffix();
BuildPipeline.BuildPlayer(GetScenes(), string.Format("Build/Android/{0}{1}.apk", projectName, suffix), BuildTarget.Android, BuildOptions.AutoRunPlayer);
}
static void BuildAndRunAndroidDevelopment ()
{
string suffix = GetProjectNameSuffix();
BuildPipeline.BuildPlayer(GetScenes(), string.Format("Build/Android/{0}{1}.apk", projectName, suffix), BuildTarget.Android, BuildOptions.AutoRunPlayer | BuildOptions.Development | BuildOptions.AllowDebugging);
}
static void BuildIOS ()
{
string suffix = GetProjectNameSuffix();
BuildPipeline.BuildPlayer(GetScenes(), string.Format("Build/iOS/{0}{1}", projectName, suffix), BuildTarget.iOS, BuildOptions.SymlinkLibraries);
}
static void BuildIOSDevelopment ()
{
string suffix = GetProjectNameSuffix();
BuildPipeline.BuildPlayer(GetScenes(), string.Format("Build/iOS/{0}{1}", projectName, suffix), BuildTarget.iOS, BuildOptions.Development | BuildOptions.AllowDebugging | BuildOptions.SymlinkLibraries);
}
static string[] GetScenes () {
return EditorBuildSettings.scenes.Where(s => s.enabled).Select(s => s.path).ToArray();
}
/// Helper function for getting the command line arguments
/// from https://effectiveunity.com/articles/making-most-of-unitys-command-line.html
private static string GetArg (string name)
{
var args = System.Environment.GetCommandLineArgs();
for (int i = 0; i < args.Length; i++)
{
if (args[i] == name && args.Length > i + 1)
{
return args[i + 1];
}
}
return null;
}
/// Return suffix to add to project name, adding a space before if not empty
private static string GetProjectNameSuffix () {
string versionArg = GetArg("-version");
return versionArg != null ? " " + versionArg : "";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.