본문 바로가기

Unity2020.1

[Unity] Google Login하기

준비물

1. 앱 업로드

https://play.google.com/console/

 

Google Play Console | Google Play Console

Google Play Console로 앱과 게임을 게시 및 관리하고 Google Play에서 비즈니스를 성장시키세요. 앱의 품질을 개선하고, 잠재고객의 참여를 유도하고, 수익을 창출하는 데 도움이 되는 기능을 알아보세

play.google.com

2. OAuth Client ID

https://console.cloud.google.com/

 

Google Cloud Platform

하나의 계정으로 모든 Google 서비스를 Google Cloud Platform을 사용하려면 로그인하세요.

accounts.google.com

https://hesterdiary.tistory.com/26

 

OAuth Client ID 생성하기

1. Google Cloud Platform에 접속한다. https://console.cloud.google.com/home/ Google Cloud Platform 하나의 계정으로 모든 Google 서비스를 Google Cloud Platform을 사용하려면 로그인하세요. accounts.googl..

hesterdiary.tistory.com

남은 작업

1. Android Config Setup

2. Build 및 테스트

작업 시작

1. Android Config Setup

Unity에 해당 경로로 팝업을 출력한다.

①은 Google Play Console에서 로그인을 한 후

업로드된 앱 보기를 선택한다.

Play 게임 서비스-> 설정 및 관리->설정을 선택한 후

현재 프로젝트 사용자 인증 정보 옆에 리소스보기를 클릭한다.

위의 내용을 복붙하면 된다.

②은 Google Cloud Platform에서

클라이언트ID를 복붙하면 된다.

이제 Setup버튼을 눌러보면

위와 같은 팝업이 뜬다면 성공이다!

2. Build 및 테스트

Scene구성은 위와 같이 하고 로그인에 성공하면 LoginState에 Success가 출력되고 UserName이 출력될 것이다.

using UnityEngine;

using UnityEngine.UI;
using GooglePlayGames;
using GooglePlayGames.BasicApi;

namespace GameScene
{
    public class FirstScene : MonoBehaviour
    {
        [SerializeField] GameObject _objUserNameActiveText = null;
        [SerializeField] GameObject _objUserNameDeactiveText = null;
        [SerializeField] Button _btnLogin = null;

        [SerializeField] private Text _txtUserName = null;
        [SerializeField] private Text _txtLoginBtn = null;
        [SerializeField] private Text _txtLoginState = null;

        private const string _strLogin = "Login";
        private const string _strLogout = "Logout";
        private const string _strSuccess = "Success";
        private const string _strFail = "Fail";
        private const string _strDefault = "-";

        private void Awake()
		{
            PlayGamesClientConfiguration config = new PlayGamesClientConfiguration.Builder().Build();
            PlayGamesPlatform.InitializeInstance(config);
            PlayGamesPlatform.DebugLogEnabled = true;
            PlayGamesPlatform.Activate();

            _btnLogin.onClick.AddListener(OnClickLogin);
            Refresh();
        }
        private void Refresh()
		{
            bool isAuthenticated = Social.localUser.authenticated;

            _txtLoginBtn.text = isAuthenticated ? _strLogout : _strLogin;
            _txtUserName.text = isAuthenticated ? Social.localUser.userName : string.Empty;
            _objUserNameActiveText.SetActive(isAuthenticated);
            _objUserNameDeactiveText.SetActive(!isAuthenticated);
        }
        #region Button Event
        private void OnClickLogin()
        {
            if (!Social.localUser.authenticated)
            {
                Social.localUser.Authenticate((bool bSuccess) =>
                {
                    _txtLoginState.text = bSuccess ? _strSuccess : _strFail;
                    Refresh();
                });
            }
            else
            {
                ((PlayGamesPlatform)Social.Active).SignOut();
                _txtLoginState.text = _strDefault;
                Refresh();
            }           
        }
		#endregion
	}
}

다른 유튜브와 블로그 주소를 참고해서 적용하였다. 버튼 같은 건 사실 코드로 연결하는 건 나중에 업데이트할 때 불편하니 외부에서 연결해주는 게 좋다.

 

필수 코드는 아래와 같이 초기화와 인증하는 코드만 들어가면 된다.

PlayGamesClientConfiguration config = new PlayGamesClientConfiguration.Builder().Build();
PlayGamesPlatform.InitializeInstance(config);
PlayGamesPlatform.DebugLogEnabled = true;
PlayGamesPlatform.Activate();
Social.localUser.Authenticate((bool bSuccess) =>{}); //로그인
((PlayGamesPlatform)Social.Active).SignOut(); //로그아웃

빌드를 한 후에 실행을 시켜보면 아래와 같이 잘 동작하는 걸 확인할 수 있다.

 

참고:

https://www.youtube.com/watch?v=M6nwu00-VR4&t=971s 

https://scvtwo.tistory.com/75

 

[Unity] 유니티 구글 플레이 게임 서비스 연동 ( 로그인 하기 )

안녕하세요. 이 글은 이전에 작성한  [Unity] 유니티 구글 플레이 게임 서비스(GPGS) 연동 후 앱 등록을 진행한 것을 기반으로 합니다. [Unity] 유니티 구글 플레이 게임 서비스(GPGS) 연동 후 앱등록

scvtwo.tistory.com