Skip to content

Instantly share code, notes, and snippets.

@garcia556
Last active April 6, 2023 05:47
Show Gist options
  • Save garcia556/9380a88704136fc9decacb1839a5bb8b to your computer and use it in GitHub Desktop.
Save garcia556/9380a88704136fc9decacb1839a5bb8b to your computer and use it in GitHub Desktop.
Unity script to enable building for all platforms using command line
using UnityEditor;
/*
Methods to execute:
- RunMacOS
- RunWin
- RunWebGL
Command line arguments:
- output : build output path
- scene : scene to include into build
- development : same as Unity Editor flag: "Development Build"
- debug : same as Unity Editor flag: "Script Debugging"
Usage example on macOS:
/Applications/Unity/Unity.app/Contents/MacOS/Unity \
-batchmode \
-nographics \
-silent-crashes \
-logFile build.log \
-projectPath ./UnitySrc \
-executeMethod "BuildManager.RunMacOS" \
-output MyCoolGame.app \
-scene "Assets/Main.unity" \
-development \
-debug \
-quit
*/
class BuildManager
{
public const string KEY_OUTPUT = "-output";
public const string KEY_SCENE = "-scene";
public const string KEY_DEVELOPMENT = "-development";
public const string KEY_DEBUG = "-debug";
public static void RunMacOS() { Run(BuildTarget.StandaloneOSXUniversal); }
public static void RunWin() { Run(BuildTarget.StandaloneWindows64); }
public static void RunWebGL() { Run(BuildTarget.WebGL); }
private static void Run(BuildTarget buildTarget)
{
string sceneActive = UnityEngine.SceneManagement.SceneManager.GetActiveScene().name;
string[] scenes = new string[0];
string output = null;
BuildOptions buildOptions = BuildOptions.None;
string[] args = System.Environment.GetCommandLineArgs();
for (int i = 1; i < args.Length; i++)
{
if (args[i - 1].Equals(KEY_OUTPUT)) output = args[i];
if (args[i - 1].Equals(KEY_SCENE)) scenes = new string[] { args[i] };
if (args[i].Equals(KEY_DEVELOPMENT)) buildOptions |= BuildOptions.Development;
if (args[i].Equals(KEY_DEBUG)) buildOptions |= BuildOptions.AllowDebugging;
}
BuildPipeline.BuildPlayer(scenes, output, buildTarget, buildOptions);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment