This document is about: FUSION 2
SWITCH TO

PlayFabとのインテグレーション

イントロダクション

このドキュメントでは、PlayFabとPhotonのインテグレーションをサポートします。 このアプローチを使用すれば、PlayFabとPhotonを併用して、最大限活用できます。 請求は、それぞれのサービスについて個別に発生します。 PlayFabの Photonアドオンはこちら で参照してください-ご自分のタイトルの "アドオン" - "マルチプレイヤー" セクションの下です。 PlayFabのPhotonアドオンでは、1つまたは2つのPhotonアプリケーションをセットアップできます(1つのPhoton Realtimeアプリケーション、および/または1つのPhoton Chatアプリケーション)。 PlayFab側からの設定手順は こちらのガイドを参照してください。 Photon向けのセットアップ手順を参照してください。

現在、FusionはPlayFabのWebHooksインテグレーションをサポートしていません。 詳細は、以下のクライアントコードセクションをご覧ください。

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

カスタム認証

ダッシュボードの設定

以下に、PlayFabでカスタム認証をセットアップする際の手順を示します:

  1. Photon ダッシュボードに進みます。
  2. アプリケーションを選択するか、またはアプリケーションを新規作成します。
  3. 「管理」をクリックします。
  4. 「認証」セクションで、「カスタムサーバー」をクリックします。
  5. [必須] 認証URLをhttps://{PlayFabTitleId}.playfabapi.com/photon/authenticateに設定します。 {PlayFabTitleId}プレースホルダーを実際のPlayFab TitleIdで置換するよう留意してください。
  6. 「作成」をクリックして保存します。
  7. [推奨] 「設定済みのプロバイダと関係のない、匿名クライアントの接続を許可する」をチェックしないでください。

クライアントコード

クライアントは、キー/値のセットを機密情報として送信するよう予期されています。

  • ログインしたユーザーのPlayFabユーザーID
  • Photonトークン(GetPhotonAuthenticationToken クライアントAPIメソッドを使用して取得).

以下の例は、PlayFabを使用してプレイヤーを認証し、後に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());
    }
}

FusionアプリケーションでPlayFabAuthenticatorクラスを使用する方法は以下の通りです。

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