소셜 로그인/수업내용

Unity 수업내용 21.06.22. GPGS

HappyFrog 2021. 6. 22. 11:12

GPGS로그인에서는 유니티로부터 Social클래스를 이용하여 유저의 프로필정보를 불러올 수 있다. 그러므로 로그인하는 유저의 Profile에서 id를 가져와서 해당 id를 DB에 저장할 수 있다.

 

 우선 GPGS에 연결을 하며 인증에 성공했다면(Social.localUser.authenticated == true)미리 만들어둔 UI들에 해당 유저의 프로필정보가 대입될 수 있다록 지정한다. 그리고 프로필의 썸네일같은 경우는 늦게 불러와지는 경우가 있으니 코루팀으로 성공할때까지 기다린 뒤 불러오는데 성공하면 출력하는 것으로 한다.

눈여겨 봐야할 점은 localUser의 image는 Texture2D로 전달받기 때문에 이를 Sprite로 바꾸기 위해서는 Sprite.Create메서드를 이용해야한다. 

 

 

 

Unity - Scripting API: Sprite.Create

Sprite.Create creates a new Sprite which can be used in game applications. A texture needs to be loaded and assigned to Create in order to control how the new Sprite will look. In the script example below a new Sprite is displayed when the button is presse

docs.unity3d.com

Sprite.Create에 대한 내용이 담긴 링크

 

위 사진은 Sprite.Create의 정의이며 나는 아래에서 2번째 함수를 사용했다. 매개변수로는 (변환시킬 Texture2D, Sprite의 크기(width * height), Sprite의 pivot위치)를 할당해주면 Texture2D가 정상적으로 Sprite로 변환되는 모습을 확인할 수 있다.

 

서술했듯이 이미지가 바로 로드되지 않을 수 있기 때문에 코루틴으로 이미지를 가져올 때까지 기다려준다. 여기서 이미지를 가져오는 순간 callback()이 실행되는데, 이는 StartCoroutine부분에서 보면 알 수 있듯이 매개변수를 람다식으로 내용을 선언하여 콜백메서드화 시킨 모습이다. 그렇기 때문에 대리자를 매개변수로 써야했고, 따라서 매개변수의 형식이 System.Action인 것이다.

 

로딩되는 image의 크기

로그로 출력해보니 이미지의 크기는 96*96으로 나온다.


스크립트

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using GooglePlayGames;
using GooglePlayGames.BasicApi;
using UnityEngine.SocialPlatforms;

public class App : MonoBehaviour
{
    public Text version;
    public Text txtId;
    public Text txtUserName;
    public Text txtState;
    public Image thumb; //아바타 이미지

    void Start()
    {
        this.version.text = Application.version;


        Debug.Log("==================== Init GPGS");

        PlayGamesClientConfiguration config = new PlayGamesClientConfiguration.Builder()
            .Build();

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

        Debug.Log("==================== Authenticate");

        PlayGamesPlatform.Instance.Authenticate(SignInInteractivity.CanPromptAlways, (result) =>
        {
            Debug.Log("====================" + result);
            Debug.Log("====================" + Social.localUser);
            Debug.Log("====================" + Social.localUser.authenticated);

            if (Social.localUser.authenticated)
            {
                this.txtId.text = Social.localUser.id;
                this.txtUserName.text = Social.localUser.userName;
                this.txtState.text = Social.localUser.state.ToString();


                StartCoroutine(WaitForLoadThumb(() =>
                {
                    //이미지 있음
                    Debug.Log("Loaded Image" + Social.localUser.image);

                    Debug.LogFormat("{0} x {1}", Social.localUser.image.width, Social.localUser.image.height);
                    this.thumb.sprite = Sprite.Create(Social.localUser.image, new Rect(0, 0, Social.localUser.image.width, Social.localUser.image.height), Vector2.zero);
                    this.thumb.SetNativeSize();
                }));
            }            
        });
    }

    IEnumerator WaitForLoadThumb(System.Action callback)
    {
        while (true)
        {
            if(Social.localUser.image != null)
            {
                break;
            }
            yield return null;
        }
        callback();
    }
}

 

* 주의  

"PlayGamesClientConfiguration config = new PlayGamesClientConfiguration.Builder().Build()"부분을 보면 Build만 하고있는데, 이 전까지는 EnableSavedGame같이 게임저장이 활성화된 상태였는데, 이 부분은 GooglePlayConsole에서 해당 앱 -> Play Game Service -> 설정 -> 속성수정 으로 가면 확인할 수 있는 상태와 동일해야 한다.

 

나는 EnableSavedGame이 없기때문에 저장된 게임을 사용하지 않는다. 만약 사용할 것이라면 해당 코드를 스크립트에 추가한 뒤, Google Play Console에서도 설정을 바꿔주면 된다.

 

나는 다른 방법으로 저장을 할 예정이라 하지 않는다.