This document is about: FUSION 1
SWITCH TO

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.

Read PlayFab's Photon Quickstart here.
Below are the setup instructions for Photon.

Currently, Fusion does not support the WebHooks integration with PlayFab.
For more information, please refer to the Client Code section below.

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:

Here is an example of how to authenticate a player using PlayFab and store the authentication values for later use in Fusion.

C#

using Fusion.Photon.Realtime;
using PlayFab;
using PlayFab.ClientModels;
using UnityEngine;

public static class PlayFabAuthenticator
{
    public static AuthenticationValues AuthValues { get; private set; }
    private static string _playFabPlayerIdCache;

    /*
     * Step 1
     * We authenticate a current PlayFab user normally.
     * In this case we use the LoginWithCustomID API call for simplicity.
     * You can absolutely use any Login method you want.
     * We use PlayFabSettings.DeviceUniqueIdentifier as our custom ID.
     * We pass RequestPhotonToken as a callback to be our next step, if
     * authentication was successful.
     */
    public static void AuthenticateWithPlayFab()
    {
        Debug.Log("PlayFab authenticating using Custom ID...");

        PlayFabClientAPI.LoginWithCustomID(new LoginWithCustomIDRequest()
        {
            CreateAccount = true,
            CustomId      = PlayFabSettings.DeviceUniqueIdentifier
        }, RequestPhotonToken, OnPlayFabError);
    }

    /*
     * Step 2
     * We request a Photon authentication token from PlayFab.
     * This is a crucial step, because Photon uses different authentication tokens
     * than PlayFab. Thus, you cannot directly use PlayFab SessionTicket and
     * you need to explicitly request a token. This API call requires you to
     * pass a Photon App ID. The App ID may be hard coded, but in this example,
     * we are accessing it using convenient static field on PhotonAppSettings class.
     * We pass in AuthenticateWithPhoton as a callback to be our next step, if
     * we have acquired the token successfully.
     */
    private static void RequestPhotonToken(LoginResult loginResult)
    {
        Debug.Log("PlayFab authenticated. Requesting photon token...");

        //We can player PlayFabId. This will come in handy during next step
        _playFabPlayerIdCache = loginResult.PlayFabId;

        PlayFabClientAPI.GetPhotonAuthenticationToken(new GetPhotonAuthenticationTokenRequest()
        {
            PhotonApplicationId = PhotonAppSettings.Global.AppSettings.AppIdFusion
        }, AuthenticateWithPhoton, OnPlayFabError);
    }

    /*
     * Step 3
     * This is the final and the simplest step. We create and store a new AuthenticationValues instance.
     * This class describes how to authenticate a player inside the Photon environment.
     */
    private static void AuthenticateWithPhoton(GetPhotonAuthenticationTokenResult authenticationTokenResult)
    {
        Debug.Log("Photon token acquired: " + authenticationTokenResult.PhotonCustomAuthenticationToken + "  Authentication complete.");

        //We set AuthType to custom, meaning we bring our own, PlayFab authentication procedure.
        var customAuth = new AuthenticationValues() { AuthType = CustomAuthenticationType.Custom };

        //We add "username" parameter. Do not let it confuse you: PlayFab is expecting this parameter to contain player PlayFab ID (!) and not username.
        customAuth.AddAuthParameter("username", _playFabPlayerIdCache); // expected by PlayFab custom auth service

        //We add "token" parameter. PlayFab expects it to contain Photon Authentication Token issues to your during previous step.
        customAuth.AddAuthParameter("token", authenticationTokenResult.PhotonCustomAuthenticationToken);

        //We finally store to use this authentication parameters throughout the entire application.
        AuthValues = customAuth;
    }

    private static void OnPlayFabError(PlayFabError obj)
    {
        Debug.Log(obj.GenerateErrorReport());
    }
}

Here is how you can use the PlayFabAuthenticator class in your Fusion application:

C#

public class RunnerManager : MonoBehaviour
{
    private void Awake()
    {
        PlayFabAuthenticator.AuthenticateWithPlayFab();
    }

    private static async Task<NetworkRunner> StartGame(GameMode gameMode)
    {
        var runner = CreateNetworkRunner(); // creates a custom NetworkRunner object

        // Use the PlayFabAuthenticator.AuthValues to authenticate the player
        var result = await runner.StartGame(new StartGameArgs()
        {
            GameMode   = gameMode,
            AuthValues = PlayFabAuthenticator.AuthValues // gets the PlayFab authentication values
        });

        return runner;
    }

    // ... other methods
}
Back to top