当前位置:   article > 正文

Unity3D GameCenter 排行榜, 成就实现_unity3d iuserprofile

unity3d iuserprofile

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. public class IOSManager : MonoBehaviour {
  6. public bool GameCenterState;
  7. public string userInfo;
  8. /// <summary>
  9. /// 初始化 GameCenter 登陆
  10. /// </summary>
  11. void Start () {
  12. Social.localUser.Authenticate(HandleAuthenticated);
  13. }
  14. /// <summary>
  15. /// 初始化 GameCenter 结果回调函数
  16. /// </summary>
  17. /// <param name="success">If set to <c>true</c> success.</param>
  18. private void HandleAuthenticated(bool success)
  19. {
  20. GameCenterState = success;
  21. Debug.Log("*** HandleAuthenticated: success = " + success);
  22. ///初始化成功
  23. if (success) {
  24. userInfo = "Username: " + Social.localUser.userName +
  25. "\nUser ID: " + Social.localUser.id +
  26. "\nIsUnderage: " + Social.localUser.underage;
  27. Debug.Log (userInfo);
  28. } else {
  29. ///初始化失败
  30. }
  31. }
  32. void OnGUI(){
  33. GUI.TextArea ( new Rect( Screen.width -200, 0, 200, 100), "GameCenter:"+GameCenterState );
  34. GUI.TextArea ( new Rect( Screen.width -200, 100, 200, 100), "userInfo:"+userInfo );
  35. if (GUI.Button (new Rect (0, 0, 110, 75), "打开成就")) {
  36. if (Social.localUser.authenticated) {
  37. Social.ShowAchievementsUI();
  38. }
  39. }
  40. if (GUI.Button (new Rect (0, 150, 110, 75), "打开排行榜")) {
  41. if (Social.localUser.authenticated) {
  42. Social.ShowLeaderboardUI();
  43. }
  44. }
  45. if (GUI.Button (new Rect (0, 300, 110, 75), "排行榜设置分数")) {
  46. if (Social.localUser.authenticated) {
  47. Social.ReportScore(1000, "XXXX", HandleScoreReported);
  48. }
  49. }
  50. if (GUI.Button (new Rect (0, 300, 110, 75), "设置成就")) {
  51. if (Social.localUser.authenticated) {
  52. Social.ReportProgress("XXXX", 15, HandleProgressReported);
  53. }
  54. }
  55. }
  56. //上传排行榜分数
  57. public void HandleScoreReported(bool success)
  58. {
  59. Debug.Log("*** HandleScoreReported: success = " + success);
  60. }
  61. //设置 成就
  62. private void HandleProgressReported(bool success)
  63. {
  64. Debug.Log("*** HandleProgressReported: success = " + success);
  65. }
  66. /// <summary>
  67. /// 加载好友回调
  68. /// </summary>
  69. /// <param name="success">If set to <c>true</c> success.</param>
  70. private void HandleFriendsLoaded(bool success)
  71. {
  72. Debug.Log("*** HandleFriendsLoaded: success = " + success);
  73. foreach(IUserProfile friend in Social.localUser.friends)
  74. {
  75. Debug.Log("* friend = " + friend.ToString());
  76. }
  77. }
  78. /// <summary>
  79. /// 加载成就回调
  80. /// </summary>
  81. /// <param name="achievements">Achievements.</param>
  82. private void HandleAchievementsLoaded(IAchievement[] achievements)
  83. {
  84. Debug.Log("* HandleAchievementsLoaded");
  85. foreach(IAchievement achievement in achievements)
  86. {
  87. Debug.Log("* achievement = " + achievement.ToString());
  88. }
  89. }
  90. /// <summary>
  91. ///
  92. /// 成就回调描述
  93. /// </summary>
  94. /// <param name="achievementDescriptions">Achievement descriptions.</param>
  95. private void HandleAchievementDescriptionsLoaded(IAchievementDescription[] achievementDescriptions)
  96. {
  97. Debug.Log("*** HandleAchievementDescriptionsLoaded");
  98. foreach(IAchievementDescription achievementDescription in achievementDescriptions)
  99. {
  100. Debug.Log("* achievementDescription = " + achievementDescription.ToString());
  101. }
  102. }
  103. }
 别着急此时你要是在真机运行估计还不可以,你还需要在 ITunes Connect 里面设置 排行榜单成就

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



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


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


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

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号