In the game development cycle, Quality Assurance and testing can be tedious and time consuming. Because of this, these tasks are all too often delayed for small studios. Things like critical bug fixes or new features typically take a higher priority and time is not consistently spent on automated testing solutions.

That said, every so often a bug gets leaked into a release or a mysterious compile error crops up in the main code repository, making the need for testing clear again. To mitigate these risks, teams will typically perform unit testing with tools like nUnit or Google Test. However, additional steps are required to execute Unity specific code and tests through an automated process.

Having a programmatic way of integrating testing tools with Unity can benefit teams greatly by offloading repetitive tasks like build releases or running unit tests on an automated system. Luckily Unity has an often overlooked feature: its command line toolset. This is available on all supported Unity editor platforms, including the Unity Linux beta. By using the command line toolset, teams can automate events like builds and unit testing.

For our team at KinematicSoup, we chose to use Unity’s command line as it provided us with the flexibility we needed. In our case, when we learned that Unity for Linux supported the command line toolset, we wanted to be able to migrate our deployment and build systems to Linux. To do this, we needed the Windows deployment and asset bundle builder tools we built for our current system to work on Linux in the future. The command line toolset in Unity allowed us to do this, and here’s how:

Using Command Line methods

The basic format of invoking Unity command line methods is to call the Unity editor application followed by the command line options: “/Unity.exe <options>”. In order to do this you will need to know the paths of the installation locations for Unity. The default Unity install locations for Windows, Mac and Linux are as follows:

Windows: C:\Program Files\Unity\Editor\Unity.exe

OSX: /Applications/Unity/Unity.app/Contents/MacOS/Unity

Linux: /opt/Unity/Editor/Unity

Before we discuss how to run custom classes and methods through the command line, let’s briefly review the built in Unity command line options. The built in functions allow you to do things like build an asset package or build a targeted release. The following list is an excerpt from the full documentation.

-batchmode - this should always be present when calling Unity from the command line, it lets Unity know not to open pop ups.

-nographics - tells Unity to not load any GUI or windows. Unfortunately Unity will still load the Direct X frameworks which will throw errors on systems without graphics capabilities. For example, a headless Linux build machine. We will discuss a work around for this later in the blog.

-projectPath <path> - opens the Unity project at the specified path. This will likely also be used every time.

-logFile <path> - Unity will redirect the log output to this file. This is really useful for builds and debugging.

-buildWindows64Player <build output path> - builds a win64 version of your project.

-importPackage <package path> - imports the package at the given location.

-exportPackage <Asset Path> <Export Path> - exports the assets at <Asset Path> into an asset bundle at the location and name of <Export Path>.

Invoking Custom Methods

The Unity command line functions are often not enough to complete required tasks, which is where the ability to invoke custom methods becomes very helpful. The syntax to call a custom Unity method is as follows:

/unity.exe <parameter1> <parameter2>...<parameterN> -projectPath <project path> -exit -batchmode -executeMethod <nameSpace.class.method>

Where parameter1 through parameterN are strings to pass into your Unity method.

This can be used to call a public static method where the script is saved in an “Editor” folder (resides in a folder in the assets directory with the name “Editor”). Below is an example of a generic C# class that could be called from the command line.