일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
- Basic
- 오류
- github
- W3Schools
- 재귀
- python
- DP
- Tutorial
- 기초
- guide
- 프로그래밍
- Unity
- dfs
- w3school
- UE5
- parameter
- 파이썬
- C#
- Material
- c++
- 문제풀이
- 백준
- Class
- String
- Unreal Engine 5
- 시작해요 언리얼 2022
- Algorithm
- Programming
- dynamic
- loop
- Today
- Total
행복한 개구리
Unity 수업내용 21.06.23. GPGS 업적 본문
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
Github에 있는 GPGS 매뉴얼을 따라서 실행해보자.
increment : 증가
=> 즉, 업적의 단계를 증가시켜주는 함수이다. 두번째 매개변수로 해당 단계를 지정할 수 있는데, 보기에서는 5라고 되어있으니 업적 5단계로 바꿔주는 역할을 한다. 그리고 세번째 매개변수의 람다식으로 해당 통신이 성공했는지, 실패했는지 여부가 출력된다.
이런 단계별 업적을 사용하기 위해서는 Google Play Console에서 업적을 생성할 때 단계별 업적으로 설정한 뒤 단계가 얼마나 있는지도 지정해주어야한다.
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 version;
public Text txtId;
public Text txtUserName;
public Text txtState;
public Image thumb; //아바타 이미지
public Button btnStart;
public ePlayMode playMode;
void Start()
{
if (playMode == ePlayMode.Test)
{
this.btnStart.gameObject.SetActive(true);
}
else
{
this.btnStart.gameObject.SetActive(false);
}
this.btnStart.onClick.AddListener(() =>
{
SceneManager.LoadSceneAsync("GameScene").completed += (oper) =>
{
GameMain gameMain = GameObject.FindObjectOfType<GameMain>();
gameMain.Init(Social.localUser.userName);
};
});
this.version.text = Application.version;
Debug.Log("==================== Init GPGS");
PlayGamesClientConfiguration config = new PlayGamesClientConfiguration.Builder()
.Build();
PlayGamesPlatform.InitializeInstance(config);
PlayGamesPlatform.DebugLogEnabled = true;
PlayGamesPlatform.Activate();
Debug.Log("==================== Authenticate");
PlayGamesPlatform.Instance.Authenticate(SignInInteractivity.CanPromptAlways, (result) =>
{
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();
StartCoroutine(WaitForLoadThumb(() =>
{
//이미지 있음
Debug.Log("Loaded Image" + Social.localUser.image);
Debug.LogFormat("{0} x {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();
}));
}
});
}
IEnumerator WaitForLoadThumb(System.Action callback)
{
while (true)
{
if(Social.localUser.image != null)
{
break;
}
yield return null;
}
callback();
}
}
Test 타입일 때, 버튼을 누르면 GameScene을 재생하도록 한다.
using GooglePlayGames;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameMain : MonoBehaviour
{
public Text txtUserName;
public Button btnAchievement;
public Button btnAttack;
public GameObject achievementDoing;
public GameObject achievementDone;
public Text txtStep;
public Text txtCount;
private int goalCount = 10;
private int totalSteps = 2;
private int currentCount = 0;
private int currentStep = 1;
//초기화
public void Init(string userName)
{
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);
});
}
}
딱히 업적의 진행상황을 불러오는 함수를 찾지 못해서 PlayerPrefs로 업적정보를 저장한 뒤에 해당 업적 정보들로 서버와 교류한다.
스텝이 올라갈 때마다 ReportProgress함수를 통해 업적의 달성도를 전송하고 카운트가 10만큼 올라갈 때마다 Step을 1증가시켜 IncrementAchievement로 서버에 전달한다. 그리고 2단계까지 완료했다면 진행창이 사라지고 완료했다고 표시해주는 창이 나타난다.
첫번째 업적과 두번째 업적 모두 첫 실행시에는 정상작동햇으며 ShowAchievemnetsUI를 통해 잘 불러와졌다.
하지만 두번째 실행시부터는 제대로 출력되지 않았으며, UI에서 업적이 없다는 화면이 나왔다.
=> 해당 테스트를 지속하기 위해서 정상적으로 초기화하는 방법이 필요하다.
'소셜 로그인 > 수업내용' 카테고리의 다른 글
소셜로그인 21.07.08. 구글 애널리틱스(Google Analytics) (0) | 2021.07.08 |
---|---|
소셜로그인 21.06.30. Firebase - Google (0) | 2021.06.30 |
Unity 수업내용 21.06.24. Naver로그인 (0) | 2021.06.24 |
Unity 수업내용 21.06.24 GPGS LeaderBoard (0) | 2021.06.24 |
Unity 수업내용 21.06.22. GPGS (0) | 2021.06.22 |