【Unity】Play Game ServicesのSign-in処理とか【Android/iOS】
前回の記事でGooglePlayGameにログインする処理などテキトウにしてしまったので素人知識全開で書いてみたいと思います。
kirisimcreate.hateblo.jp
サインイン
using GooglePlayGames; using GooglePlayGames.BasicApi; using UnityEngine.SocialPlatforms; public class PlayGameSignIn : MonoBehaviour{ void Start() { //初期化 PlayGamesClientConfiguration config = new PlayGamesClientConfiguration.Builder().Build(); PlayGamesPlatform.InitializeInstance(config); PlayGamesPlatform.Activate(); //デバッグログを出したい場合 PlayGamesPlatform.DebugLogEnabled = true; } private void SignIn() { Social.localUser.Authenticate((bool success) => { if(success) { Debug.Log("ログイン成功") } }); } }
リーダーボードにスコアを登録
スコアを登録したいリーダーボードのIDをコピーしておきましょう
using GooglePlayGames; using UnityEngine.SocialPlatforms; public class Leaderboard : MonoBehaviour{ int score; int highScore; string highScoreKey; //一度だけ処理する bool isDone; void Start() { highScorekey = "HIGH SCORE"; highScore = PlayerPrefs.Getint(highScoreKey, 0); isDone = false; } void Update() { if(score > highScore && !isDone) { highScore = score; PlayerPrefs.Setint(highScoreKey, highScore); Social.ReportScore(highScore, "リーダーボードのid", (bool success) => { if (success) isDone = true; }); } } }
リーダーボードを表示
uGUIを使っているならボタンをメニュー画面などに設置して
using GooglePlayGames; using UnityEngine.Socialplatforms; public class ShowLeaderboard : MonoBehaviour{ public void ShowLeaderBoardButton() { Social.ShowLeaderdoard(); } }
スクリプトをボタンにアタッチしてOnClick()の+ボタンをクリックします
ボタンオブジェクトを追加して先ほど記述したShowLeaderBoardButton()を選択します
これでボタンを押せばワンクッション挟んでリーダーボードが表示できます。
もし、リーダーボードが一つしかなくて直接そのリーダーボードを表示したい場合は
public void ShowLeaderBoardButton() { PlayGamesPlatform.Instance.ShowLeaderboardUI("リーダーボードid"); }
このように書き換えてやれば直接リーダーボードを表示できます。
実績の解除
増分実績を設定している場合としていない場合で多少書き方が変わります。
using GooglePlayGames; using UnityEngine.Socialplatforms; public class Progress : MonoBehaviour { int Score; int TotalScore; void Update() { if(Score >= 1000) { Social.ReportProgress("解除したい実績のID", 100.0f, (bool success) => { if(success) { Debug.Log("成功"); } }); { /* 増分実績を設定している場合 Social.ReportProgress()を使うことは非推奨なので以下を使います */ if(/*ゲームオーバーになった時とか*/) { //Score分加算されていって増分実績で設定している値以上になったら実績解除です PlayGamesPlatform.Instance.IncrementAchievement("解除したい実績のID", Score, (bool success) => { if(success) { Debug.Log("成功"); } }); } } }
実績表示
ボタンなどを設置して
using GooglePlayGames; using UnityEngine.SocialPlatforms; public class ShowAchievement :MonoBehaviour { public void Show_achievement() { Social.ShowAchievementsUI(); } }
Editorで確認すると実績解除のタイミングでエラーになってしまう場合があるので
if(Application.platform == RuntimePlatform.Android)
とか実機でしか処理しないようにしたほうがいいかもしれません。
他にもできることは多いようなので分かり次第更新したいと思います。