Building multiple platforms with one click

EDIT: this is now up on GitHub.

I finally got tired of manually changing platforms after creating builds, so I went and created a customised BuildPipeline. This creates a 64-bit Linux build, then switches to 64-bit Windows, and ends with 64-bit Mac OSX, all with one click of a button. I wish I had known how to do this earlier!

The basis of this is manually calling BuildPipeline.BuildPlayer. This requires a few parameters:

  • An array of strings with the names of the levels you want to include
  • The name and location of the executable (i.e. what the players are going to click on)
  • The target platform (in my case, 64-bit standalones for Linux, Windows and Mac).
  • The BuildOptions. This is the tricky one: according to the docs, it is possible to combine multiple values of this, but I haven’t figured out how to do this yet. It might be because I’m on 5.3.6, instead of 5.5.

And that’s it. Here’s what the code looks like:

using UnityEditor;
using UnityEngine;
using System;

public class MyBuildPipeline {

// constants for defaults
const string WINDOWS_FILENAME = "_Windows";
const string LINUX_FILENAME = "_Linux";
const string MAC_FILENAME = "_Mac";
const string SCENE_FOLDER = "Assets/_Scenes/";
const string BUILD_PATH = "Player Files/";

// TODO: find how to combine these
static BuildOptions devOptions = BuildOptions.AllowDebugging;

[MenuItem("Tools/Aceade/Build")]
public static void Build()
{
Debug.Log("Creating new builds");
var levels = new string[6]{
SCENE_FOLDER + "Preloading.Unity", SCENE_FOLDER + "Main Menu.unity", SCENE_FOLDER + "Sound Test.unity",
SCENE_FOLDER + "City Inbound.unity", SCENE_FOLDER + "Inner City.unity", SCENE_FOLDER + "Museum Model.unity"
};

// calculate the build date
DateTime date = DateTime.Now;
string buildDate = string.Format("_{0}.{1}.{2}",date.Day, date.Month, date.Year);
Debug.LogFormat("Creating new builds on {0}", buildDate);

MakeLinuxBuild(levels, buildDate, BUILD_PATH);
MakeWindowsBuild(levels, buildDate, BUILD_PATH);
MakeMacBuild(levels, buildDate, BUILD_PATH);
}

///

/// Makes a Linux build.
///

/// Levels to build.
/// Date string.
/// Path.
static void MakeLinuxBuild(string[] levelsToBuild, string dateString, string path)
{
Debug.Log("Creating Linux build");
string fileName = LINUX_FILENAME + dateString + ".x86_64";
BuildTarget target = BuildTarget.StandaloneLinux64;
BuildPipeline.BuildPlayer(levelsToBuild, path + fileName, target, devOptions);
}

///

/// Makes the Windows build.
///

/// Levels to build.
/// Date string.
/// Path.
static void MakeWindowsBuild(string[] levelsToBuild, string dateString, string path)
{
Debug.Log("Creating Windows build");
string fileName = WINDOWS_FILENAME + dateString + ".exe";
BuildTarget target = BuildTarget.StandaloneWindows64;
BuildPipeline.BuildPlayer(levelsToBuild, path + fileName, target, devOptions);
}

///

/// Makes a build for Mac devices.
///

/// Levels to build.
/// Date string.
/// Path.
static void MakeMacBuild(string[] levelsToBuild, string dateString, string path)
{
Debug.Log("Creating Mac OSX build");
string fileName = MAC_FILENAME + dateString + ".app";
BuildTarget target = BuildTarget.StandaloneOSXIntel64;
BuildPipeline.BuildPlayer(levelsToBuild, path + fileName, target, devOptions);
}
}

I’m considering adding this to GitHub, once I’ve figured out the BuildOptions.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.