Using Unity3D from command line to automate tasks

Using Unity3D from command line to automate tasks

Wender L.
Posted on 1/17/2019Last Modified: 1/20/2019

Did you know Unity had command line features? And it is pretty easy to use them. Weather you are on Linux/Mac using Bash or Windows/PowerShell you can use interesting features. Automating the boring tasks is way more fun than executing them. My goal was simple: Build all the multiple platforms and pack them with the right folders to upload to Steam and other stores plus one headless build for test purposes. I could also choose the scenes I wanted to build, as a result reducing the size and build time for tests. Unity offers their build services which is nice most of time but it can be long, unpredictable and you have limited access like to change player settings.

You need to create your script MyClass.cs which has the static method "MyMethod". This method you write will produce the build using Unity class BuildPipeline. It should look something like this:


using UnityEditor;
using UnityEngine;

public class MyClass: MonoBehaviour {
public static void MyMethod(){
	BuildPipeline.BuildPlayer(
	    new string[] {"MySceneMenu","MySceneGame"}, 
	    "path/to/build",
	    BuildTarget.StandaloneLinuxUniversal, 
	    BuildOptions.EnableHeadlessMode | BuildOptions.Development
	);
}}

Next I created a simple file called Build.sh (Bash Linux) or Build.bat (PowerShell Windows).

To call unity through command line you need the path to the unity executable and pass arguments in the pattern "-parameterKey parameterValue".

The arguments to be used:
* projectPath: The project path you want to build
* batchmode: to signal no human interaction.
* nographics: so unity wont show windows or use any gpu graphics device.
* executeMethod: The method to execute and finally quit argument. Inside the file you have something like this:


Linux



#!/bin/sh
#Build.sh
/path/to/Unity -projectPath "/path/to/my/project" -batchmode -nographics -executeMethod MyClass.MyMethod -quit
#After this I added various commands to clean and zip the project folder.
cd /path/to/build/
rm Build.zip #remove last one
zip -r Build.zip BuildFolder/


Windows



ECHO Building...
"Path\To\Unity.exe" -projectPath "Path\To\Project" -batchmode -nographics -executeMethod MyClass.MyMethod -quit
ECHO "Build Completed"
ECHO "Compressing Build"
del "Path\To\Build.zip"
powershell.exe -nologo -noprofile -command "& { Add-Type -A 'System.IO.Compression.FileSystem';[IO.Compression.ZipFile]::CreateFromDirectory('Path\To\Build', 'Path\To\Build.zip'); }"
ECHO "Compressing Completed"

Execute the bash file and Bam! Unity is building silently and doing the work while you drink more coffee and think about the cool parts of your game! In your script in c# or command line could have some automation like uploading to your testers, send a message/notification warning testers, add extra files like manuals,artwork, etc.

Using command line brings a lot of benefits. If you want to convert the extension of some file. In my case I wanted to convert json files to yaml, which is more compact and friendly format. I used the following code:


#./change-extension.sh "path/to/folder"
cd $1
for file in *.json; do
		filename="${file%.*}"
		echo "converting $filename"
		json2yaml "$filename.json" > "$filename.yaml"
done

Another thing to illustrate how elegant the code can be is to automate variety of tasks, the following example is a code to convert mp4 file to gif animation:


#!/bin/sh
#create gif
sudo rm -rf $1
mkdir $1
ffmpeg -i $1.mp4 -s 960x540 -r 5 $1'/frame-%03d.jpg'
#480x270
cd $1
convert -delay 10 -loop 0 *.jpg $1.gif
mv $1.gif ..
cd ..
sudo rm -rf $1


#!/bin/sh

for file in *.mp4; do 
  filename="${file%.*}"
	./create_gif.sh $filename
done

There are many open-source tools and scripts you could use for your needs. Official reference at Unity API Documentation.

Wender L.
Posted on 1/17/2019Last Modified: 1/20/2019

...

Recomendations

Games for Sale

Comments 0

Display Name

Email


0 / 10