2017. 10. 30. 15:23

안녕하세요. 게임 개발자 놀이터입니다.


이번 포스팅은 구글 플레이 게임 서비스(Google Play Game Services)(이하 GPG)를 유니티를 이용해 연동하는 방법을 적어보려고합니다.


감사합니다.




구글 플레이 서비스 ( Google Play Services ) Unity 연동


구글 플레이 서비스 for Unity 는 아래와 같은 기능을 제공합니다.

  • 플러그인이 Google Play Games API 중 지원하는 기능
  • 로그인
  • 업적 잠금 해제, 발견, 증가
  • 리더보드에 점수 등록
  • 클라우드 세이브 읽기/쓰기
  • 내장 업적/리더보드 UI
  • 이벤트 및 퀘스트
  • 턴제 멀티플레이
  • 실시간 멀티플레이


1. SDK 다운로드

SDK 다운로드 : https://github.com/playgameservices/play-games-plugin-for-unity


Current-build 안에 있는 Unity Packages 를 프로젝트에 추가 합니다.




다음과 같이 프로젝트에 추가 되었다면 성공입니다!



 

2. 어플리케이션 등록 & 안드로이드 셋업


구글 플레이 서비스를 이용하려면 구글 개발자 등록이 되어있어야합니다.

그럼 구글 개발자 등록이 되어있다고 가정하에 아래 개발자 사이트를 들어갑니다.


https://play.google.com/apps/publish/


오른쪽 애플리케이션 만들기를 선택해, 애플리케이션을 만들어줍니다

(유니티에서 기본으로 빌드한 APK를 넣어주면 됩니다. )



설정을 끝낸후, 게임 서비스 탭으로 이동하여, 마찬가지로 새 게임 추가를 눌러서 추가해줍니다.



추가가 완료되었다면, 클릭해서 들어가 봅시다.




연결된 앱을 클릭해서 들어가줍니다!




패키지 이름을 클릭해 만든 게임과 연동시키고, 턴방식 멀티플레이나 실시간 멀티 플레이라면 알맞게 사용을 체크해줍니다!



그리고 돌아와 오른쪽 메뉴에서 업적이나 리더보드등 여러가지 추가하고 싶은것들을 알맞게 추가해줍니다~




그후 밑에 보이는 리소스 받기 버튼을 눌러서 나오는 XML 내용을 복사해둡시다!




자, 이제 다시 유니티로 돌아갑시다.


Window - Google Play Games - Setup - Android setup... 을 눌러줍시다.




그럼 아래와 같은 창이 뜰겁니다. 먼저 가운데 Resources Definition 에 아까 복사한 XML리소스를 붙어넣어 줍니다.

그리고 Constants class name은 이걸로 생성될 클래스의 위치를 잡아줍니다. 저같은 경우 GPGTest.Test 라고 해줬었습니다.


그후 중요한건 맨밑에 ClinetID인데, 이건 다시 구글 개발자 콘솔로 돌아갑니다.




게임 서비스 - 연결된 앱  으로 들어가 연결한 앱을 누르면 맨밑에 정보가 나옵니다.




이중 OAuth2 클라이언트 ID를 복사해서 위 Client ID에 붙어 넣어주고 Setup을 누릅니다.


그럼 확인창 2개가 뜰텐데, 그 중



와 같이 뜬다면 완벽합니다!



3. 구글 로그인 및 업적, 리더보드 올리기




초기화

구글 플레이게임 서비스를 초기화합니다.

//Activate the Google Play gaems platform

PlayGamesPlatform.Activate();


로그인

구글 서비스에 로그인합니다.

Social.localUser.Authenticate((bool success) => {

//handle success or failure

});


업적 달성

업적은 0~100까지 달성도로 나타냅니다.

// unlock achievement (achievement ID "Cfjewijawiu_QA")

Social.ReportProgress("Cfjewijawiu_QA", 100.0f, (bool success) => {

//handle success or failure

});


리더보드에 점수 게시

리더보드에 점수를 게시합니다.

//post score 12345 to leaderboard ID "Cfji293fjsie_QA")

social.ReportScore(12345, "Cfji293fjsie_QA", (bool success) =>{

//handle success or failure

});


업적 UI 보여주기

모든 업적이 보이는 UI창을 띄웁니다.

//show achievements UI

Social.ShowAchievementsUI();


리더보드 UI 보여주기

리더보드 UI창을 띄웁니다.

        // show leaderboard UI

Social.ShowLeaderboardUI();


게임 상태를 클라우드에 저장하기

클라우드의 게임을 저장합니다.

         public class MyClass : OnStateLoadedListener {

void SaveState() {

// serialize your game state to a byte array:

byte[] mySaveState = ...;

int slot = 0; // slot number to use

((PlayGamesPlatform) Social.Active).UpdateState(slot,

mySaveState, this);

}

public void OnStateSaved(bool success, int slot) {

// handle success or failure

}

}


게임 상태를 클라우드로부터 불러오기

