When you develop a game with Unity3D and you want to take advantage of the Microsoft ecosystem like Visual Studio Online, it can be hard to configure your CI/CD environment. This blog post will explain how to set up that from pushing code to deploy the game on device automatically.

I can’t cover all platforms so I will only focus on Android. It’s pretty much the same stuff  for other platforms. The name of the game will be : “Adam Must Live” (yes, it’s an in progress game ;))

Unity CLI

So, you have a game that you want to build for the Android platform. Great! What’s next ? Command line arguments.

The first thing to do is to create a script that can be used to build the game without any user interaction and the Unity CLI is the perfect target for doing that. Create a folder named Editor in your Assets folder and add a new script named MyEditorScript.cs :

using System.IO;
using System.Linq;
using UnityEditor;

public class MyEditorScript
{
    public static void PerformBuild()
    {
        // Get all actives scenes
        var scenes = EditorBuildSettings.scenes.Where(s => s.enabled).Select(s => s.path).ToArray();

        var outputFile = "./Builds/Android/AdamMustLive.apk";
        if (File.Exists(outputFile))
        {
            File.Delete(outputFile);
        }

        var buildPlayerOptions =
            new BuildPlayerOptions
            {
                scenes = scenes,
                locationPathName = outputFile,
                target = BuildTarget.Android,
                options = BuildOptions.None
            };
        BuildPipeline.BuildPlayer(buildPlayerOptions);
    }
}

This static method can now be called be the CLI to build the game and generate the .apk file. We just need to run the command :

C:\Program Files\Unity\Hub\Editor\2018.1.1f1\Editor\Unity.exe -quit -batchmode -projectPath UNITY_PROJECT_PATH -executeMethod MyEditorScript.PerformBuild

We use the -batchmode to run Unity editor in a non-interactive mode (no user interaction will be required). The -quit option forces Unity to close himself when the task ends.

If you planned to install Unity on a machine hat don’t have a graphic card, you may need to add the parameter -nographics but some restrictions will be applied.

In the next post, we will see how to create a build definition with Visual Studio Online.