Create a build from the command line
Build Unity players from the command line without opening the Editor.
Read time 3 minutesLast updated 2 days ago
You can build a Unity Player from the command line without opening the Unity Editor. This is useful for automated builds in continuous integration (CI) systems and build pipelines.
There are several approaches to building a Player from the command line:
Build type | Command line details |
|---|---|
| Build with a build target | Use the |
| Build with a build profile | Use the |
| Build with a custom script and build target | Use the |
| Build with a custom script and build profile | Use the |
If you want to build a Player without a custom build script, use the argument with either or . This approach is similar to selecting Build in the Build Profiles window.
-build-buildTarget-activeBuildProfileIf you have a custom build script, use the argument to specify the static method to execute, along with either or .
-executeMethod-buildTarget-activeBuildProfileRequired arguments
The following arguments are required for command line builds:
- : Specifies the path to your Unity project.
-projectPath <pathname> - : Exits the Unity Editor after execution completes.
-quit
Recommended arguments
The following arguments are optional but recommended for command line builds:
- : Runs Unity in batch mode without the graphical user interface.
-batchmode - : Saves the Editor log to a specific file for easier debugging and monitoring.
-logFile <pathname> - or
-buildTarget: Ensures that Unity starts with the correct target platform configuration. If you don't specify this then Unity loads with the last used configuration.-activeBuildProfile
For a complete list of command line arguments, refer to Unity Editor command line arguments.
Target switching limitations in batch mode
Some APIs that can change or specify the target platform don't work as expected in batch mode, because when you change target platform, Unity recompiles and reloads the Editor assemblies. This reload can't happen while a script is running, so the change doesn't take effect.
The following APIs don't work as expected:
BuildProfile.SetActiveBuildProfileEditorUserBuildSettings.SwitchActiveBuildTargetAsyncBuildAssetBundlesParameters.targetPlatformBuildPlayerOptions.target
Additionally, platform-specific conditional compilation varies based on the current target. The assemblies that Unity loads can also differ depending on the target platform. These differences can influence the build output and lead to issues that are hard to diagnose.
Example: Build with a custom script on Windows
The following command executes a custom build script on Windows:
"C:\Program Files\Unity\Hub\Editor\6000.3.XXf1\Editor\Unity.exe" -executeMethod BuildScripts.BuildWindows64 -buildTarget StandaloneWindows64 -batchmode -quit -projectPath "C:\path\to\Project" -logFile C:\Logs\build.log
Example: Build with a custom script on macOS
The following command executes a custom build script on macOS:
/Applications/Unity/Hub/Editor/6000.3.XXf1/Unity.app/Contents/MacOS/Unity -executeMethod BuildScripts.BuildMacOS -buildTarget StandaloneOSX -batchmode -quit -projectPath "/path/to/Project" -logFile ~/Logs/build.log