GameCenter(Social)
UnityからGameCenterにつないでみたくて試してみました。
主に使うのはSocialクラスです。Apple Game Centerだけでなく、XBoxのスコアボードにも共通のクラスでアクセスできるので便利。便利でシンプルなんだけど、構造が少し分かり辛く戸惑いました。
まずは何よりログイン。これはSocial.localUser.Authenticate()にて実現できる。
Social.localUser.Authenticate(success => {
this.logined = success;
Debug.Log("ログイン : " + success);
});
次にスコア送信。これは簡単でSocial.ReportScore()。
Social.ReportScore(1000, this.leaderboardID, success => {
Debug.Log(success ? "送信成功" : "送信失敗");
});
既成のインターフェースでのLeaderboardの表示は、こちらも簡単でSocial.ShowLeaderboardUI()を呼ぶだけ。
Social.ShowLeaderboardUI();
既成のインターフェースではなく、データとしてスコアの取得にはSocial.LoadScores()。
Social.LoadScores(this.leaderboardID, scores => {
Debug.Log("スコア件数 : " + scores.Length);
foreach (IScore score in scores) {
Debug.Log(score.userID + " : " + score.formattedValue);
}
});
ただしこれで取れるのは自身のスコアのみ。それ以外のスコアデータを取るには、SocialクラスにCreateLeaderboard()というメソッドがあり、ここで作られるILeaderboardインスタンスを使うと取れる。ILeaderboardのuserScopeというプロパティがあるのでこれを設定すると取得する範囲が変わり、UserScope.GlobalやUserScope.FriendsOnlyがある。
友達の取得にはSocial.localUser.friendsに格納されているんだけど、初期は情報がない状態で、Social.localUser.LoadFriends()を呼び出すことにより格納される。
Social.localUser.LoadFriends(success => {
Debug.Log("友達の数 : " + Social.localUser.friends.Length);
foreach (IUserProfile friend in Social.localUser.friends) {
Debug.Log(friend.id + " : " + friend.userName);
}
});
次は友達のスコアの取得。先ほどのSocial.LoadScores()にて取得できる情報ではIScoreなので、ここで取得できるのはユーザーのIDのみ。なので、友達情報(IUserProfile)をあらかじめ取得しておいて情報を結合させる必要がある。
ILeaderboard leaderboard = Social.CreateLeaderboard();
leaderboard.id = this.leaderboardID;
leaderboard.userScope = UserScope.FriendsOnly;
leaderboard.LoadScores(success => {
Debug.Log("取得 : " + success);
Debug.Log("件数 : " + leaderboard.scores.Length);
foreach (IScore score in leaderboard.scores) {
foreach (IUserProfile profile in Social.localUser.friends) {
if (score.userID == profile.id) {
Debug.Log(profile.id + " : " + profile.userName + " : " + score.formattedValue);
}
}
}
});
Achievementについてはあまりやってないけど、Leaderboard同様にできそう。
コメントを残す