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.
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.
I'm using no spaces in my name or file path because it often just causes issues.
Add a plane object to provide our scene with a focal point.
In the inspector on the right, adjust some of the values. Take a look at my position
0, 0, 0.
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.
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.
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
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:
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.
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:
My folder structure is as follows:
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:
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:
This will let you watch the logs file. Just do
CTRL + C to exit it.
You should now see a
builds folder in the your game folder. For me:
You will see the output of the build and a runnable WebGL game.
You can try to open the
index.html file. For me, I get this error:
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.
http://localhost:5000 in a browser.
That's it! Thanks for reading. Please follow my on Twitter for new tutorials.