-
06/23 GPGS 3 업적게임 플랫폼 응용프로그래밍 2021. 6. 23. 14:22
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
업적
App.cs
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 versionText; public Text txtid; public Text txtUserName; public Text txtState; public Image thumb; public Button btnStart; public ePlayMode playMode; void Start() { versionText.text = Application.version; if (this.playMode == ePlayMode.TEST) this.btnStart.gameObject.SetActive(true); else this.btnStart.gameObject.SetActive(false); //this.btnStart.gameObject.SetActive(false); this.btnStart.onClick.AddListener(() => { SceneManager.LoadScene("GameScene"); SceneManager.LoadSceneAsync("GameScene").completed += (oper) => { var gameMain = GameObject.FindObjectOfType<GameMain>(); gameMain.Init(Social.localUser.id); }; }); Debug.Log("================================> Init GPGS"); PlayGamesClientConfiguration config = new PlayGamesClientConfiguration.Builder() .EnableSavedGames() .Build(); PlayGamesPlatform.InitializeInstance(config); // recommended for debugging: PlayGamesPlatform.DebugLogEnabled = true; // Activate the Google Play Games platform PlayGamesPlatform.Activate(); Debug.Log("================================> Authenticate"); // authenticate user: PlayGamesPlatform.Instance.Authenticate(SignInInteractivity.CanPromptAlways, (result) => { // handle results Debug.Log("================================>" + result); Debug.Log("================================>" + Social.localUser); Debug.Log("================================>" + Social.localUser.authenticated); this.txtid.text = Social.localUser.id; this.txtUserName.text = Social.localUser.userName; this.txtState.text = Social.localUser.state.ToString(); StartCoroutine(this.WaitForLoadThumb(() => { Debug.Log(Social.localUser.image); 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(); })); }); } private IEnumerator WaitForLoadThumb(System.Action callback) { while(true) { if(Social.localUser.image != null) { break; } yield return null; } callback(); } }
GameMain.cs
using GooglePlayGames; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class GameMain : MonoBehaviour { public Text txtUserName; public Button btnAchivement; public Button btnAttack; public GameObject doing; public GameObject done; 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__10); string currentCountKey = string.Format("{0}_currentCount", GPGSIds.achievement__10); PlayerPrefs.GetInt(stepKey); PlayerPrefs.GetInt(currentCountKey); 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); } 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(GPGSIds.achievement__10, 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__10, 100, (success) => { if(success) { this.doing.SetActive(false); this.done.SetActive(true); } }); }else { //로딩바를 보여준다 PlayGamesPlatform.Instance.ReportProgress(GPGSIds.achievement__10, 100, (success) => //로딩바를 안보여준다 { if (success) { //스탭을 올려준다 this.currentStep++; PlayerPrefs.SetInt(stepKey, this.currentCount); this.txtStep.text = string.Format("step: {0}/{1}", this.currentStep, this.totalSteps); //로딩바를 보여준다 PlayGamesPlatform.Instance.IncrementAchievement(GPGSIds.achievement__10, currentStep, (success) => { //로딩바를 안보여준다 Debug.LogFormat("{0} {1} {2}", GPGSIds.achievement__10, this.currentStep, success); }); this.currentCount = 0; PlayerPrefs.SetInt(currentCountKey, this.currentCount); this.txtCount.text = string.Format("kill count: {0}/{1}", this.currentCount, this.goalCount); } }); } } }); //PlayGamesPlatform.Instance.LoadAchievements((ach) => //{ // Debug.LogFormat("{0}, {1}", ach[1].id, ach[1].percentCompleted); //}); this.txtUserName.text = userName; this.btnAchivement.onClick.AddListener(() => { PlayGamesPlatform.Instance.ShowAchievementsUI(); }); PlayGamesPlatform.Instance.IncrementAchievement( GPGSIds.achievement, 1, (bool success) => { // handle success or failure Debug.LogFormat("IncrementAchivement {0}, {1}", GPGSIds.achievement, success); }); } }
Immortal.cs
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Immortal : MonoBehaviour { private void Awake() { DontDestroyOnLoad(this.gameObject); } }
SampleScene GameScene '게임 플랫폼 응용프로그래밍' 카테고리의 다른 글
06/25 웹뷰 (0) 2021.06.25 06/24 네이버로그인 (0) 2021.06.24 06/24 GPGS 4 리더보드 (0) 2021.06.24 06/22 GPGS 테스트 2 (0) 2021.06.22 06/18~19 유니티 어플 구글 플레이 콘솔에 등록 및 로그인 구현 연습 (0) 2021.06.17