Custom build tools


While it's easy enough to use Unity's built-in build tools, you can make more customized build commands that are specifically tailored towards your game. This allows you to perform some tasks prior to exporting the final build, such as dynamically generating data files, or dynamically building a list of scenes to include.

I have a custom class extending EditorWindow, and on that window I have some simple buttons that perform build-related tasks for me. Here are some simple examples, and I'm sure you can think of more that apply to your own game to save you some time:

Generate a dynamic, custom data file used by the game:

In my example, I will have many "maps" which are Unity scenes loaded into a main scene by LoadLevelAdditive(). I have a build tool that opens every single map, gets information about it, and saves all this data (width, height, supported game modes, etc) to a master maps.json file. That way, my game menus and logic can pull this info from the file at runtime.

Define build scenes in a Constants file, and add them to the build when generating:

This is useful if you have tons of scenes that will be loaded additively (such as my maps from above), and you don't want to add them all to the build settings dialog manually. If you have a lot of churn during development, with maps being deleted and created continuously, this is a big time saver. I have my main build scenes defined in a Constants.cs file, and I combine this list with all my maps not in the list at build time. Here's a quick example that doesn't work on its own as a lot of surrounding code is missing, but gives the general idea:

//---[ Constants.cs ]--------------------------------------

// scenes to include in build (in Constants.cs)
#if UNITY_EDITOR
public static string[] BuildScenes = {
	"Assets/Scenes/TitleScene.unity",
	"Assets/Scenes/OptionsScene.unity",
	"Assets/Scenes/PlayerSetupScene.unity",
	"Assets/Scenes/BattleScene.unity"
};
#endif

//---[ build tools ]---------------------------------------

// combine all the scenes to include:
List allBuildScenes = new List();
allBuildScenes.AddRange(Constants.BuildScenes);
allBuildScenes.AddRange(otherScenesToInclude);

// finally, generate the build:
BuildPipeline.BuildPlayer(
    allBuildScenes.ToArray(),
    buildExportPath,
    buildPlatformTarget,
    BuildOptions.None
);

Pull in information and data from external sources:

I have a data file hosted on a web server that makes it easy for myself and others to continuously contribute to it whenever we have ideas. I can pull down this file in my build process and sync it with the game's version. That way, we can contribute to it at any time, and it will be included in the next build:

// import external data file
WWW dataFileWWW = new WWW(dataURL);
while (!dataFileWWW.isDone)
{
    EditorUtility.DisplayProgressBar("Importing data...", dataURL, dataFileWWW.progress);
}
File.WriteAllText(Application.dataPath + "/Resources/data.txt", dataFileWWW.text);
AssetDatabase.Refresh();
EditorUtility.ClearProgressBar();