【转】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.      if ( Social. localUser. authenticated ) {
  68.        Social. ReportProgress (achievementId, progress, HandleProgressReported );
  69.      }
  70.    }
  71.   
  72.    private void HandleProgressReported ( bool success )
  73.    {
  74.      Debug. Log ( "*** HandleProgressReported: success = " + success );
  75.    }
  76.   
  77.    public void ShowAchievements ( )
  78.    {
  79.      if ( Social. localUser. authenticated ) {
  80.        Social. ShowAchievementsUI ( );
  81.      }
  82.    }
  83.   
  84.    // leaderboard
  85.   
  86.    public void ReportScore ( string leaderboardId, long score )
  87.    {
  88.      if ( Social. localUser. authenticated ) {
  89.        Social. ReportScore (score, leaderboardId, HandleScoreReported );
  90.      }
  91.    }
  92.   
  93.    public void HandleScoreReported ( bool success )
  94.    {
  95.      Debug. Log ( "*** HandleScoreReported: success = " + success );
  96.    }
  97.   
  98.    public void ShowLeaderboard ( )
  99.    {
  100.      if ( Social. localUser. authenticated ) {
  101.        Social. ShowLeaderboardUI ( );
  102.      }
  103.    }
  104.   
  105.    // gui
  106.   
  107.    public void OnGUI ( )
  108.    {
  109.      // four buttons, allowing us to bump and test setting achievements
  110.      int yDelta = buttonGap;
  111.      if ( GUI. Button ( new Rect (buttonGap, yDelta, buttonWidth, buttonHeight ), "Ach 1" ) ) {
  112.        ReportProgress ( "A0001", ach1 );
  113.       ach1 = (ach1 == 100 ) ? 0 : ach1 + 10;
  114.      }
  115.     yDelta += buttonHeight + buttonGap;
  116.      if ( GUI. Button ( new Rect (buttonGap, yDelta, buttonWidth, buttonHeight ), "Ach 2" ) ) {
  117.        ReportProgress ( "A0002", ach2 );
  118.       ach2 = (ach2 == 100 ) ? 0 : ach2 + 10;
  119.      }
  120.     yDelta += buttonHeight + buttonGap;
  121.      if ( GUI. Button ( new Rect (buttonGap, yDelta, buttonWidth, buttonHeight ), "Ach 3" ) ) {
  122.        ReportProgress ( "A0003", ach3 );
  123.       ach3 = (ach3 == 100 ) ? 0 : ach3 + 10;
  124.      }
  125.     yDelta += buttonHeight + buttonGap;
  126.      if ( GUI. Button ( new Rect (buttonGap, yDelta, buttonWidth, buttonHeight ), "Ach 4" ) ) {
  127.        ReportProgress ( "A0004", ach4 );
  128.       ach4 = (ach4 == 100 ) ? 0 : ach4 + 10;
  129.      }
  130.      // show achievements
  131.     yDelta += buttonHeight + buttonGap;
  132.      if ( GUI. Button ( new Rect (buttonGap, yDelta, buttonWidth, buttonHeight ), "Show Achievements" ) ) {
  133.       ShowAchievements ( );
  134.      }
  135.     
  136.      // two buttons, allowing us to bump and test setting high scores
  137.      int xDelta = Screen. width - buttonWidth - buttonGap;
  138.     yDelta = buttonGap;
  139.      if ( GUI. Button ( new Rect (xDelta, yDelta, buttonWidth, buttonHeight ), "Score 1" ) ) {
  140.        ReportScore ( "L01", score1 );
  141.       score1 += 500;
  142.      }
  143.     yDelta += buttonHeight + buttonGap;
  144.      if ( GUI. Button ( new Rect (xDelta, yDelta, buttonWidth, buttonHeight ), "Score 2" ) ) {
  145.        ReportScore ( "L02", score2 );
  146.       score2 += 100;
  147.      }
  148.      // show leaderboard
  149.     yDelta += buttonHeight + buttonGap;
  150.      if ( GUI. Button ( new Rect (xDelta, yDelta, buttonWidth, buttonHeight ), "Show Leaderboard" ) ) {
  151.       ShowLeaderboard ( );
  152.      }
  153.    }
  154. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值