Posted on

Create Multiplayer Rooms with Photon 2 in Unity

How to make a Multiplayer Video Games in Unity using the Photon 2 plugin

Photon Documentation: https://doc.photonengine.com/en-us/pun/v2/getting-started/pun-intro
Photon API: http://doc-api.photonengine.com/en/PUN/current/index.html

For this Unity Multiplayer Tutorial on how to make a multiplayer game in Unity, we will teach you how to make a connection to the photon Multiplayer servers. We will also teach you how to create methods by which players can either join existing rooms or create new ones if none are available. This is the basis for all multiplayer games. Before you get to networking the gameplay you first have to establish a connection between the players.

To get started in this lesson we will begin by creating a new C# script called PhotonLobby. This C# script will be responsible for setting up a connection to the Photon Multiplayer servers and creating new rooms. We will then open this C# script.

Inside this C# script, we will begin by creating a singleton of this C# script and object so that we can reference this C sharp script from other C# scripts. Next, we want to create some more variables for us to use in this C# script. In the start function, we want to establish a connection to the Photon Servers. This will require us to include the Photon.PUN namespace above. When we successful connect we want to enable a button for the players to click which will let them either create a new room or join an existing random room. We will first try to join a room and if we fail to join a room then we will try to create a new room.

Once you have finished creating this C sharp script you will need to save and return to Unity. In Unity, we will create a new empty object and then attach this Csharp script. We will then create two new buttons, one for the battle button and one for the cancel button. We will then set the variables of our Lobby C# script.

If you have followed the video about you should be able to make a build of your project and have multiple instances of your game connect to each other.