If you have a Windows PC and you want to automate your unity build process, then look no further!
I will show you how you can make bat files to do this easily.
Bat files also known as batch scripts are instructions you can run to tell your computer to do things. Almost anything on the command line that you can do, you can do in a bat file.
To make one is easy. You can open up notebook, write a few commands, and then save it as my_bat_file.bat. Then you can run the file by typing in the path to the file in the command line, or simply double clicking on it.
If you can run an application process from the command line, you can automate this task in a bat file.
Running Unity on the Command Line
Unity lets you run it on the command line giving it arguments. Therefore you can write a batch script automating the build.
Here's an example script:
@echo off
set projectpath="C:\Users\MyUserName\Projects\MyGame"
set buildpath="C:\Users\MyUserName\Builds"
set logfilepath="C:\Users\MyUserName\UnityBuildLogs\unityOutputLogFileForWebBuild.txt"
echo Building Unity
"C:\Program Files\Unity\Hub\Editor\6000.0.35f1\Editor\Unity.exe" -quit -batchmode ^
-projectPath %projectpath% ^
-executeMethod Builder.PerformBuild ^
-buildPath %buildpath% ^
-logfile %logfilepath%
if %errorlevel% equ 0 (
echo Build succeeded!
) else (
echo Build failed.
)
You can save this anywhere on your computer. Maybe a projects folder for batch scripts. Give it a useful name like unity_build_for_web.bat
If you read through it it's not too hard to understand what it does.
The important part is the middle where we call our unity program and give it arguments.
Calling a Method in Unity from the command line
In Unity you can call methods from the command line. This method I set up to build the web build. Here's my script:
using UnityEditor;
using UnityEngine;
using System.Linq;
public static class Builder
{
// build for the web!
public static void PerformBuild()
{
// Get all command line arguments
string[] args = System.Environment.GetCommandLineArgs();
// Log the arguments (for debugging purposes)
foreach (var arg in args)
{
Debug.Log("Arg: " + arg);
}
// Assume the eight argument after the Unity executable is the buildPath
string buildPath = args.Length > 1 ? args[8] : "."; // Default to current dir
BuildPlayerOptions buildPlayerOptions = new BuildPlayerOptions();
buildPlayerOptions.scenes = EditorBuildSettings.scenes
.Where(scene => scene.enabled)
.Select(scene => scene.path)
.ToArray();
buildPlayerOptions.target = BuildTarget.WebGL;
buildPlayerOptions.locationPathName = buildPath;
BuildPipeline.BuildPlayer(buildPlayerOptions);
}
}
You can save this as Assets/Editor/Builder.cs
This should be enough to get you started. You can change the target to whichever target you want.
Closing and Reopening Unity
Unity should be closed for this to work. But what if your unity is open and you want to run this script?
@echo off
echo Closing all instances of Unity Forcefully so make sure you have saved
:: This line will close all running Unity instances
taskkill /F /IM Unity.exe /T
:: This line will also close Unity Hub if it's running
taskkill /F /IM UnityHub.exe /T
:: End of script
echo All Unity instances have been closed.
Save it as something like "close_all_unity_instances.bat"
And now you want to re-open your unity program so you can continue working on your project, right?
Simply :
@echo off
echo Opening Unity for MyGame
set projectpath="C:\Users\Me\Projects\MyGame"
"C:\Program Files\Unity\Hub\Editor\6000.0.35f1\Editor\Unity.exe" -projectPath %projectPath%
exit
Save it as open_unity_project.bat
Simple enough. Notice how we don't feed in the -quit or -batchmode arguments so it opens in regular mode and stays open.
Tying it all together
Okay, so now we have three separate scripts we have to run. Why not just put them all together in one big bat file?
I would argue that you want to keep them separate. They all are self contained tasks.
Instead, you can make a pipeline for all these tasks with another batch script! We treat each of the previous files as jobs to run.
Here's what that looks like:
@echo off
call "C:\Users\Me\Projects\Batch\close_all_unity_instances.bat"
call "C:\Users\Me\Projects\Batch\unity_build_for_web.bat"
call "C:\Users\Me\Projects\Batch\open_unity_project.bat"
pause
Yo dawg! I heard you like batch scripts. So you can use the call command to call other batch scripts from your batch script.
You can save this with a useful name like build_my_game_for_web.bat
And now you can see the power of batch scripting!
What's Next?
- You can use curl to call webhooks to post an update message to slack or discord.
- You can upload your game to itch with a program called butler
- You can zip up your build, rename it with the date and time, and move it into another folder
- You can buy a mini programmable keyboard/controller and program it to run your build at the press of a button.
I hope this article helped. There are many ways to automate. This is just one way on a Windows machine with bat files. You could use other languages such as Perl or Python. But you don't have to install anything to run bat files on Windows.
If you want to build on Linux or Mac, look into shell scripting.
Enjoy!
Photo by Marvin Meyer on Unsplash
Top comments (0)