클라우드에서 게임을 불러옵니다.

        public class MyClass : OnStateLoadedListener {

void LoadState() {

int slot = 0; // slot number to use

((PlayGamesPlatform) Social.Active).LoadState(slot, this);

}

public void OnStateLoaded(bool success, int slot, byte[] data) {

if(success) {

//do something with data[]

} else {

//handle failure

}

}

}


클라우드 충돌 해결

2개 이상의 기기에서 클라우드를 나눠쓴다면 충돌이 날 수 있습니다. 

public class MyClass : OnStateLoadedListener {

public byte[] OnStateConflict(int slot, byte[] local, byte[] server){

//resolve conflict and return a byte[] representing the

//resolved state.

}

}



4. 실시간 & 턴기반 멀티플레이 구현하기

실시간 멀티 플레이 – 빠른 게임 만들기

특정수의 자동 상대와 빠른 매칭을 시도합니다.

const int MinOpponents = 1 MaxOpponents = 3;

const int GameVariant = 0;

PlayGamesPlatform.Instance.RealTime.CreateQuickGame (MinOpponents, MaxOpponents,

GameVariant, listener);


실시간 멀티 플레이 – 초대 화면

원하는 친구를 선택할수있는 초대화면을 표시합니다.

const int MinOpponents = 1 MaxOpponents = 3;

const int GameVariant = 0;

PlayGamesPlatform.Instance.RealTime.CreateWithInvitationScreen (MinOpponents, MaxOpponents,

GameVariant, listener);


실시간 멀티 플레이 – 초대장 표시하기

사용자가 받은 초대를 표시합니다.

PlayGamesPlatform.Instance.RealTime.AcceptFromInbox (listener);


실시간 멀티 플레이 – 초대 엑세스

초대 수락후 초대장에 엑세스합니다

PlayGamesPlatform.Instance.RealTime.GetInvitation ();


실시간 멀티 플레이 – 초대 수락

초대를 수락합니다.

Invitation invitation = ....;  // (obtained via delegate)

    PlayGamesPlatform.Instance.RealTime.AcceptInvitation(invitationId, invitation.InvitationId);


실시간 멀티 플레이 – 모든 초대장 표시하기

받은 모든 초대장을 표시합니다.

PlayGamesPlatform.Instance.RealTime.GetAllInvitations(

        (invites) =>

        {

            Debug.Log("Got " + invites.Length + " invites");

            string logMessage = "";

            foreach(Invitation invite in invites)

            {

                logMessage += " " + invite.InvitationId + " (" +

                    invite.InvitationType + ") from " +

                    invite.Inviter + "\n";

                if (mFirstInvite == null) {

                    mFirstInvite = invite;

                }

            }

            Debug.Log(logMessage);

    });


실시간 멀티 플레이 – 연결 대기

연결 상황에 따라 대기실을 표시해줍니다.

private bool showingWaitingRoom = false;

    public void OnRoomSetupProgress(float progress) {

        // show the default waiting room.

        if (!showingWaitingRoom) {

            showingWaitingRoom = true;

            PlayGamesPlatform.Instance.RealTime.ShowWaitingRoomUI();

        }

    }


실시간 멀티 플레이 – 매칭 완료

매칭이 완료되었을때 불리는 함수입니다.

public void OnRoomConnected(bool success) {

        if (success) {

            // Successfully connected to room!

            // ...start playing game...

        } else {

            // Error!

            // ...show error message to user...

        }

    }



실시간 멀티 플레이 – 참가자 인덱스

멀티에 참가한 플레이어들은 index로 구분됩니다.

Participant myself = PlayGamesPlatform.Instance.RealTime.GetSelf();

    Debug.Log("My participant ID is " + myself.ParticipantId);



실시간 멀티 플레이 – 참가자 목록

다른 참가자들을 인덱스로 받아옵니다.

List<Participant> participants = PlayGamesPlatform.Instance.RealTime.GetConnectedParticipants();



실시간 멀티 플레이 – 메세지 보내기

다른 모든 참가자들에게 메세지를 보낼수있습니다.

Reliable은 신뢰성의 여부를 따집니다.

byte [] message = ....; // 메시지를 작성하십시오. 

    bool  reliable = true ;

    PlayGamesPlatform.Instance.RealTime.SendMessageToAll (reliable, message);



실시간 멀티 플레이 – 메세지 받기

메세지가 도착했을때 불리는 함수입니다.

public void OnRealTimeMessageReceived(bool isReliable, string senderId, byte[] data) {

        // handle message! (e.g. update player's avatar)

    }



실시간 멀티 플레이 – 연결 이벤트 처리

유저가 게임을 나갔을때

public  void  OnLeftRoom () {

         // 오류 메시지를 표시하고 메뉴 화면으로 돌아갑니다.

        

        // (PlayGamesPlatform.Instance.RealTime.LeaveRoom ()을 여기에서 호출하지 마십시오.) 

        // 이미 방을 나왔습니다 !) 

    }


다른 유저가 방에 들어오거나 나갔을때

