소셜 로그인/수업내용
Unity 수업내용 21.06.24 GPGS LeaderBoard
HappyFrog
2021. 6. 24. 11:48
GooglePlayConsole에서 Play 게임서비스 - 리더보드에 들어간다.
미리 만들어둔 리더보드가 나온다. 그렇다면 오른쪽 상단에 있는 리소스 보기를 눌러 코드를 복사 한 뒤에 Unity Editor로 돌아가서
Google Play Games - Setup - Android setup으로 들어가서 코드를 붙여넣어준다.
그리고 에디터에서 점수를 저장하고 해당 점수로 인한 최고기록을 확인하기 위한 UI들을 만들어보자.
이전 화면에서 점수획득, 그 위에있는 점수표시 Text, 그리고 우상단의 LeaderBoard를 생성했다.
+ 최고점수 표시버튼과 그를 서버에 Post해주는 버튼도 만들어준다.
리더보드에 최고점수가 공유되며 최고점수보다 낮은 점수를 Post할 때는 최고점수가 변경되지 않는다.
클라우드에 게임을 저장하는 기능은 이제 지원을 하지 않는다고 한다.
using GooglePlayGames;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.Linq;
using UnityEngine.SocialPlatforms;
public class GameMain : MonoBehaviour
{
public Text txtUserName;
public Button btnAchievement;
public Button btnAttack;
public Button btnPostScore;
public Button btnScore;
public Button btnLeaderBoard;
public GameObject achievementDoing;
public GameObject achievementDone;
public Text txtStep;
public Text txtCount;
public Text txtScore;
public Text txthighScore;
private int goalCount = 10;
private int totalSteps = 2;
private int currentCount = 0;
private int currentStep = 1;
string userName;
int score;
//초기화
public void Init(string userName)
{
userName = userName;
this.InitAchievement();
this.InitLeaderBoard();
}
public void InitLeaderBoard()
{
this.btnLeaderBoard.onClick.AddListener(() =>
{
PlayGamesPlatform.Instance.ShowLeaderboardUI();
});
this.btnScore.onClick.AddListener(() =>
{
this.score += 10;
this.txtScore.text = this.score.ToString();
});
this.btnPostScore.onClick.AddListener(() =>
{
//현재점수와 최고점수를 비교하여 갱신했다면 포스트
//this.txtHighScore.text =
Social.ReportScore(this.score, GPGSIds.leaderboard, (success) =>
{
Debug.Log(success);
this.score = 0;
this.txtScore.text = this.score.ToString();
});
});
Social.LoadScores(GPGSIds.leaderboard, scores => {
if (scores.Length > 0)
{
Debug.Log("Got " + scores.Length + " scores");
string myScores = "Leaderboard:\n";
foreach (IScore score in scores)
myScores += "\t" + score.userID + " " + score.formattedValue + " " + score.date + "\n";
Debug.Log(myScores);
Debug.Log("==============================================");
//내 점수 찾기
var foundByScores = scores.Where<IScore>(x => x.userID == Social.localUser.id);
foreach(var score in scores)
{
Debug.LogFormat("score : {0}", score);
}
}
else
Debug.Log("No scores loaded");
});
}
public void InitAchievement()
{
Debug.Log("userName: " + userName);
string stepKey = string.Format("{0}_step", GPGSIds.achievement_2);
//CgkIoJubiaIZEAIQAw_step
string currentCountKey = string.Format("{0}_currentCount", GPGSIds.achievement_2);
//CgkIoJubiaIZEAIQAw_currentCount
if (PlayerPrefs.GetInt(stepKey) == 0)
{
//처음 이 업적을 실행
PlayerPrefs.SetInt(stepKey, currentStep);
}
else
{
this.currentStep = PlayerPrefs.GetInt(stepKey);
}
if (PlayerPrefs.GetInt(currentCountKey) == 0)
{
//현재 스텝에서 이 업적 처음 실행하는것
}
else
{
this.currentCount = PlayerPrefs.GetInt(currentCountKey);
}
//UI업데이트
this.txtCount.text = string.Format("kill count: {0}/{1}", this.currentCount, this.goalCount);
this.txtStep.text = string.Format("step: {0}/{1}", this.currentStep, this.totalSteps);
this.btnAttack.onClick.AddListener(() => {
this.currentCount++;
//저장
PlayerPrefs.SetInt(currentCountKey, currentCount);
//UI업데이트
this.txtCount.text = string.Format("kill count: {0}/{1}", this.currentCount, this.goalCount);
if (this.currentCount >= this.goalCount)
{
if (this.currentStep >= this.totalSteps)
{
PlayGamesPlatform.Instance.ReportProgress(GPGSIds.achievement_2, 100, (success) => {
if (success)
{
//achievement_doing 게임오브젝트를 비활성화
this.achievementDoing.SetActive(false);
//achievement_done 게임오브젝트를 활성화
this.achievementDone.SetActive(true);
}
});
}
else
{
PlayGamesPlatform.Instance.ReportProgress(GPGSIds.achievement_2, 100, (success) => {
if (success)
{
//스텝을 올려준다
this.currentStep++;
//저장
PlayerPrefs.SetInt(stepKey, this.currentCount);
//UI업데이트
this.txtStep.text = string.Format("step: {0}/{1}", this.currentStep, this.totalSteps);
PlayGamesPlatform.Instance.IncrementAchievement(GPGSIds.achievement_2, this.currentStep, (success) => {
Debug.LogFormat("{0} {1} {2}", GPGSIds.achievement_2, this.currentStep, success);
});
//초기화
this.currentCount = 0;
//저장
PlayerPrefs.SetInt(currentCountKey, this.currentCount);
//UI업데이트
this.txtCount.text = string.Format("kill count: {0}/{1}", this.currentCount, this.goalCount);
}
});
}
}
});
this.txtUserName.text = userName;
this.btnAchievement.onClick.AddListener(() => {
PlayGamesPlatform.Instance.ShowAchievementsUI();
});
PlayGamesPlatform.Instance.IncrementAchievement(GPGSIds.achievement, 1, (bool success) => {
// handle success or failure
Debug.LogFormat("IncrementAchievement: {0}, {1}", GPGSIds.achievement, success);
});
}
}