유니티3D 프로그래밍

GPGS 구글플레이 앱 테스트 (21.06.23) 본문

Google App/수업내용

GPGS 구글플레이 앱 테스트 (21.06.23)

tjdgus9955 2021. 6. 23. 11:29

App.cs

로그인과 로그인 한 유저의 간단한 정보를 불러온다.

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

public class App : MonoBehaviour
{
    public enum ePlayMode
    {
        TEST, BUILD
    }
    public Text test;
    public Text txtid;
    public Text txtUserName;
    public Text txtState;
    public Image thumb;
    public Button btnStart;

    // Start is called before the first frame update
    void Start()
    {
        this.test.text = Application.version;
        //this.btnStart.gameObject.SetActive(false);
        //this.btnStart.onClick.AddListener(() =>
        //{
        //    //씬전환
        //    SceneManager.LoadSceneAsync("GameScene").completed += (oper) =>
        //    {
        //        var gameMain = GameObject.FindObjectOfType<GameMain>();
        //    };
        //});
        Debug.Log("-----------------> lnit GPGS");

        PlayGamesClientConfiguration config = new PlayGamesClientConfiguration.Builder()
            .Build();
        PlayGamesPlatform.InitializeInstance(config);
        PlayGamesPlatform.DebugLogEnabled = true;
        PlayGamesPlatform.Activate();
        Debug.Log("----------------->Authenticate");

        // authenticate user:
        PlayGamesPlatform.Instance.Authenticate(SignInInteractivity.CanPromptAlways, (result) => {
            // handle results


            Debug.Log("----------------->" + result);
            Debug.Log("----------------->" + Social.localUser);
            Debug.Log("----------------->" + Social.localUser.authenticated);

            this.txtid.text = Social.localUser.id;
            this.txtUserName.text = Social.localUser.userName;
            this.txtState.text = Social.localUser.state.ToString();

            Debug.Log("----------------->" + Social.localUser.image);

            StartCoroutine(this.WaitForLoadThumb(() => {
                Debug.Log(Social.localUser.image);
                Debug.LogFormat("{0}, {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();
            }));

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

앱 실행 전

앱 실행 후

앱이 구글 플레이 콘솔과 연결 되면서 로그인, 로그인 한 유저의 이름, 유저의 id 값을 보여준다.

 

App.cs

접속시 로그인 하고 버튼을 생성해서 버튼 클릭시 씬 전환이 발생한 뒤 그 씬의 버튼을 클릭하면 리더보드를 보여준다.

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

public class App : MonoBehaviour
{
    public enum ePlayMode
    {
        TEST, BUILD
    }
    public Text test;
    public Text txtid;
    public Text txtUserName;
    public Text txtState;
    public Image thumb;
    public Button btnStart;
    public ePlayMode playMode;

    // Start is called before the first frame update
    void Start()
    {
        this.test.text = Application.version;
        if (playMode == ePlayMode.TEST)
        {
            this.btnStart.gameObject.SetActive(true);
        }
        else if (playMode == ePlayMode.BUILD)
        {
            this.btnStart.gameObject.SetActive(false);
        }


        this.btnStart.onClick.AddListener(() =>
        {
            SceneManager.LoadSceneAsync("GameScene").completed += (oper) =>
            {
                var gameMain = GameObject.FindObjectOfType<GameMain>();
                gameMain.Init(Social.localUser.userName);
            };


        });
        Debug.Log("-----------------> lnit GPGS");

        PlayGamesClientConfiguration config = new PlayGamesClientConfiguration.Builder()
            .Build();
        PlayGamesPlatform.InitializeInstance(config);
        PlayGamesPlatform.DebugLogEnabled = true;
        PlayGamesPlatform.Activate();
        Debug.Log("----------------->Authenticate");

        // authenticate user:
        PlayGamesPlatform.Instance.Authenticate(SignInInteractivity.CanPromptAlways, (result) => {
            // handle results


            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();

                Debug.Log("----------------->" + Social.localUser.image);

                StartCoroutine(this.WaitForLoadThumb(() => {
                    Debug.Log(Social.localUser.image);
                    Debug.LogFormat("{0}, {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();
                }));

                this.btnStart.gameObject.SetActive(true);
            }

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

 

Immortal.cs

App.cs를 계속 유지할 수 있게 하는 스크립트

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Immortal : MonoBehaviour
{
    private void Awake()
    {
        DontDestroyOnLoad(this.gameObject);
    }
}

GameMain.cs

버튼 클릭 시 리더보드를 보여준다.

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

public class GameMain : MonoBehaviour
{
    public Text txtUserName;
    public Button btnAchievement;
    public Button btnReaderBoard;

    public void Init(string userName)
    {
        Debug.Log("userName:" + userName);

        this.txtUserName.text = userName;

        this.btnAchievement.onClick.AddListener(() =>
        {
            PlayGamesPlatform.Instance.ShowAchievementsUI();
        });

        this.btnReaderBoard.onClick.AddListener(() =>
        {
            PlayGamesPlatform.Instance.ShowLeaderboardUI();
        });

        PlayGamesPlatform.Instance.IncrementAchievement(
        GPGSIds.achievement, 1, (bool success) =>
        {
           // handle success or failure
           Debug.LogFormat("IncrementAchievement : {0}. {1}", GPGSIds.achievement, success);
        });

    }

}