Provide a Facebook sign-in option to enable players to authenticate using their Facebook accounts in your game.
読み終わるまでの所要時間 2 分最終更新 1ヶ月前
最小 SDK バージョン: 2.0.0
このセクションでは、Facebook アカウントを使用してゲーム内でのプレイヤーの認証を設定する以下のシナリオについて説明します。
ゲーム内のプレイヤーに Facebook サインインのオプションを提供するには、Facebook 開発者ポータルでアプリケーションを作成し、Facebook SDK for Unity をインストールしてプレイヤーをサインインし、アクセストークンを取得してください。
Facebook アカウントサインインを設定する
以下の手順では、Facebook アプリケーションを設定済みであることを前提としています。- Facebook の入門ドキュメント に従って、Facebook SDK を設定し、インストールします。
-
Facebook を Unity Authentication 用の ID プロバイダーとして追加します。
- Unity エディターメニューで、Edit (編集) > Project Settings... (プロジェクト設定...) を選択し、ナビゲーションメニューから Services (サービス) > Authentication を選択します。
- ID Providers (ID プロバイダー) を Facebook に設定し、Add (追加) をクリックします。
- App ID テキストフィールドにアプリケーション ID (ステップ 1 で確認) を入力します。
- App Secret テキストフィールドにアプリケーションシークレットを入力し、Save (保存) をクリックします。
-
こちらのサンプルコードを使用 して Facebook サインインを実装します。
using System.Collections.Generic;using UnityEngine;// Other needed dependenciesusing Facebook.Unity;public class FacebookExampleScript : MonoBehaviour{ public string Token; public string Error; // Awake function from Unity's MonoBehaviour void Awake() { if (!FB.IsInitialized) { // Initialize the Facebook SDK FB.Init(InitCallback, OnHideUnity); } else { // Already initialized, signal an app activation App Event FB.ActivateApp(); } } void InitCallback() { if (FB.IsInitialized) { // Signal an app activation App Event FB.ActivateApp(); // Continue with Facebook SDK } else { Debug.Log("Failed to Initialize the Facebook SDK"); } } void OnHideUnity(bool isGameShown) { if (!isGameShown) { // Pause the game - we will need to hide Time.timeScale = 0; } else { // Resume the game - we're getting focus again Time.timeScale = 1; } } public void Login() { // Define the permissions var perms = new List<string>() { "public_profile", "email" }; FB.LogInWithReadPermissions(perms, result => { if (FB.IsLoggedIn) { Token = AccessToken.CurrentAccessToken.TokenString; Debug.Log($"Facebook Login token: {Token}"); } else { Error = "User cancelled login"; Debug.Log("[Facebook Login] User cancelled login"); } }); }}
戻ってきたプレイヤーのサインインまたは新しいプレイヤーの作成
SignInWithFacebookAsync- Facebook の認証情報を使用して新しい Unity Authentication プレイヤーを作成する。
- Facebook の認証情報を使用して既存のプレイヤーをサインインする。
SignInWithFacebookAsyncSignInWithFacebookAsyncSignInWithFacebookAsyncasync Task SignInWithFacebookAsync(string accessToken){ try { await AuthenticationService.Instance.SignInWithFacebookAsync(accessToken); Debug.Log("SignIn is successful."); } catch (AuthenticationException ex) { // Compare error code to AuthenticationErrorCodes // Notify the player with the proper error message Debug.LogException(ex); } catch (RequestFailedException ex) { // Compare error code to CommonErrorCodes // Notify the player with the proper error message Debug.LogException(ex); }}
プレイヤーを匿名から Facebook アカウントへと更新する
匿名認証を設定した後、プレイヤーが匿名からアップグレードし、Facebook アカウントを作成して Facebook を使用してサインインすることを希望する場合は、ゲームのプレイヤーにプロンプトを表示し、Facebook サインインをトリガーして Facebook からアクセストークンを取得するよう求める必要があります。その後、LinkWithFacebookAsync- を使用して、キャッシュされたプレイヤーのアカウントにサインインします。
SignInAnonymouslyAsync - を使用して、キャッシュされたプレイヤーのアカウントを Facebook アカウントにリンクします。
LinkWithFacebookAsync
プレイヤーがサインインするか新しいプレイヤープロファイルを作成して Facebook サインインをトリガーする場合、Facebook アクセストークンを受け取ったら、以下の API を呼び出してプレイヤーの認証を行います。async Task LinkWithFacebookAsync(string accessToken){ try { await AuthenticationService.Instance.LinkWithFacebookAsync(accessToken); Debug.Log("Link is successful."); } catch (AuthenticationException ex) when (ex.ErrorCode == AuthenticationErrorCodes.AccountAlreadyLinked) { // Prompt the player with an error message. Debug.LogError("This user is already linked with another account. Log in instead."); } catch (AuthenticationException ex) { // Compare error code to AuthenticationErrorCodes // Notify the player with the proper error message Debug.LogException(ex); } catch (RequestFailedException ex) { // Compare error code to CommonErrorCodes // Notify the player with the proper error message Debug.LogException(ex); }}
async Task SignInWithFacebookAsync(string accessToken){ try { await AuthenticationService.Instance.SignInWithFacebookAsync(accessToken); Debug.Log("SignIn is successful."); } catch (AuthenticationException ex) { // Compare error code to AuthenticationErrorCodes // Notify the player with the proper error message Debug.LogException(ex); } catch (RequestFailedException ex) { // Compare error code to CommonErrorCodes // Notify the player with the proper error message Debug.LogException(ex); }}
Facebook サインイン認証を実装する
プレイヤーが Facebook サインインをトリガーしてサインインするか新しいプレイヤーを作成する場合、Facebook アクセストークンを受け取ったら、以下の API を呼び出してプレイヤーをサインインします:SignInWithFacebookAsync(string accessToken)Facebook アカウントをリンク解除する
UnlinkFacebookAsyncasync Task UnlinkFacebookAsync(string idToken){ try { await AuthenticationService.Instance.UnlinkFacebookAsync(idToken); Debug.Log("Unlink is successful."); } catch (AuthenticationException ex) { // Compare error code to AuthenticationErrorCodes // Notify the player with the proper error message Debug.LogException(ex); } catch (RequestFailedException ex) { // Compare error code to CommonErrorCodes // Notify the player with the proper error message Debug.LogException(ex); }}