排行榜
Unity3D内部已经集成了对 GameCenter的支持 在
UnityEngine.SocialPlatforms命名空间下,基本已经满足目前需求
http://wiki.ceeger.com/manual:social_api?s[]=socialplatforms
C#:
-
using UnityEngine;
-
using System.Collections;
-
using UnityEngine.SocialPlatforms;
-
using UnityEngine.SocialPlatforms.GameCenter;
-
public class IOSManager : MonoBehaviour {
-
public bool GameCenterState;
-
public string userInfo;
-
/// <summary>
-
/// 初始化 GameCenter 登陆
-
/// </summary>
-
void Start () {
-
Social.localUser.Authenticate(HandleAuthenticated);
-
}
-
/// <summary>
-
/// 初始化 GameCenter 结果回调函数
-
/// </summary>
-
/// <param name="success">If set to <c>true</c> success.</param>
-
private void HandleAuthenticated(bool success)
-
{
-
GameCenterState = success;
-
Debug.Log("*** HandleAuthenticated: success = " + success);
-
///初始化成功
-
if (success) {
-
userInfo = "Username: " + Social.localUser.userName +
-
"\nUser ID: " + Social.localUser.id +
-
"\nIsUnderage: " + Social.localUser.underage;
-
Debug.Log (userInfo);
-
} else {
-
///初始化失败
-
}
-
}
-
void OnGUI(){
-
GUI.TextArea ( new Rect( Screen.width -200, 0, 200, 100), "GameCenter:"+GameCenterState );
-
GUI.TextArea ( new Rect( Screen.width -200, 100, 200, 100), "userInfo:"+userInfo );
-
if (GUI.Button (new Rect (0, 0, 110, 75), "打开成就")) {
-
if (Social.localUser.authenticated) {
-
Social.ShowAchievementsUI();
-
}
-
}
-
if (GUI.Button (new Rect (0, 150, 110, 75), "打开排行榜")) {
-
if (Social.localUser.authenticated) {
-
Social.ShowLeaderboardUI();
-
}
-
}
-
if (GUI.Button (new Rect (0, 300, 110, 75), "排行榜设置分数")) {
-
if (Social.localUser.authenticated) {
-
Social.ReportScore(1000, "XXXX", HandleScoreReported);
-
}
-
}
-
if (GUI.Button (new Rect (0, 300, 110, 75), "设置成就")) {
-
if (Social.localUser.authenticated) {
-
Social.ReportProgress("XXXX", 15, HandleProgressReported);
-
}
-
}
-
}
-
//上传排行榜分数
-
public void HandleScoreReported(bool success)
-
{
-
Debug.Log("*** HandleScoreReported: success = " + success);
-
}
-
//设置 成就
-
private void HandleProgressReported(bool success)
-
{
-
Debug.Log("*** HandleProgressReported: success = " + success);
-
}
-
/// <summary>
-
/// 加载好友回调
-
/// </summary>
-
/// <param name="success">If set to <c>true</c> success.</param>
-
private void HandleFriendsLoaded(bool success)
-
{
-
Debug.Log("*** HandleFriendsLoaded: success = " + success);
-
foreach(IUserProfile friend in Social.localUser.friends)
-
{
-
Debug.Log("* friend = " + friend.ToString());
-
}
-
}
-
/// <summary>
-
/// 加载成就回调
-
/// </summary>
-
/// <param name="achievements">Achievements.</param>
-
private void HandleAchievementsLoaded(IAchievement[] achievements)
-
{
-
Debug.Log("* HandleAchievementsLoaded");
-
foreach(IAchievement achievement in achievements)
-
{
-
Debug.Log("* achievement = " + achievement.ToString());
-
}
-
}
-
/// <summary>
-
///
-
/// 成就回调描述
-
/// </summary>
-
/// <param name="achievementDescriptions">Achievement descriptions.</param>
-
private void HandleAchievementDescriptionsLoaded(IAchievementDescription[] achievementDescriptions)
-
{
-
Debug.Log("*** HandleAchievementDescriptionsLoaded");
-
foreach(IAchievementDescription achievementDescription in achievementDescriptions)
-
{
-
Debug.Log("* achievementDescription = " + achievementDescription.ToString());
-
}
-
}
-
}
别着急此时你要是在真机运行估计还不可以,你还需要在 ITunes Connect 里面设置排行榜单 和 成就
当然此时你的APP 应该是已经申请好的咯,并且 “GameCenter” 功能应该是开启的
然后就是 设置你的 排行榜ID 和 成就ID
代码中的“XXXX” 的地方就是 填这两个东西
完事了然后你就可以在你的 游戏中用 GameCenter 了!