I.Introduction Photon Unity Networking

Photon Unity Networking is provided by ExitGames. It aims to provide an API consistent with Unity Networking, while at the same time solving many of the issues with Unity Networking, such as the dreaded NAT punch-through problem (players behind NAT are often unable to host game)

Photon Unity Networking works with another service known as Photon Cloud.

Just like Unity Networking, Photon Unity Networking uses UDP for communication, but the key difference is that players don’t directly host servers. Instead, clients connect to a server cluster (known as the “cloud”) and request a room. A room will be created on one of the servers in the cluster, and clients connect to this room.

This, in almost all cases, solves the aforementioned NAT issues with Unity, as all the players connect to a public server (with the ideal network configuration) instead of connecting directly to each other. Rather than making players do port-forwarding and other workarounds to get their private, self-hosted server visible to the public, servers are hosted externally and all of the work is done already. Players don’t connect to each other anymore, they simply connect to a server which acts as a middleman for communication.

Additionally, rather than having a “host” (which would normally be the player who hosts the server), Photon Unity Networking defines a “master client”. The player to create a room is, by default, the master client. However, if that player leaves, another player will be chosen to be the master client. Contrast this to Unity Networking, where if a host leaves, the game is simply ended or must be migrated, in PUN the game can seamlessly continue.

II.create multiplayer game using Photon Unity Networking

  • Creating a server:

    To create a server, we need to initialize it on the network and register it to the master server. The initialization requires a maximum amount of players and a port number (25000). For the server registration the name of the game should be unique, otherwise you might get in trouble with others projects using the same name. The room name can be any name, and in our case we eventually used the player name.

    All we need now is some form of input to let us actually start the server when we want to. To test it out we create buttons using the Unity GUI. We only want to see these buttons if we have not started a server or joined one, so the button will show itself if the user is neither a client nor a server.

  • Joining a server:

    To achieve this, we need to send a request to the master server to get a list of HostData. This contains all data required to join a server. Once the host list is received, a message is sent to the game which triggersOnMasterServerEvent(). This function is called for several events, so we need to add a check to see if the message equals MasterServerEvent.HostListReceived. If this is the case, we can store the host list.

    To join a server, all we need is one entry of the host list. The OnConnectedToServer() is called after we actually joined the server. We will extend this function at a later point

  • Create game:

    To create game like some game with Unity.



Unity Multiplayer Games (ebook) – Alan R. Stagner


All Rights Reserved

Let's register a Viblo Account to get more interesting posts.