Lessons

Building your Unity game using Bash

In this quick tutorial, I will give you the steps for building your Unity game via the command line (OSx). I will build my demo game to compile to be a WebGL game.

If you already have a game, feel free to skip the Game Setup section.

Game Setup

Create Game

Let's start by opening up Unity and building a basic game. It doesn't have to do anything, other than render a plane. I'm using Unity 2019.4.17f1. I'm going to create a new game.

Screenshot of Github Workflow Running

I'm using no spaces in my name or file path because it often just causes issues.

Screenshot of Github Workflow Running

Add Plane

Add a plane object to provide our scene with a focal point.

Screenshot of Github Workflow Running

In the inspector on the right, adjust some of the values. Take a look at my position 0, 0, 0.

Screenshot of Github Workflow Running

Adjust Camera

On the left side under the scene name, select Main Camera. In the inspector on the left, set the position to be 0, 0, -7. Set the rotation to be 20, 0, 0.

Screenshot of Github Workflow Running

Test Run

We should be all setup now. Let's make sure this runs inside of Unity. Hit the play button in the centre at the top.

Screenshot of Github Workflow Running

Setup Build

We are going to add a new C# file called Assets/Editor/WebGLBuilder.cs. Your bottom folder structure starts in Assets. You may need to create Editor. You just need to right click to add a new Folder and C# Script.

Screenshot of Github Workflow Running

Screenshot of Github Workflow Running

Screenshot of Github Workflow Running

Screenshot of Github Workflow Running

Now that the file is created, open it in an editor. I've configured my Unity app to default to VSCode.

Put this code in the script:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 using UnityEngine; using UnityEditor; class WebGLBuilder { static void build() { // Place all your scenes here string[] scenes = {"Assets/Scenes/SampleScene.unity"}; string pathToDeploy = "builds/WebGLversion/"; BuildPipeline.BuildPlayer(scenes, pathToDeploy, BuildTarget.WebGL, BuildOptions.None); } }

Don't know what your scenes are? You can go back in your Assets folder and click on the scene. It will show you the path.

Screenshot of Github Workflow Running

It is an array of scenes, you can add multiple. Everything you want to be included in the build. For me, I have just the one. Save that file. Save your scene. Let's run it. This is the slightly annoying part, you can't run two instances of Unity at the same time. To compile it, you will be running it without a UI and in the background. This will stop you from keeping your editor open.

You will have to find the path to your Unity app on your local machine. For me, mine is:

1 /Applications/Unity/Hub/Editor/2019.4.17f1/Unity.app/Contents/MacOS/Unity

My folder structure is as follows:

1 2 3 4 unity-bash-demo/ DemoGame/ Assets/ ... Other folders...

I'm running this command from inside unity-bash-demo. You will have to update $pwd/DemoGame/ to be your game folder. You will also have to update the additional arguments if you are not using WebGL. With Unity closed, I run:

1 /Applications/Unity/Hub/Editor/2019.4.17f1/Unity.app/Contents/MacOS/Unity -quit -batchmode -logFile stdout.log -projectPath "$pwd/DemoGame/" -executeMethod WebGLBuilder.build

I'm currently mid-development on another game and this takes me about 5 minutes to run. Building the game is not the fast command, so please be patient. If you want to see what is going on in the build process, in another terminal window you can run:

1 tail -f stdout.log

This will let you watch the logs file. Just do CTRL + C to exit it.

Screenshot of Github Workflow Running

Screenshot of Github Workflow Running

You should now see a builds folder in the your game folder. For me:

1 unity-bash-demo/DemoGame/builds/WebGLversion/

Screenshot of Github Workflow Running

You will see the output of the build and a runnable WebGL game.

Screenshot of Github Workflow Running

You can try to open the index.html file. For me, I get this error:

Screenshot of Github Workflow Running

To solve this, I just need to run a Node server. You can also do this with Python, I have link to my references at the bottom.

1 npm install -g serve

In unity-bash-demo/DemoGame/builds/WebGLversion/, run:

1 serve

Screenshot of Github Workflow Running

Now open http://localhost:5000 in a browser.

Screenshot of Github Workflow Running

That's it! Thanks for reading. Please follow my on Twitter for new tutorials.

References

  • https://simmer.io/articles/starting-a-localhost-server-for-a-unity-webgl-build/