Automating Unity Builds — Part 1 : Unity via Command Line

Photo by Alex Knight on Unsplash

In this series of post, I am sharing on how we construct our system to automate building Unity projects. You can refer of the overview of this process here.

In this part, I will explain about how to prepare our Unity project so that it can be executed through some automation service. Doing this part will enable the project to be build without need of manual interaction.

The automation is generally consists of two major parts :

1. Executing Unity via Command Line

One key part of automating Unity builds is figuring out whether Unity projects can be built without going through its GUI. This means that we want Unity to run “in the background” without human interaction at all. Is it possible to do such thing?

Thankfully, we can run Unity via command line, with additional arguments that can be parsed. This allows external process, such as a shell command or a CI server like Jenkins to execute Unity processes.

Executing Unity via command-line is easy, for example in MacOS, calling this in terminal will launch the Unity Editor:

/Applications/Unity/Unity.app/MacOS/Contents/MacOS/Unity

Now, we are going to put some arguments to configure our Unity execution.

Looking into Unity documentation, we could notice that there are some especially useful arguments for our case in point.

Source: Unity Docs

2. Secondly, -executeMethod. Passing this argument enables Unity to execute a staticmethod when Unity launches. We can also pass custom arguments to the Unity command line and parse it in our script. For example, let’s say we want to execute method SomeMethod() of SomeClass in CoolProject namespace. We also want to pass an argument of 100 points, therefore the command will be :

2. Building Unity Project via Script

Ok, so how does Unity builds its projects?

The usual way of building Unity project is going to Build Settings menu. We choose the platform, configure some parameters such as Development Build, etc. We also make sure that all configuration in Player Settings is appropriate. Then we click on Build / Build and Run, and wait… patiently.

Our lovely Build Settings window.

Internally, Unity exposes its Build API in the BuildPipeline.BuildPlayer under UnityEditor namespace. The method receives BuildPlayerOptions argument that contains configurations needed for that build, such as scenes, file location, target platform, etc.

We will use this as the base for our build process.

For example, let’s say we want to have a Build command that can build to either Android and iOS upon request. We will use BuildTarget as an argument to the function. We will also make it as static so we can execute it from command line.

The function will be :

Builds unity project to specific target and path.

Executing BuildProject(...) will build the active Scenes in your project to platform and outputPath of your choice.

3. Executing via Command-Line

Now, it is time we develop a way to execute this method via command line. Firstly, you will need to implement your own parser for getting custom arguments. We will needSystem.Environment.GetCommandLineArgs() to parse each of our commands to variables that we want to use. Learn more about the method on this link.

After we write the parser, we are ready to run it.

Put the BuildProject method inside Builder class of CoolProject namespace. This is the example command-line to build your project for Android platform :

Summary

By executing the Unity script via command-line, we are able to run Unity builds without requiring human interaction. It is crucial in building a fully automatic build process.

For Android, this step is generally enough, as Unity will export the result in .APK that can be installed directly to devices. For iOS platforms, however, the build will generate an XCode project, which will require more process.

In the following post, I will share the process about automating the building of iOS binaries from XCode project. Stay tuned!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Fadhil Noer Afif

Half-nerd, half-geek. Co-founder at Pikorua Studio. Loves playing roguelike games even though I suck at them.