유니티 엔진/유니티 엔진의 구성

유니티 엔진의 구성 - 46) 유니티 인증(Unity Authentication)

monstro 2026. 6. 1. 18:48
728x90
반응형

- 개요

Unity Authentication유니티 게이밍 서비스(UGS, Unity Gaming Services)에서 제공하는 기능으로, 

게임 플레이어의 신원을 식별하고 고유한 ID를 부여하는 사용자 인증 시스템이다

복잡하게 서버를 구축할 필요없이, 게임 내에 간편하게 로그인 기능을 구현할 수 있다

 

1) 프로젝트에서 사용할 Unity Authentication 생성

Unity Dashboard - Player Authentication

 

Unity Dashboard에 접속하고 Player Authentication을 실행하면 프로젝트에서 사용할 수 있다

 

 

이어서 Add Identity Provider 옵션을 선택하여 인증 방식을 결정해야 한다

 

 

인증을 받게 될 클라이언트의 이름인증이 진행될 플랫폼을 설정하고

개인정보 처리 방침이용약관을 설정하면 인증 방식이 결정된다

 

최종 완성된 Unity Authtentication

 

위와 같이 유니티 인증을 위한 ID Provider가 생성된 것을 확인할 수 있다

 

2) 프로젝트와 생성한 Unity Authentication 연동

2 - 1) 프로젝트 연결 및 패키지 설치

에디터 상단 Project Settings - Services 탭

 

진행중인 프로젝트에 대한 Organization Project ID를 생성한다

 

패키지 설치

 

Authentication 패키지를 프로젝트에 설치한다

 

2 - 2) 스크립트 작성

public class Test : MonoBehaviour
{
    private async void Start()
    { 
        await UnityServices.InitializeAsync();
        Debug.Log(UnityServices.State);

        SetupEvents();

        await SignUpAnonymouslyAsync();
    }

    void SetupEvents()
    {
        AuthenticationService.Instance.SignedIn += () => {
            Debug.Log($"PlayerID: {AuthenticationService.Instance.PlayerId}");
            Debug.Log($"Access Token: {AuthenticationService.Instance.AccessToken}");

        };

        AuthenticationService.Instance.SignInFailed += (err) => {
            Debug.LogError(err);
        };

        AuthenticationService.Instance.SignedOut += () => {
            Debug.Log("Player signed out.");
        };

        AuthenticationService.Instance.Expired += () =>
        {
            Debug.Log("Player session could not be refreshed and expired.");
        };
    }

    async Task SignUpAnonymouslyAsync()
    {
        try
        {
            await AuthenticationService.Instance.SignInAnonymouslyAsync();
            Debug.Log("Sign in anonymously succeeded!");
            Debug.Log($"PlayerID: {AuthenticationService.Instance.PlayerId}");
        }
        catch (AuthenticationException ex)
        {
            Debug.LogException(ex);
        }
        catch (RequestFailedException ex)
        {
            Debug.LogException(ex);
        }
    }
}

 

위와 같이 유니티 공식문서를 활용하여 테스트 코드를 구성하였다

테스트 코드는 유니티 게이밍 서비스(UGS)와 연동된 '고유 계정'을 생성하고 로그인시킨다

  • Start 함수 : 유니티 서비스 초기화 + 이벤트 등록 + 익명 로그인을 순차적으로 처리
  • SetupEvents 함수 : 인증 상태 변화를 감지하는 이벤트 리스너들을 등록
  • SignUpAnonymouslyAsync 함수 : Unity Authentication로그인을 요청하고 처리

 

- 최종 실행 결과

 

최종 실행된 결과는 위와 같다

1)에서 생성한 프로젝트에 할당된 ID Provider를 통해 로그인이 처리된 것을 확인할 수 있다

 

728x90
반응형