【转】Unity3.5 GameCenter基础教程

  1. 转载地址
     
  2. using UnityEngine;
  3. using UnityEngine.SocialPlatforms;
  4.  
  5. public class Startup : MonoBehaviour
  6. {
  7.   // we'll create some buttons in OnGui, allowing us to bump achievement and
  8.   // score values for testing
  9.   
  10.   private double ach1 = 0;
  11.   private double ach2 = 0;
  12.   private double ach3 = 0;
  13.   private double ach4 = 0;
  14.   
  15.   private long score1 = 1000;
  16.   private long score2 = 200;
  17.   
  18.   private int buttonWidth = 120;
  19.   private int buttonHeight = 50;
  20.   private int buttonGap = 10;
  21.   
  22.   void Start()
  23.   {
  24.     Social.localUser.Authenticate(HandleAuthenticated);
  25.   }
  26.   
  27.   // authentication
  28.   
  29.   private void HandleAuthenticated(bool success)
  30.   {
  31.     Debug.Log("*** HandleAuthenticated: success = " + success);
  32.     if (success) {
  33.       Social.localUser.LoadFriends(HandleFriendsLoaded);
  34.       Social.LoadAchievements(HandleAchievementsLoaded);
  35.       Social.LoadAchievementDescriptions(HandleAchievementDescriptionsLoaded);
  36.     }
  37.   }
  38.   
  39.   private void HandleFriendsLoaded(bool success)
  40.   {
  41.     Debug.Log("*** HandleFriendsLoaded: success = " + success);
  42.     foreach (IUserProfile friend in Social.localUser.friends) {
  43.       Debug.Log("*   friend = " + friend.ToString());
  44.     }
  45.   }
  46.   
  47.   private void HandleAchievementsLoaded(IAchievement[] achievements)
  48.   {
  49.     Debug.Log("*** HandleAchievementsLoaded");
  50.     foreach (IAchievement achievement in achievements) {
  51.       Debug.Log("*   achievement = " + achievement.ToString());
  52.     }
  53.   }
  54.   
  55.   private void HandleAchievementDescriptionsLoaded(IAchievementDescription[] achievementDescriptions)
  56.   {
  57.     Debug.Log("*** HandleAchievementDescriptionsLoaded");
  58.     foreach (IAchievementDescription achievementDescription in achievementDescriptions) {
  59.       Debug.Log("*   achievementDescription = " + achievementDescription.ToString());
  60.     }
  61.   }
  62.   
  63.   // achievements
  64.   
  65.   public void ReportProgress(string achievementId, double progress)
  66.   {
  67.       Social.ReportProgress(achievementId, progress, HandleProgressReported);
  68.     }
  69.   }
  70.   
  71.   private void HandleProgressReported(bool success)
  72.   {
  73.     Debug.Log("*** HandleProgressReported: success = " + success);
  74.   }
  75.   
  76.   public void ShowAchievements()
  77.   {
  78.       Social.ShowAchievementsUI();
  79.     }
  80.   }
  81.   
  82.   // leaderboard
  83.   
  84.   public void ReportScore(string leaderboardId, long score)
  85.   {
  86.       Social.ReportScore(score, leaderboardId, HandleScoreReported);
  87.     }
  88.   }
  89.   
  90.   public void HandleScoreReported(bool success)
  91.   {
  92.     Debug.Log("*** HandleScoreReported: success = " + success);
  93.   }
  94.   
  95.   public void ShowLeaderboard()
  96.   {
  97.       Social.ShowLeaderboardUI();
  98.     }
  99.   }
  100.   
  101.   // gui
  102.   
  103.   public void OnGUI()
  104.   {
  105.     // four buttons, allowing us to bump and test setting achievements
  106.     int yDelta = buttonGap;
  107.     if (GUI.Button(new Rect(buttonGap, yDelta, buttonWidth, buttonHeight), "Ach 1")) {
  108.       ReportProgress("A0001", ach1);
  109.       ach1 = (ach1 == 100) ? 0 : ach1 + 10;
  110.     }
  111.     yDelta += buttonHeight + buttonGap;
  112.     if (GUI.Button(new Rect(buttonGap, yDelta, buttonWidth, buttonHeight), "Ach 2")) {
  113.       ReportProgress("A0002", ach2);
  114.       ach2 = (ach2 == 100) ? 0 : ach2 + 10;
  115.     }
  116.     yDelta += buttonHeight + buttonGap;
  117.     if (GUI.Button(new Rect(buttonGap, yDelta, buttonWidth, buttonHeight), "Ach 3")) {
  118.       ReportProgress("A0003", ach3);
  119.       ach3 = (ach3 == 100) ? 0 : ach3 + 10;
  120.     }
  121.     yDelta += buttonHeight + buttonGap;
  122.     if (GUI.Button(new Rect(buttonGap, yDelta, buttonWidth, buttonHeight), "Ach 4")) {
  123.       ReportProgress("A0004", ach4);
  124.       ach4 = (ach4 == 100) ? 0 : ach4 + 10;
  125.     }
  126.     // show achievements
  127.     yDelta += buttonHeight + buttonGap;
  128.     if (GUI.Button(new Rect(buttonGap, yDelta, buttonWidth, buttonHeight), "Show Achievements")) {
  129.       ShowAchievements();
  130.     }
  131.     
  132.     // two buttons, allowing us to bump and test setting high scores
  133.     int xDelta = Screen.width - buttonWidth - buttonGap;
  134.     yDelta = buttonGap;
  135.     if (GUI.Button(new Rect(xDelta, yDelta, buttonWidth, buttonHeight), "Score 1")) {
  136.       ReportScore("L01", score1);
  137.       score1 += 500;
  138.     }
  139.     yDelta += buttonHeight + buttonGap;
  140.     if (GUI.Button(new Rect(xDelta, yDelta, buttonWidth, buttonHeight), "Score 2")) {
  141.       ReportScore("L02", score2);
  142.       score2 += 100;
  143.     }
  144.     // show leaderboard
  145.     yDelta += buttonHeight + buttonGap;
  146.     if (GUI.Button(new Rect(xDelta, yDelta, buttonWidth, buttonHeight), "Show Leaderboard")) {
  147.       ShowLeaderboard();
  148.     }
  149.   }
  150. }
posted @ 2012-07-11 22:26  U_探索  阅读(2139)  评论(0编辑  收藏  举报