최소 SDK 버전: 2.0.0
참고: 아래 코드 예시가 작동하려면 올바른 SDK 및 플러그인 버전을 사용해야 합니다.
이 문서에서는 게임에서 Google 계정을 사용해 플레이어의 인증을 설정하는 다음 시나리오에 대해 설명합니다.
- Google 로그인 설정
- 기존 사용자 로그인 또는 새 사용자 생성
- 사용자를 익명 로그인에서 Google 계정을 통한 플랫폼 로그인으로 업데이트
게임에서 플레이어에게 Google 로그인 옵션을 제공하려면 Google Play Console에서 앱을 생성하고 Google Play Games plugin for Unity v10.14를 설치해 플레이어를 로그인 처리하고 액세스 토큰을 가져오십시오. 이를 위해서는 Firebase 또는 Google과 같은 다른 서비스에서 플레이어를 식별하기 위해 Open Authentication(ID) 액세스 토큰이 필요합니다.
{
((PlayGamesLocalUser)Social.localUser).GetIdToken()
}
v10.14 이상을 실행하는 플러그인 버전은 Google Play 게임즈를 참고하십시오.
참고: 아래 코드에서는 플레이어의 Google ID 토큰을 이미 획득했다고 가정합니다.
Google 로그인 설정
참고: Google 로그인은 Play Games Services v1 SDK를 사용하는 Google Play Games plugin for Unity v10.14 이하를 통해 지원됩니다.
- Google Play Games plugin for Unity v10.14를 다운로드하고 임포트합니다.
- 앱의 Google 로그인을 활성화합니다. Google 로그인을 사용하도록 게임을 설정하려면 Google의 기술 자료를 참고하십시오.
- Unity Authentication의 ID 제공업체를 Google로 설정합니다.
- Unity 에디터 메뉴에서 Edit > **Project Settings…**로 이동한 후, 내비게이션 메뉴에서 Services > Authentication을 선택합니다.
- ID Providers를 Google로 설정한 후, Add를 선택합니다.
- Client ID 텍스트 필드에 웹 앱 클라이언트 ID를 입력한 후, Save를 선택합니다.
참고: Authentication SDK를 사용하려면 플러그인 설정 다이얼로그에서 웹 앱 클라이언트 ID를 설정해야 합니다.
void InitializePlayGamesLogin()
{
var config = new PlayGamesClientConfiguration.Builder()
// Requests an ID token be generated.
// This OAuth token can be used to
// identify the player to other services such as Firebase.
.RequestIdToken()
.Build();
PlayGamesPlatform.InitializeInstance(config);
PlayGamesPlatform.DebugLogEnabled = true;
PlayGamesPlatform.Activate();
}
void LoginGoogle()
{
Social.localUser.Authenticate(OnGoogleLogin);
}
void OnGoogleLogin(bool success)
{
if (success)
{
// Call Unity Authentication SDK to sign in or link with Google.
Debug.Log("Login with Google done. IdToken: " + ((PlayGamesLocalUser)Social.localUser).GetIdToken());
}
else
{
Debug.Log("Unsuccessful login");
}
}
기존 플레이어 로그인 또는 신규 플레이어 생성
SignInWithGoogleAsync
메서드를 사용하여 다음 중 하나를 수행할 수 있습니다.
- Google 자격 증명을 사용해 새 Unity Authentication 플레이어 생성
- Google 자격 증명을 사용해 기존 플레이어 로그인
async Task SignInWithGoogleAsync(string idToken)
{
try
{
await AuthenticationService.Instance.SignInWithGoogleAsync(idToken);
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);
}
}
플레이어를 익명 로그인에서 Google 계정 로그인으로 업데이트
익명 인증을 설정한 후, 플레이어가 Google 계정을 생성하고 Google을 통해 로그인하도록 업그레이드하려는 경우, 게임이 플레이어에게 Google 로그인 창을 표시하고 Google에서 ID 토큰을 가져와야 합니다. 그런 다음 LinkWithGoogleAsync
API를 호출해 플레이어를 Google ID 토큰에 연결합니다.
SDK에 캐시된 플레이어가 존재하는 경우, 캐시된 플레이어를 Google 계정에 연결할 수 있습니다.
SignInAnonymouslyAsync
를 사용해 캐시된 플레이어의 계정에 로그인합니다.LinkWithGoogleAsync
를 사용해 캐시된 플레이어의 계정을 Google 계정에 연결합니다.
캐시된 플레이어에 대한 자세한 내용은 캐시된 사용자 로그인 섹션을 참고하십시오.
async Task LinkWithGoogleAsync(string idToken)
{
try
{
await AuthenticationService.Instance.LinkWithGoogleAsync(idToken);
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);
}
}
Google 계정 연결 해제
플레이어가 Google 계정 연결을 해제할 수 있도록 UnlinkGoogleAsync
API를 사용합니다. 연결이 해제되면 계정이 다른 ID에 연결되지 않은 경우 익명 계정으로 전환됩니다.
async Task UnlinkGoogleAsync(string idToken)
{
try
{
await AuthenticationService.Instance.UnlinkGoogleAsync(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);
}
}