Creating Multiplayer Game (Server and Client)
Creating Multiplayer Game (Server and Client)

Unity Multiplayer Example

Ahmed Schrute

--

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

The ports running on the server, TCP and UDP ports
The ports running on the server

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.

Once you have your server running you should find the Server Statistics Running at http://localhost:7776
Server Statistics Running at http://localhost:7776

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

Importing EdgeMultiplay PingPong Example using EdgeMultiplay Menu in Unity Editor
Importing EdgeMultiplay PingPong Example

Under Assets/PingPongGame/Scenes/PingPongScene, Select the Scene.

In the Hierarchy select the GameManager

Find Game Manager in the PingPong Hierarchy so you can set Unity to connect to the locally hosted server
Find Game Manager in the PingPong Hierarchy

In GameManager Inspector, Under EdgeManager, check use Use LocalHost Server and add your IP Address (ex. 127.0.0.1)

Set UseLocalHost to true in EdgeManager Component and Add your IP Address, EdgeManager Component is part of GameManager in the PingPong Scene
Set UseLocalHost to true in EdgeManager Component

Now you can build the Scene and run One Build on your Machine and the other one in Unity Editor.

Screenshot of the Multiplayer Game Running using the locally hosted EdgeMultiplay Server
Screenshot of the Multiplayer Game Running

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

EdgeMultiplay Unity Examples, Examples of Multiplayer Games
EdgeMultiplay Unity Examples

Notes

To change the server ports:

Go to node_modules/edge-multiplay/server.js and change your ports to the desired ports.

If needed Change those ports to the desired ports under node_modules/edge-multiplay/server.js
If needed Change those 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

https://discord.gg/CHCWfgrxh6

--

--