Photon Unity



Photon Unity Networking (PUN) is a Unity package for multiplayer games. Flexible matchmaking gets your players into rooms where objects can be synced over the network. RPCs, Custom Properties or 'low level' Photon events are just some of the features. The fast and (optionally) reliable communication is done through dedicated Photon server(s), so clients don't need to connect one to one.

Photon Unity Networking framework for realtime multiplayer games and applications with no punchthrough issues. Export to all Unity supported platforms, no matter what Unity license you have! Get the Photon Unity Networking Classic - FREE package from Exit Games and speed up your game development process. Find this & other Network options on the Unity Asset Store. Sprint into Spring Sale is on: Get 50% off top assets and score extra savings with coupon code SPRING2021. Summarizes a 'player' within a room, identified (in that room) by actorID. Each player has an actorId (or ID), valid for that room. It's -1 until it's assigned by server. Each client can set it's player's custom properties with SetCustomProperties, even before being in a room. Photon Unity Network (PUN) is our is our take on a Unity specific, high-level solution: Matchmaking, easy to use callbacks, components to synchronize GameObjects, Remote Procedure Calls (RPCs) and similar features provide a great start. Beyond that is a solid, extensive API for more advanced control.

PUN exports to basically all platforms supported by Unity and has two options:

PUN FREE

No-cost package with various demos, pre-made scripts and reference documentation. Exports to basically all platforms.

PUN PLUS

Same content as PUN FREE, plus a 100 concurrent user plan for the Photon Cloud (approx. 40k MAU, valid 12 months).

Join our discord server to chat with the community or one of our team members when available.

PUN's Structure

Unity

Usually, you don't have to mind the structure of the PUN package but just for the overview, here's how things stack up. The PUN package wraps up three layers of APIs:

  • The highest level is the PUN code, which implements Unity-specific features like networked objects, RPCs and so on.
  • The second level contains the logic to work with Photon servers, do matchmaking, callbacks and such. This is the Realtime API. This can be used on it's own already. You will notice a lot of overlap of topics between PUN and the Realtime API (a.k.a. LoadBalancing API) but that's fine.
  • The lowest level is made up of DLL files, which contain the de/serialization, protocols and such.

Get Started

To get the best out of PUN, you will need to do some programming.This page shows several important code snippets as an overview.

To get properly started, work through the 'PUN Basics Tutorial'.

Connect And Callbacks

ConnectUsingSettings gets you online in no time: It grabs all important settings from the PhotonServerSettings asset and off you go.

PUN uses callbacks to let you know when the client established the connection, joined a room, etc..

For example: IConnectionCallbacks.OnConnectedToMaster.

For convenience, PUN has the MonoBehaviourPunCallbacks MonoBehaviour. It implements important callback-interfaces and registers itself automatically, so you can inherit it and just override specific callback-methods.

Alternatively implement IConnectionCallbacks in any class and register instances for callbacks via PhotonNetwork.AddCallbackTarget.

Matchmaking

Within OnConnectedToMaster you could try to join an existing room or create your own.The following code snippets show possible method calls to start or join games.

When friends want to play together and have a way to communicate outside of PUN (e.g. with Photon Chat, Facebook), they can make up a room name and use JoinOrCreateRoom. If nobody else should be matched into this room, make it invisible for matchmaking:

With JoinOrCreateRoom, the room gets created on demand, so it doesn't matter who is first.If it's full, IMatchmakingCallbacks.OnJoinRoomFailed gets called (if you implemented and registered it somewhere).

Read more about matchmaking in our guide.

Photon

Game Logic

Photon Unity Webgl

GameObjects can be instantiated as 'networked GameObjects' with a PhotonView component. It identifies the object and the owner (or controller). The player who's in control, updates everyone else.

Typically, you would add a PhotonView to a prefab, select the Observed component for it and use PhotonNetwork.Instantiate to create an instance.

Unity

The observed component of a PhotonView is in charge of writing (and reading) the state of the networked object several times a second. To do so, a script must implement IPunObservable, which defines OnPhotonSerializeView. It looks like this:

Photon Unity

Clients can do Remote Procedure Calls on specific networked objects for anything that happens infrequently:

Independent from GameObjects, you can also send your own events:

Read more about PUN's RPCs and RaiseEvent.

Photon Unity

Photon Unity Webgl

Demos And Tutorials

In the PUN packages, you find several demos and useful scripts, which you can reuse and or dissect and redo.

To get properly started, take your time to read and code the 'PUN Basics Tutorial'.

To Document Top

For this lesson on how to make a multiplayer video game in Unity with the Photon 2 plugin in Unity 3D, I will show you how to create a character selection option that will then synchronize the selected character across the network. Features like the are prevalent in many video games today such as Rainbow Six Siege, Call of Duty Black Ops 4, and Fortnite. This lesson will teach you the basics of RPC or Remote Procedure Call Functions. RPC’s are probably one of the most important principles in developing a multiplayer video game. It is through RPC functions that we are able to sync data from one client to another. After following this tutorial you should be able to create your own RPC function and implement them into your own video games.

In between lesson I create a menu system with four different buttons. This will allow us to selected different characters for us to use as your player avatar prefab. We will begin by creating a new C sharp script called Player Info. This script will save the value of the character our player has chosen.

We then need to create a new C# script that will control do a new menu system. Inside this function, we only need to have a public function that will set the value of the character model we want to use.

We then need to create a Player Info object and a Menu Controller Object. We will then set the on click events of your buttons in our menu system.

We then need to create another C# script. Inside this script, we will create an RPC function that will synchronize our character model value across the network and instantiate it.

Photon Unity Tutorial

If you followed along with the video you should now be able to build your project. Once built you should be able to select different character models which will then be instantiated into the multiplayer scene once connected.