This document is about: PUN 2
SWITCH TO

PUN Classic (v1)、PUN 2 和 Bolt 處於維護模式。 PUN 2 將支援 Unity 2019 至 2022,但不會添加新功能。 當然,您所有的 PUN & Bolt 專案可以用已知性能繼續運行使用。 對於任何即將開始或新的專案:請切換到 Photon Fusion 或 Quantum。

PlayFab Integration

Introduction

In this document we help you integrate PlayFab with Photon.
With this approach, both systems will get used in parallel to their full potential.
Billing is separate for each service.
Find PlayFab's Photon add-on here under the "Add-ons" - "Multiplayer" section of your title.
PlayFab's Photon add-on allows you to set up one or two Photon applications (one Photon Realtime and/or one Photon Chat application).
Read the instructions for the setup from PlayFab side in this guide.
Read on for setup instructions for Photon.

Custom Authentication

Dashboard Configuration

Here are the steps to setup custom authentication with PlayFab:

  1. Go to Photon dashbaord.
  2. Choose an application or create a new one.
  3. Click "Manage".
  4. Under "Authentication" section, click "Custom Server".
  5. [Required] Set Authentication URL to https://{PlayFabTitleId}.playfabapi.com/photon/authenticate.
    Make sure to replace {PlayFabTitleId} placeholder with your actual PlayFab TitleId.
    Do not keep the open ({) and closing (}) braces or curly brackets characters in the URL.
    Example: if your PlayFab TitleId is AB12: https://AB12.playfabapi.com/photon/authenticate.
  6. Save by hitting "Create".
  7. [Recommended] Untick "Allow anonymous clients to connect, independently of configured providers".

Client Code

Client is expected to send a pair of key/values as credentials:

C#

PhotonNetwork.AuthValues = new AuthenticationValues();
PhotonNetwork.AuthValues.AuthType = CustomAuthenticationType.Custom;
PhotonNetwork.AuthValues.AddAuthParameter("username", PlayFabUserId);
PhotonNetwork.AuthValues.AddAuthParameter("token", PlayFabPhotonToken);
// do not set AuthValues.Token or authentication will fail
// connect

Realtime Webhooks and WebRPC Configuration

If you do not need Realtime Webhooks nor WebRPCs for your application, you can skip this part.

It is recommended to setup custom authentication first.
Realtime Webhooks and WebRPCs may not work otherwise.

Here is how to setup Realtime Webhooks and WebRPCs to work with your PlayFab title.

  1. Go to Photon dashbaord.
  2. Choose an application or create a new one.
  3. Click "Manage".
  4. Under "Webhooks" section, click "Create a new Webhook".
  5. From the dropdown list "Select Type" choose Webhooks v1.2.
  6. [Required] Set "BaseUrl" to https://{PlayFabTitleId}.playfablogic.com/webhook/1/prod/{PhotonSecretKey}.
    • Make sure to replace {PlayFabTitleId} placeholder with your actual PlayFab TitleId.
      Do not keep the open ({) and closing (}) braces or curly brackets characters in the URL.
      Example: if your PlayFab TitleId is AB12 and your Photon Secret Key is 3z4ujpikyp4hbufno3jfm6fzcxsw5zxjaberfe4zkf4hzuen3:
      https://AB12.playfablogic.com/webhook/1/prod/3z4ujpikyp4hbufno3jfm6fzcxsw5zxjaberfe4zkf4hzuen3.
    • The "Photon Secret Key" is a string that you find on PlayFab's Photon add-on page once you add a Photon Realtime application.
    • You can replace prod with test if you want to target latest uploaded / pushed CloudScript revision as opposed to the latest deployed / active one.
  7. [Optional] Configure the Webhooks Paths you need.
    Remove those you do not need.
    Read more here.
  8. [Optional] Configure "IsPersistent", "HasErrorInfo" and "AsyncJoin".
    Remove their keys if you want to keep default values.
    Read more here.

Notes

  • "CustomHttpHeaders" setting is not supported as those are not exposed in the CloudScript handlers. Any value you set will not be useful.

  • PlayFab suggests the following names for Realtime Webhooks CloudScript handlers functions:

    • "PathCreate": "RoomCreated"
    • "PathClose": "RoomClosed" (do not change this handler name)
    • "PathJoin": "RoomJoined"
    • "PathLeave": "RoomLeft"
    • "PathEvent": "RoomEventRaised"
    • "PathGameProperties": "RoomPropertyUpdated"

    Other than "PathClose", you are free to choose whatever handler name you want for the other paths.
    PlayFab expects a valid PlayFabId as UserId argument in all Webhooks or WebRPCs handlers.
    The only exception to this is "RoomClosed".

  • All Photon Realtime Webhooks except "PathClose" and all WebRPCs should set PlayFab's CloudScript global variable "currentPlayerId" to the value of the "UserId" argument.

  • Configured Realtime Webhooks or WebRPCs called by client will not work unless there is an explicit CloudScript handler with the same name.

    Examples:
    If you configured "PathJoin" to "GameJoined", you need to have this function in the target CloudScript revision:

    JavaScript

    handlers.GameJoined = function(args)
    {
        // your custom code goes here
        return { ResultCode : 0, Message: "Success" }; 
    };
    

    If client calls "foo" WebRPC method, you need to have this function in the target CloudScript revision:

    JavaScript

    handlers.foo = function(args) 
    {
        // your custom code goes here
        return { ResultCode : 0, Message: "Success" }; 
    };
    
Back to top