排行榜

Unity3D内部已经集成了对 GameCenter的支持 在

UnityEngine.SocialPlatforms命名空间下,基本已经满足目前需求

 http://wiki.ceeger.com/manual:social_api?s[]=socialplatforms

 

C#:

 

 
  1. using UnityEngine;

  2. using System.Collections;

  3. using UnityEngine.SocialPlatforms;

  4. using UnityEngine.SocialPlatforms.GameCenter;

  5.  
  6. public class IOSManager : MonoBehaviour {

  7.  
  8. public bool GameCenterState;

  9. public string userInfo;

  10. /// <summary>

  11. /// 初始化 GameCenter 登陆

  12. /// </summary>

  13. void Start () {

  14. Social.localUser.Authenticate(HandleAuthenticated);

  15. }

  16.  
  17. /// <summary>

  18. /// 初始化 GameCenter 结果回调函数

  19. /// </summary>

  20. /// <param name="success">If set to <c>true</c> success.</param>

  21. private void HandleAuthenticated(bool success)

  22. {

  23. GameCenterState = success;

  24. Debug.Log("*** HandleAuthenticated: success = " + success);

  25. ///初始化成功

  26. if (success) {

  27. userInfo = "Username: " + Social.localUser.userName +

  28. "\nUser ID: " + Social.localUser.id +

  29. "\nIsUnderage: " + Social.localUser.underage;

  30. Debug.Log (userInfo);

  31. } else {

  32. ///初始化失败

  33.  
  34. }

  35. }

  36.  
  37.  
  38. void OnGUI(){

  39.  
  40. GUI.TextArea ( new Rect( Screen.width -200, 0, 200, 100), "GameCenter:"+GameCenterState );

  41. GUI.TextArea ( new Rect( Screen.width -200, 100, 200, 100), "userInfo:"+userInfo );

  42.  
  43. if (GUI.Button (new Rect (0, 0, 110, 75), "打开成就")) {

  44.  
  45. if (Social.localUser.authenticated) {

  46. Social.ShowAchievementsUI();

  47. }

  48. }

  49.  
  50. if (GUI.Button (new Rect (0, 150, 110, 75), "打开排行榜")) {

  51.  
  52. if (Social.localUser.authenticated) {

  53. Social.ShowLeaderboardUI();

  54. }

  55. }

  56.  
  57. if (GUI.Button (new Rect (0, 300, 110, 75), "排行榜设置分数")) {

  58.  
  59. if (Social.localUser.authenticated) {

  60. Social.ReportScore(1000, "XXXX", HandleScoreReported);

  61. }

  62. }

  63.  
  64. if (GUI.Button (new Rect (0, 300, 110, 75), "设置成就")) {

  65.  
  66. if (Social.localUser.authenticated) {

  67. Social.ReportProgress("XXXX", 15, HandleProgressReported);

  68. }

  69. }

  70.  
  71. }

  72.  
  73.  
  74. //上传排行榜分数

  75. public void HandleScoreReported(bool success)

  76. {

  77. Debug.Log("*** HandleScoreReported: success = " + success);

  78. }

  79. //设置 成就

  80. private void HandleProgressReported(bool success)

  81. {

  82. Debug.Log("*** HandleProgressReported: success = " + success);

  83. }

  84.  
  85. /// <summary>

  86. /// 加载好友回调

  87. /// </summary>

  88. /// <param name="success">If set to <c>true</c> success.</param>

  89. private void HandleFriendsLoaded(bool success)

  90. {

  91. Debug.Log("*** HandleFriendsLoaded: success = " + success);

  92. foreach(IUserProfile friend in Social.localUser.friends)

  93. {

  94. Debug.Log("* friend = " + friend.ToString());

  95. }

  96. }

  97.  
  98. /// <summary>

  99. /// 加载成就回调

  100. /// </summary>

  101. /// <param name="achievements">Achievements.</param>

  102. private void HandleAchievementsLoaded(IAchievement[] achievements)

  103. {

  104. Debug.Log("* HandleAchievementsLoaded");

  105. foreach(IAchievement achievement in achievements)

  106. {

  107. Debug.Log("* achievement = " + achievement.ToString());

  108. }

  109. }

  110.  
  111. /// <summary>

  112. ///

  113. /// 成就回调描述

  114. /// </summary>

  115. /// <param name="achievementDescriptions">Achievement descriptions.</param>

  116. private void HandleAchievementDescriptionsLoaded(IAchievementDescription[] achievementDescriptions)

  117. {

  118. Debug.Log("*** HandleAchievementDescriptionsLoaded");

  119. foreach(IAchievementDescription achievementDescription in achievementDescriptions)

  120. {

  121. Debug.Log("* achievementDescription = " + achievementDescription.ToString());

  122. }

  123. }

  124.  
  125.  
  126.  
  127.  
  128.  
  129. }

 别着急此时你要是在真机运行估计还不可以,你还需要在 ITunes Connect 里面设置排行榜单 和 成就

 

当然此时你的APP 应该是已经申请好的咯,并且 “GameCenter” 功能应该是开启的
 

 

排行榜

 

然后就是 设置你的 排行榜ID  和  成就ID

排行榜

 

代码中的“XXXX” 的地方就是 填这两个东西

 

完事了然后你就可以在你的 游戏中用 GameCenter 了!