본문 바로가기

관리 메뉴

[Unity/GPGS] 구글 리더보드 UI에 띄우기 Google play leaderboard with custom UI 본문

프로그래밍/ㄴ기타

[Unity/GPGS] 구글 리더보드 UI에 띄우기 Google play leaderboard with custom UI

최갓 2020. 9. 2. 15:12
반응형

구글플레이 게임 서비스의 리더보드 정보를 가져와 

커스텀 UI에 띄우는 작업을 하게되었다.

 

해당 기능 구현에 대한 글이다.

 

주의할점은 GPGS 리더보드에서 가져오는 user profile의 profile image와 username은 비동기로 

처리된다는 부분이다.

 

주의

한번에 요청할 수 있는 리더보드 유저 정보 최대 수는 25~30이다. 이 이상의 수를 요청할 시 에러가 발생된다

 

사용 유니티 버전 : 2019.3.15f

에셋 : NGUI

구글 플러그인 패키지 : GooglePlayGamesPlugin-0.10.10

 

 

랭킹 요청 진입점

public void LoadSocres(EventDelegate vFinishDelegate)
    {        
        m_iProfileLoadCount = 0; //프로필 로드 완료 체크를 위한 카운트 (int)
        a_bLoadCanceld = false; //로딩 중 캔슬 여부 식별
        m_vRankingLoadFinishDelegate = vFinishDelegate; //랭킹 로드 완료시 호출할 delegate

        PlayGamesPlatform.Instance.LoadScores(
            "key",
            LeaderboardStart.TopScores,
            10,
            LeaderboardCollection.Public,
            LeaderboardTimeSpan.AllTime,
            GPGSUserInfoCallback);
    }

 

LoadScores 콜백 메소드

private void GPGSUserInfoCallback(LeaderboardScoreData data)
    {
        List<string> userIds = new List<string>();
        List<G_UserRankingData> vRankingList = new List<G_UserRankingData>();
        G_UserRankingData vPlayerInfo = new G_UserRankingData(); //유저 랭킹데이터를 담아 전달하기 위한 단순 데이터 보관용 클래스
        
        foreach (IScore vData in data.Scores)
        {
            userIds.Clear();
            userIds.Add(vData.userID);

            G_UserRankingData vUser = new G_UserRankingData();
            vUser.iRank = vData.rank;
            vUser.iScore = vData.value;
            vUser.strFormattedScore = vData.formattedValue;

            m_iProfileLoadCount += 1; //프로필 요청을 보냈으니 카운트+1
            Social.LoadUsers(userIds.ToArray(), profiles => {
                StartCoroutine(WaitUserProfile(profiles, vUser));
            });
            vRankingList.Add(vUser);
        }

        vPlayerInfo.iRank = data.PlayerScore.rank;
        vPlayerInfo.iScore = data.PlayerScore.value;
        vPlayerInfo.strFormattedScore = data.PlayerScore.formattedValue;

        m_iProfileLoadCount += 1; //프로필 요청을 보냈으니 카운트 +1
        Social.LoadUsers(userIds.ToArray(), profiles => {
            StartCoroutine(WaitUserProfile(profiles, vPlayerInfo));
        });

        StartCoroutine(WaitUserProfileLoadFinish(vPlayerInfo, vRankingList));
    }

 

유저 프로파일 데이터 받아오기는 처리 메소드

 IEnumerator WaitUserProfile(UnityEngine.SocialPlatforms.IUserProfile[] profiles, G_UserRankingData vUser)
    {
        int i = 0;
        Texture2D texture = null;
        for (i = 0; i < profiles.Length; i++)
            texture = profiles[i].image;

        yield return new WaitUntil(() =>
        {
            int count = 0;
            for (i = 0; i < profiles.Length; i++)
                if (profiles[i].image == null || profiles[i].userName == string.Empty || profiles[i].userName == "")
                    count++;
         
            return count == 0;
        });

        // build ranking here
        vUser.strName = profiles[0].userName;
        vUser.vProfile = profiles[0].image;
        Debug.Log("User Name : " + vUser.strName + ", Profile : " + vUser.vProfile);

        m_iProfileLoadCount -= 1; //데이터 받아왔으니 카운트 -1. 로드 실패 등도 일단 여기로 온다
    }

 

최종 랭킹/프로필 로드가 끝났을 때 처리하는 부분

   private IEnumerator WaitUserProfileLoadFinish(G_UserRankingData vPlayerInfo, List<G_UserRankingData> vRankingList)
    {
        while (m_iProfileLoadCount > 0) //로드할게 남았으면 대기
        {
            //대기 중 캔슬 요청 들어왔으면 그냥 탈출
            yield return null;
        }

		//유저 및 랭킹 데이터 담아서 최종 전달.
        m_vRankingLoadFinishDelegate.parameters[0] = new EventDelegate.Parameter(vPlayerInfo);
        m_vRankingLoadFinishDelegate.parameters[1] = new EventDelegate.Parameter(vRankingList);
        m_vRankingLoadFinishDelegate.Execute();
    }

 

 

여기서 프로필 이미지는 Play Games의 그림아이콘(?) 이다.

 

핵심은 비동기로 처리되는 리더보드 데이터 로드, 유저 프로필 로드의 완료시점을 알고 연결시켜 주는 것이다

반응형
Comments