public  void  OnPeersConnected ( string [] participantIds ) {

         // 적절하게 반응합니다 (예 : 새 아바타를 게임에 추가).

    }

    

    public  void  OnPeersDisconnected ( string [] participantIds ) {

         // 적절하게 반응 (예 : 게임에서 아바타 제거) 

    }


실시간 멀티 플레이 – 방 나가기

방에서 나옵니다.

PlayGamesPlatform.Instance.RealTime.LeaveRoom ();




턴 멀티 플레이 – 빠른 게임

퀵 매치를 시작합니다.

PlayGamesPlatform.Instance.TurnBased.CreateQuickMatch(MinOpponents, MaxOpponents,

        Variant, OnMatchStarted);


    // Callback:

    void OnMatchStarted(bool success, TurnBasedMatch match) {

        if (success) {

            // go to the game screen and play!

        } else {

            // show error message

        }

    }


턴 멀티 플레이 – 초대 화면

초대가 가능한 화면으로 만듭니다.

PlayGamesPlatform.Instance.TurnBased.CreateWithInvitationScreen(MinOpponents, MaxOpponents,

        Variant, OnMatchStarted);


턴 멀티 플레이 – 초대보기

받은 초대를 봅니다.

PlayGamesPlatform.Instance.TurnBased.AcceptFromInbox(OnMatchStarted);


턴 멀티 플레이 – 초대 수락

받은 초대를 수락합니다.

PlayGamesPlatform.Instance.TurnBased.AcceptFromInbox(OnMatchStarted);


턴 멀티 플레이 – 모든 초대장 보기

받은 모든 초대장을 표시합니다.

PlayGamesPlatform.Instance.TurnBased.GetAllInvitations(

        (invites) =>

        {

            Debug.Log("Got " + invites.Length + " invites");

            string logMessage = "";

            foreach(Invitation invite in invites)

            {

                logMessage += " " + invite.InvitationId + " (" +

                    invite.InvitationType + ") from " +

                    invite.Inviter + "\n";

                if (mFirstInvite == null) {

                    mFirstInvite = invite;

                }

            }

            Debug.Log(logMessage);

    });


턴 멀티 플레이 – 매치 완료

매치가 완료되었다는 콜백을 받습니다.

void OnMatchStarted(bool success, TurnBasedMatch match) {

        if (success) {

            // get the match data

            byte[] myData = match.Data;

            

            // I can only make a move if the match is active and it's my turn!

            bool canPlay = (match.Status == TurnBasedMatch.MatchStatus.Active &&

                match.TurnStatus == TurnBasedMatch.MatchTurnStatus.MyTurn);

            

            // Deserialize game state from myData into scene and

            // go to gameplay screen. If canPlay == true, let user play a move; 

            // if not, they can only see the current state of the game but can't play.

        } else {

            // show error message, return to main menu

        }

    }


턴 멀티 플레이 – 내 차례 알림

내 차례를 알려주는 콜백을 받습니다.

TurnBasedMatch match = ....; // received in OnMatchStarted

    

    // your representation of the new state of the game after the player

    // has taken their turn:

    byte[] myData = .....;

    

    // this indicates whose turn is next (a participant ID)

    string whoIsNext = .....;

    

    PlayGamesPlatform.Instance.TurnBased.TakeTurn(match.MatchId, myData, whoIsNext, (bool success) => {

        if (success) {

            // turn successfully submitted!

        } else {

            // show error

        }

    });


턴 멀티 플레이 – 턴 넘기기

자동으로 다음 플레이어에게 넘기거나, 특정 플레이어에게 턴을 넘깁니다.

string DecideWhoIsNext(TurnBasedMatch match) {

        if (match.AvailableAutomatchSlots > 0) {

            // hand over to an automatch player

            return null;

        } else {

            // pick a player among match.Participants,

            // according to the game's logic

        }

    }


턴 멀티 플레이 – 경기 끝

경기가 끝낫을떄 받는 함수입니다.

TurnBasedMatch match = .....;  // our current match

    byte[] finalData = .....; // match data representing the final state of the match

    

    // define the match's outcome

    MatchOutcome outcome = new MatchOutcome();

    foreach (Participant p in match.Participants) {

        // decide if participant p has won, lost or tied, and

        // their ranking (1st, 2nd, 3rd, ...):

        MatchOutcome.ParticipantResult result = .....;

        int placement = ......;

        

        outcome.SetParticipantResult(p.ParticipantId, result, placement);

    }

   

    // finish the match

    PlayGamesPlatform.Instance.TurnBased.Finish(match.MatchId, finalData, outcome, (bool success) => {

        if (success) {

            // sent successfully

        } else {

            // an error occurred

        }

    });


턴 멀티 플레이 – 게임 나가기

필요하다면 아래 함수로 게임을 나갈수 있습니다.

PlayGamesPlatform.Instance.TurnBased.Leave(match.MatchId, (bool success) => {

        if (success) {

            // successfully left

        } else {

            // error leaving match

    });




감사합니다!!

Posted by 시리시안