본문 바로가기
개발/Playfab

유니티 플레이팹 구글 로그인 Playfab Sign In with Google Login 간단 구현

by SPNK 2022. 6. 22.
반응형

구글 로그인 SDK 설치

 

GitHub - playgameservices/play-games-plugin-for-unity: Google Play Games plugin for Unity

Google Play Games plugin for Unity. Contribute to playgameservices/play-games-plugin-for-unity development by creating an account on GitHub.

github.com

 

구글 로그인 구현

using GooglePlayGames;
using GooglePlayGames.BasicApi;
using PlayFab;
using PlayFab.ClientModels;
using PlayFab.Json;
using PlayFab.ProfilesModels;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using EntityKey = PlayFab.ProfilesModels.EntityKey;

public class PlayfabManager : MonoBehaviour
{
    private void Awake()
    {
#if UNITY_ANDROID
        GoogleActivate();
#endif
    }

    private void GoogleActivate() //구글 초기화
    {
        PlayGamesClientConfiguration config = new PlayGamesClientConfiguration.Builder()
        .AddOauthScope("profile")
        .RequestServerAuthCode(false)
        .Build();

        PlayGamesPlatform.InitializeInstance(config);
        PlayGamesPlatform.DebugLogEnabled = true;
        PlayGamesPlatform.Activate();
    }

    public void OnClickGoogleLogin() //구글 로그인 버튼
    {
#if UNITY_ANDROID
        LoginGoogleAuthenticate();
#else
        SetEditorOnlyMessage("Only Android Platform");
#endif
    }

    private void LoginGoogleAuthenticate() //로그인 시도
    {
        Debug.Log("구글 로그인 시도중");

        if (Social.localUser.authenticated)
        {
            Debug.Log("이미 구글 로그인 되어있는 상태입니다.");
            return;
        }

        Social.localUser.Authenticate((bool success) =>
        {
            if (!success)
            {
                Debug.Log("구글 사용자 인증 실패!");
                return;
            }

            var serverAuthCode = PlayGamesPlatform.Instance.GetServerAuthCode();
            PlayFabClientAPI.LoginWithGoogleAccount(new LoginWithGoogleAccountRequest()
            {
                TitleId = PlayFabSettings.TitleId,
                ServerAuthCode = serverAuthCode,
                CreateAccount = true,
            },
            result =>
            {
                Debug.Log("플레이팹 구글 로그인 성공!");
                OnLoginSuccess(result);
            },
            error =>
            {
                Debug.Log("플레이팹 구글 로그인 실패!");
            });
        });
    }

    public void OnClickGoogleLogout() //구글 로그아웃
    {
        ((PlayGamesPlatform)Social.Active).SignOut();
    }


    public void OnLoginSuccess(LoginResult result) //로그인 결과
    {
        Debug.Log("Playfab Login Success");
    }
}

 

게스트 로그인 상태에서 구글 로그인으로 전환하기

    public void OnClickGoogleLink()
    {
#if UNITY_ANDROID
        if (!Social.localUser.authenticated)
        {
            Social.localUser.Authenticate((bool success) =>
            {
                if (success)
                {
                    var serverAuthCode = PlayGamesPlatform.Instance.GetServerAuthCode();

                    LinkGoogleAccountRequest request = new LinkGoogleAccountRequest()
                    {
                        ForceLink = true,
                        ServerAuthCode = serverAuthCode
                    };

                    PlayFabClientAPI.LinkGoogleAccount(request, result =>
                    {
                        Debug.Log("Link Google Account Success");
                    }, error =>
                    {
                        Debug.Log(error.GenerateErrorReport());
                    });
                }
                else
                {
                    Debug.Log("Link Google Account Fail");
                }
            });
        }
        else
        {
            Debug.Log("Link Google Account Fail");
        }
    }

 

 

  • 플레이팹 사이트 / 추가 콘텐츠 / Google 설정

 

Google 앱 패키지 Id : com.xxx.xxxx

 


 

Google 앱 라이선스 키 : 구글 콘솔 / 수익 창출 설정 / 라이선스 키

 


  • 플레이팹 Api 참고

 

 

Unity에서 Google Play 게임 로그인으로 PlayFab 인증 - PlayFab

Unity에서 Google Play 게임 로그인을 사용한 PlayFab 인증의 예제를 안내합니다.

docs.microsoft.com

 


참고할만한 글

 

플레이팹 Playfab 게스트 로그인 Guest Login 간단 사용법

using PlayFab; using PlayFab.ClientModels; using PlayFab.Json; using PlayFab.ProfilesModels; using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using Entity..

parksh3641.tistory.com

 

유니티 플레이팹 애플 로그인 Playfab Sign ln with Apple Login 간단 구현

애플 SDK https://github.com/lupidan/apple-signin-unity/releases/tag/v1.4.2 Release Sign in with Apple Unity Plugin v1.4.2 · lupidan/apple-signin-unity Changed Handles empty NSPersonNameComponents sent by Apple when not requesting a name, to be nil nativ

parksh3641.tistory.com

 

 

반응형

댓글