GPGS(Google Play Game Service) 연동

2020. 8. 12. 18:23Unity/수업내용

유니티 빌드 환경 셋팅

1. GitHub에서 연동 플러그인 설치

https://github.com/playgameservices/play-games-plugin-for-unity

 

playgameservices/play-games-plugin-for-unity

Google Play Games plugin for Unity. Contribute to playgameservices/play-games-plugin-for-unity development by creating an account on GitHub.

github.com

위 링크에서 받은 후 current-build 내에 있는 언패키지 파일을 프로젝트에 임포트하면 된다.

 

2. GPGS 라이브러리 설치

<GPGS 라이브러리 설치 방법>

- 위 과정이 정상적으로 진행되었다면, 아래의 그림과 같이 총 3가지의 항목이 생겨난다.

 1) GooglePlayGames(Folder)

 2) Plugins(Folder)

 3) GPGSIds(Script)

 

<정상 설치 시 Project 창 모습>

3. google console에서 각종 키 값 받아오기

https://developer.thebackend.io/unity3d/guide/federationEx/gpgs/

 

뒤끝 개발자

모바일 게임 서버를 쉽게 생성, 관리 할 수 있는 뒤끝의 개발자 사이트입니다.

developer.thebackend.io

위의 포스팅을 참고하면 된다. 추후 옮겨적을 예정

 

4. 스크립트 작성

<App.cs>

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 GPGSManager instance;
    public Button btnSignIn;
    public Button btnSignOut;
    public Button btnGetProfile;
    public Image imgProfile;
    public Text txtProfile;
    void Start()
    {
        this.instance = GPGSManager.GetInstance();
        this.instance.Init();

        this.btnSignIn.onClick.AddListener(() =>
        {
            this.instance.SignIn((onComplete) =>
            {
                if (onComplete == SignInStatus.Success)
                    Debug.Log("Sign In Success!!");
                else
                    Debug.Log("Sign In Fail!!");
            });
        });

        this.btnSignOut.onClick.AddListener(() =>
        {
            this.instance.SignOut();
            Debug.Log("Sign Out Success!!");
        });
        this.btnGetProfile.onClick.AddListener(() =>
        {
            this.instance.GetUserProfile((profileData) =>
            {
                Debug.LogFormat($"Log//ProfileName : {profileData.userName}, ProfileImage : {profileData.image}");
                Rect rect = new Rect(0, 0, profileData.image.width, profileData.image.height);
                this.imgProfile.sprite = Sprite.Create(profileData.image, rect, new Vector2(0.5f, 0.5f));
                this.txtProfile.text = profileData.userName;
            });
        });
    }
}

 

<GPGSManager.cs>

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

public class GPGSManager
{
    private static GPGSManager instance;

    public static GPGSManager GetInstance()
    {
        if (instance == null)
            instance = new GPGSManager();
        return instance;
    }
    public void Init()
    {
        PlayGamesClientConfiguration conf = new PlayGamesClientConfiguration.Builder().Build();

        PlayGamesPlatform.InitializeInstance(conf);
        PlayGamesPlatform.DebugLogEnabled = true;
        PlayGamesPlatform.Activate();
    }
    public void SignIn(System.Action<SignInStatus> onComplete)
    {
        PlayGamesPlatform.Instance.Authenticate(SignInInteractivity.CanPromptOnce, (result) =>
        {
            onComplete(result);
        });
    }
    public void SignOut()
    {
        PlayGamesPlatform.Instance.SignOut();
    }

    public class UserProfile
    {
        public string userName;
        public Texture2D image;
        public UserProfile(string userName, Texture2D image)
        {
            this.userName = userName;
            this.image = image;
        }
    }

    public void GetUserProfile(System.Action<UserProfile> onComplete)
    {
        Debug.Log(Social.localUser.authenticated);
        Debug.LogFormat("userName: {0}, image: {1}", Social.localUser.userName, Social.localUser.image);
        PlayGamesHelperObject.RunCoroutine(LoadImage(() =>
        {
            var profile = new UserProfile(Social.localUser.userName, Social.localUser.image);
            Debug.Log("profile: " + profile.userName + " , " + profile.image);
            onComplete(profile);
        }));
    }

    private IEnumerator LoadImage(System.Action onComplete)
    {
        Debug.Log("LoadImage");
        yield return new WaitUntil(() => Social.localUser.image != null);
        Debug.Log("image loaded: " + Social.localUser.image);
        onComplete();
    }
}

 

'Unity > 수업내용' 카테고리의 다른 글

UGUI / NGUI / AssetBundle / CDN 개념 요약  (0) 2020.08.06
특정 GameObject의 하위 GameObject / Transform 검색하기  (0) 2020.08.06
MLAgents - RollerBall  (0) 2020.07.14
Coroutine  (0) 2020.05.29
쿠키런 점프 Image  (0) 2020.05.28