Unity メモ :iOS GameCenter実装についてまとめ
Unityで作成するアプリにiOS GameCenterを実装するには、
1 : "Unity上でのスクリプト設定"
2 : "iTunesConnect上での設定"
3 : "iOS Provisioning Portal上での設定"
4 : "Xcode上での設定"
の4項目がありました。
1:Unity上での設定
①GameCenterの認証を行い、ソーシャルAPIの呼び出しを行うスクリプトをシーン内の最初に起動するように組み込む。
②スコアをLeaderbordに送信する。
③スコアをLeaderbordから取得する。
④ボタンなどを押した後に、GameCenter Leaderbordを呼び出す。
①: "SocialExample.cs" をMain Cameraなどに割り当てる。
Unity Script Reference: SocialPlatforms.ILocalUser.Authenticate
ScosialExample.cs
using UnityEngine; using UnityEngine.SocialPlatforms; public class SocialExample : MonoBehaviour { void Awake () { // 認証を行い、ProcessAuthenticationコールバックを登録 // 他のソーシャルAPIの呼び出しを行う前にこの呼び出しを行う必要があります。 Social.localUser.Authenticate (ProcessAuthentication); } // この関数は認証が完了した後に呼び出しされます // 処理が正常終了した場合、Social.localUserにはサーバから取得したデータを含むことに留意して下さい。 void ProcessAuthentication (bool success) { if (success) { Debug.Log ("認証完了、Achievementsをチェックしています"); // ロードされているAchievementsをリクエストし、それを処理するためのコールバックを登録します。 ILeaderboard leaderboard = Social.CreateLeaderboard(); leaderboard.id = "string leaderboardID"; leaderboard.LoadScores (result => { Debug.Log("スコアを " + leaderboard.scores.Length + " 受信しました。"); foreach (IScore score in leaderboard.scores) Debug.Log(score); }); } else Debug.Log ("認証失敗"); } // LoadAchievement呼び出しが完了するとこの関数が呼び出しされます。 void ProcessLoadedAchievements (IAchievement[] achievements) { if (achievements.Length == 0) Debug.Log ("エラー:Achievementsは見つかりませんでした。"); else Debug.Log ("取得:" + achievements.Length + " achievements"); // また、次のように関数を中で呼び出しすることも出来ます Social.ReportProgress ("Achievement01", 100.0, result => { if (result) Debug.Log ("正常にをAchievementの進捗をレポート"); else Debug.Log ("認証失敗"); }); } }
②: Leaderbordに送信したいスコアの値を、任意のタイミングで送信する。(リザルトの時、ランキングボタンを押した時、ゲームを起動した時など?…etc)
Test_ScoreReport.cs
Unity Script Reference: ReportScore
using UnityEngine; using System.Collections; public class Test_ScoreReport : MonoBehaviour { public int score1; void Start(){ ReportScore(score1, "leaderboardID"); // iThues Connectで設定したleaderbordIDを入れる。 } void ReportScore (long score, string leaderboardID) { Debug.Log ("Reporting score " + score + " on leaderboard " + leaderboardID); Social.ReportScore (score, leaderboardID, success => { Debug.Log(success ? "Reported score successfully" : "Failed to report score"); }); } }
③④:Leaderbordのscoreを取得して、GameCenterを呼び出す。
ボタンを押した時に実行させるスクリプトをGameCenterのボタンに割り当てる。
(もしかした③は、必要ないかもしれません…)
Unity Script Reference: Social.LoadScores
Unity Script Reference: Social.ShowLeaderboardUI
Button_GameCenter.cs
using UnityEngine; using System.Collections; using UnityEngine.SocialPlatforms; public class Button_GameCenter : MonoBehaviour { public Vector3 checkScale; void OnMouseDown() { audio.Play(); checkScale = transform.localScale; transform.localScale += new Vector3(-0.1F, -0.1F, -0.1F); //③ Social.LoadScores("string leaderboardID", scores => { if (scores.Length > 0) { Debug.Log ("保有: " + scores.Length + " 個のスコア"); string myScores = "Leaderboard:\n"; foreach (IScore score in scores) myScores += "\t" + score.userID + " " + score.formattedValue + " " + score.date + "\n"; Debug.Log (myScores); } else Debug.Log ("スコアはロードされませんでした。"); }); Social.ShowLeaderboardUI(); } void OnMouseUp() { transform.localScale = checkScale; //④ } }
続きは、また今度に。。
-
20140712追記
4:Xcode上での設定
■Xcodeでの設定
・GameKit.frameworkをフレームワークに追加する
・info.plistの「Required device capabilities」に「gamekit」を追加する
・プログラム追加(iOS6以上とiOS6より下とで認証方法が違う)
→iOS6以上: [self presentViewController:viewController animated:YES completion:nil];
→それ以外: [localPlayer authenticateWithCompletionHandler:^(NSError *error)
こちらのサイト様を参考に設定をしてみてください。