Provide a Facebook sign-in option to enable players to authenticate using their Facebook accounts in your game.
읽는 시간 1분최근 업데이트: 한 달 전
최소 SDK 버전: 2.0.0
이 문서에서는 게임에서 Facebook 계정을 사용해 플레이어의 인증을 설정하는 다음 시나리오에 대해 설명합니다.
게임에서 플레이어에게 Facebook 로그인 옵션을 제공하려면 Facebook 개발자 포털에서 앱을 생성한 다음 Unity용 Facebook SDK를 설치해 사용자를 로그인 처리하고 액세스 토큰을 가져오십시오.
Facebook 계정 로그인 설정
아래의 단계에서는 Facebook 앱을 이미 설정했다고 가정합니다.- Facebook의 시작하기 기술 자료에 따라 Facebook SDK를 설정하고 설치합니다.
-
Unity Authentication의 ID 제공업체로 Facebook을 추가합니다.
- Unity 에디터 메뉴에서 Edit > Project Settings… 로 이동한 후, 내비게이션 메뉴에서 Services > Authentication을 선택합니다.
- ID Providers를 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 액세스 토큰을 가져온 경우,SignInWithFacebookAsync(string accessToken)Facebook 계정 연결 해제
플레이어가 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); }}