Unity Multiplayer Example
In this tutorial we will create Multiplayer Game Server and Client using node JS (or docker) and Unity.
If anything in this tutorial was not clear, feel free to ask on the Discord Server.
Game Server (Node JS)
First let’s start with the Node JS server
Create an empty directory and open it in VS Code
Inside VS Code terminal run
npm init
This will create a package.json in your project
Next we need to import edge-multiplay, edge-multiplay is a node module that contains everything you need for a Game Server.
Use npm to import edge-multiplay, run this in your terminal.
npm install edge-multiplay
We need to add a script to run the server, so create a js file app.js
Inside app.js. add these lines, those 3 lines are basically your Game Server
const edgeMultiplay = require('edge-multiplay')edgeMultiplay.wsServer.on('newConnection', (path, connection)=>{
edgeMultiplay.addLobby(connection)
}
Then run your server
node app.js
You should see this log below in your terminal
Which basically means that the Server is using those three ports (2 TCP and 1 UDP).
If you go to http://localhost:7776 you can see the server statistics running on your local machine.
The Unity Client:
Find the latest EdgeMultiplay Unity package here:
Create Empty Unity project and import the Unity Package
Once the Unity Package is imported, Select the PingPong Example
Under Assets/PingPongGame/Scenes/PingPongScene, Select the Scene.
In the Hierarchy select the GameManager
In GameManager Inspector, Under EdgeManager, check use Use LocalHost Server and add your IP Address (ex. 127.0.0.1)
Now you can build the Scene and run One Build on your Machine and the other one in Unity Editor.
Voila, Now you got the Game Server and Client Running 🤩🥳🤩.
Feel free to check the examples that come with the Unity Package and EdgeMultiplay YouTube PlayList.
https://www.youtube.com/playlist?list=PLwUZZfaECSv18E5d0ooDR7S8416pImW8W
Notes
To change the server ports:
Go to node_modules/edge-multiplay/server.js and change your ports to the desired ports.
If you want to create a docker image to your server you will find a Dockerfile under node_modules/edge-multiplay/Dockerfile you can copy it and add any other ports you use and build the image.
Alternative to NodeJS (Using Docker):
If you don’t like NodeJS and just want to run the server
- Install Docker on your Machine
- Pull edge-multiplay Image using Terminal or PowerShell
docker pull mobiledgexsamples/edge-multiplay:1.2.1
- Run EdgeMultiplay as a docker container
docker run -d -p 3000:3000 -p 7776:7776 -p 5000:5000/udp mobiledgexsamples/edge-multiplay:1.2.1
If you have a question feel free to ask on the Discord Server