Photon Pun 2



Welcome to this new tutorial series on creating multiplayer video games in Unity using the Photon 2 PUN plugin. For this lesson, we will show you how to down. The ping result can be overridden via PhotonNetwork.OverrideBestCloudServer(.) This call can take up to 2 seconds if it is the first time you are using this, all cloud servers will be pinged to check for the best region. The PUN Setup Wizard stores your appID in a settings file and applies a server address/port.

Photon Cloud provides you with global connectivity to allow low latency gaming all around the world.

The initial connection of clients goes to a Photon Name Server, which provides the list of available regions.

Each region is completely separate from the others and consists of a Master Server (for matchmaking) and Game Servers (hosting rooms).

The full list of available regions is below.In the Dashboard, you can define which regions should be available for the clients.

Pun

Contents

  • Best Region Selection
  • Available Regions

Best Region Selection

PUN and Photon Voice rely on the Realtime API layer to pick the Best Region.

Photon Pun 2

C# Realtime API

Photon Realtime can detect the Best Region to connect to and enables you to stick to that region.

To do so, the clients always fetch the list of available regions from the Name Server on connect.This is used to get an up-to-date regions list and check if the saved Best Region -if any- is still available.

After pinging the servers, the results are summarized in a string which should be saved on the device for later use.The summary string includes the current best region, its ping and the current regions list.

Without results from a previous session, all regions will be pinged, which takes a moment longer.If a previous result is available, the client will check:

Photon Pun 2 Unity

a. if the region list changed (covers the case if the 'previous best region' is still available)
b. if the ping is no longer acceptable (>= 1.5x slower than previously saved reference value)

If either applies, all regions are pinged and a new result gets picked.

Using Best Region works well with the server-side Region Filter in the Dashboard.It enables you to change the list of regions available to players on demand.

To access the list of regions or to override previous results, refer to the API Reference for regions.

PUN

'Best Region' selection is done by default when you use PhotonNetwork.ConnectUsingSettings().

Aside from automatically saving the results in the player preferences, the workflow is the same as in the Realtime API.

The best region is 'sticky'.This means the client will typically use one region for a longer time.On subsequent starts, it will ping this region again.PUN automatically stores the best region summary using Unity's PlayerPrefs.

For convenience and debugging purposes, the current 'Best Region' and its ping are exposed in the PhotonServerSettings in Unity Editor.However, that is only valid for the Unity Editor's play mode.Builds that run on the same machine as the Unity Editor may have a different Best Region.

Best Region Considerations

'Best Region' option is not deterministic.Sometimes it may be 'random' due to little variations or exact same ping calculations.

Theoretically, you could:

  • have the same exact ping to multiple regions from the same device. So its random, if you end up with different regions on clients connected to the same network.
  • different ping values for the same region on different devices (or different retries on the same device) connected to the same network.

For instance, in the case of 'us' and 'usw' (or 'ru' and 'rue'), you could either make use of the online regions whitelist to select the ones you want and drop the others or connect to an explicit region.

Photon

Development Region

Photon Pun 2

Starting from PUN v2.17, we added a new feature called 'Dev Region'.It's a new setting available in PhotonServerSettings.With this setting, all development builds will use the same region, avoiding initial matchmaking problems with the 'Best Region' selection.'Development build' is enabled automatically when the PhotonServerSettings gets created and the 'Dev Region' is set during the first run (PlayMode) in Unity Editor.

The 'Dev Region' is only used in Unity Editor and in 'Development' builds, when you use PhotonNetwork.ConnectUsingSettings() to connect.You can also disable the 'Dev Region' in Unity Editor and 'Development Build' by simply deleting the value.

So to avoid Best Region selection issues during the development phase, make sure to update to the latest PUN 2 version.Run once in Unity Editor (enter PlayMode and connect).You can do so from any of the demo scenes.This first connection from Unity Editor will set the 'Dev Region' which can be seen from the PhotonServerSettings inspector.Once this is done, you can build and test from devices or do new build if you already did before.You will notice that 'Development Build' is enabled now from Build Settings and should remain this way during the development period.This way, all clients (Unity Editor and builds) will connect to the same 'Dev Region'.

Before going into production, do not forget to disable development build.

Note: if a client is connected from a development build or from Unity Editor and the other is connected from a non-development build, they could be connected to two different regions.

Available Regions

Photon Cloud has servers in several regions, distributed across multiple hosting centers over the world.

Each Photon Cloud region is identified by a 'region token'.

To connect to a specific region, set its code as 'Fixed Region' in your PhotonServerSettings under AppSettings.If you want to use PhotonNetwork.ConnectToRegion, you need to configure the networking client manually (mainly AppId and AppVersion) as PhotonServerSettings will not be used in this case.

List of available regions and tokens:

The strings for region tokens(or codes) are case insensitive. Example: 'EU' or 'eu' are both accepted and refer to the same Europe region.
RegionHosted inToken
AsiaSingaporeasia
AustraliaMelbourneau
Canada, EastMontrealcae
Chinese Mainland1 (See Instructions)Shanghaicn
EuropeAmsterdameu
IndiaChennaiin
JapanTokyojp
RussiaMoscowru
Russia, EastKhabarovskrue
South AfricaJohannesburgza
South AmericaSao Paulosa
South KoreaSeoulkr
USA, EastWashington D.C.us
USA, WestSan Joséusw

1: Chinese Mainland region needs separate AppId and subscription.

Dashboard Regions Filtering

You can filter the list of available Photon Cloud regions per application on the fly directly from the dashboard.

Go to the dashboard and then click 'Manage' for a chosen application and then click 'Edit'.You will find an input field where you can enter the list of whitelisted regions as follows:

  • the allowed list should be a string of region tokens separated by semicolons. e.g. 'eu;us'.
  • region tokens are case insensitive and are defined here.
  • undefined or unrecognized region tokens will be ignored from the list.
  • empty (') or malformed string (e.g. ';;;') means empty list.
  • empty list means all available regions are allowed.

Once you confirm and save, the operation GetRegions will return only the filtered list of regions.Thus, clients should select from that list but it's fully possible clients connect to any available region explicitly.Take into consideration that dashboard updates propagation can take up to 10 minutes.

Using The Chinese Mainland Region

You need to request access to the Chinese Mainland region for your Photon application. Send us an email so we could unlock it for your AppID.
You cannot subscribe to paid plans to be used in the Chinese Mainland region via our website.Reach out to us by email to receive a quote for any subscription.
Photon

Photon Pun 2 Synchronization

The Photon Name Server has to be local to China, as the firewall might block the traffic otherwise.The Chinese Photon Name Server is 'ns.photonengine.cn'.

Connecting with clients from outside of China mainland will most likely not produce good results.Also, connecting from the Photon servers to servers outside of China mainland (e.g. for Custom Authentication, WebHooks, WebRPCs) might not be reliable.

Important: in the current phase, changes you make to your app via your dashboard are not automatically reflected in the app caches for China.Let us know by email if you have an update request there.

Also, for legal reasons, you need a separate build for China and we recommend using a separate AppId with it.For example, use a compile condition (of your choice) to change the AppId and the Photon Name Server depending on the build.

Photon Pun 2 Pricing

Follow the instructions corresponding to your client SDK to make a special build for the Chinese market.

Photon Pun 2 Documentation

To Document Top