Posted on

Starting Multiplayer Games with Photon 2 in Unity

For this Unity Multiplayer Game Tutorial on how to make a multiplayer game in Unity using the Photon 2 plugin, we will be showing you how to create a Photon room controller script which will control some of the interactions between players connected to the same room. It will also control the start of the game and how the players are loaded into the multiplayer scene. There are two different ways to start a multiplayer game. One is a continuous load and the other is a delayed start. a continuous load means players can join and leave a game in progress at any time. whereas a delayed start is when all the players have to be connected to the room before the game can start. Our code in this video will handle both these methods and you will be able to switch between the two by changing a single bool.

We will start this lesson by creating two new C# scripts the first will be called Multiplayer Setting and the next one will be called Photon Room. We will then open these C# scripts up.

We will start in the Multiplayer Settings. We will first need to create some new variables. We can then delete the start and update functions. we will then create an awake function and set up a singleton for this C# script. We will then save this script after which we will make a few changes to our room creation in the lobby script.

We then need to open our Photon Room script. Inside this script, we will create some more variables. We will then create some code that will either load the player right into the multiplayer scene as soon as they connect to the room or the game will have a delayed start where all the players will load into the multiplayer scene at the same time. Once we have finished creating this script we will then save and return to Unity.

In Unity, we need to create an empty game object and attach our Photon room script. We will also create an empty game object and attach our multiplayer settings script. We then need to set the variable of these scripts. We will then create a placeholder for our Photon player object.

If you have followed along with the video tutorial you should then be able to build your project and run your multiplayer game, connect at least two instances of your game. This should then load your players into the multiplayer scene and you should be able to see the two players.

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.

Posted on

Set Up PUN 2 in Unity

How to make a Multiplayer Video Game in Unity using the Photon 2 plugin. For this lesson, we will teach you how to download, install, and set up the Photon 2 plugin in Unity.

In this Unity Multiplayer tutorial, you will learn why it is better to use the Photon 2 multiplayer plugin than other networking APIs such as the built-in Unity networking service. We will also teach you how to download, import, and set up the Photon 2 Multiplayer plugin. The Photon 2 Multiplayer plugin is our recommended plugin for creating multiplayer games in Unity. The Photon Multiplayer plugin is an easy to use API with good documentation and it supports all of the features you might need to create your multiplayer game which was my number one problem with Unity’s networking service. The Unity Networking service did not provide key multiplayer features such as Host Migration.

Photon Engine: https://dashboard.photonengine.com/en-US/account/signin
Payment Plans: https://www.photonengine.com/en/pun/pricing
Documentation: https://doc.photonengine.com/en-us/pun/current/getting-started/pun-intro
Scripting API: https://doc-api.photonengine.com/en/pun/v2/index.html

You will want to start by creating a new Unity Project. Once you have it created you can use Unity’s Collaboration service to back up your project. We will then want to download and import the Photon 2 multiplayer plugin. You can find it by searching for Photon PUN in the Unity asset store. You will want to download and import the PUN 2 – FREE plugin. Once you have imported all the assets you will want to go to the Photon website that I have linked to and login or create an account. After logging in you will want to click on Create New App. You will then need to select the Photon type from the drop-down menu which for us it Photon PUN. You will then need to go through the form and fill out all the required information about your project. You will then click on create to save this project.

You should now be on the home page for your Photon dashboard. You will then need to click on your new application’s AppID. You will want to copy this string value and go back to Unity. You will need to open the Photon PUN wizard window where you will paste in your AppID and hit Setup. After this, your Photon 2 Multiplayer project should be all set up and ready to start being developed.