79 lines
		
	
	
		
			2.0 KiB
		
	
	
	
		
			C#
		
	
	
	
			
		
		
	
	
			79 lines
		
	
	
		
			2.0 KiB
		
	
	
	
		
			C#
		
	
	
	
| using System.Collections;
 | ||
| using System.Collections.Generic;
 | ||
| using Touka.GameLogic;
 | ||
| using UnityEngine;
 | ||
| using UnityEngine.SocialPlatforms;
 | ||
| 
 | ||
| /// <summary>
 | ||
| /// Game Center 排行榜类
 | ||
| ///
 | ||
| /// 1. 初始化 init;(StaticOtherConfig 里LeaderBoard_Switch开关打开,自动调用)
 | ||
| /// 2. 上报分数 ReportScore;(分数,排行榜ID)
 | ||
| /// 3. 显示排行榜 ShowLeaderboard;
 | ||
| /// </summary>
 | ||
| public class LeaderBoards : ToukaSingletonMonoBehaviour<LeaderBoards>
 | ||
| {
 | ||
|     //public string leaderBoardId = "BossFightScore";
 | ||
| 
 | ||
|     /// <summary>
 | ||
|     /// 初始化
 | ||
|     /// </summary>
 | ||
|     public void init()
 | ||
|     {
 | ||
|         AuthenticateToGameCenter();
 | ||
|     }
 | ||
| 
 | ||
|     /// <summary>
 | ||
|     /// 登录Game Center
 | ||
|     /// </summary>
 | ||
|     private bool isAuthenticatedToGameCenter;
 | ||
|     public void AuthenticateToGameCenter()
 | ||
|     {
 | ||
| #if UNITY_IPHONE && !UNITY_EDITOR
 | ||
|         Social.localUser.Authenticate(success =>
 | ||
|         {
 | ||
|             if (success)
 | ||
|             {
 | ||
|                 Debug.Log("[LeaderBoards] Authentication successful");
 | ||
|             }
 | ||
|             else
 | ||
|             {
 | ||
|                 Debug.Log("[LeaderBoards] Authentication failed");
 | ||
|             }
 | ||
|         });
 | ||
| #endif
 | ||
|     }
 | ||
| 
 | ||
|     /// <summary>
 | ||
|     /// 上报分数到Game Center
 | ||
|     /// </summary>
 | ||
|     /// <param name="score"> 分数 </param>
 | ||
|     /// <param name="leaderboardID"> 排行榜ID (Appstore后台创建的)</param>
 | ||
|     public void ReportScore(long score, string leaderboardID)
 | ||
|     {
 | ||
| #if UNITY_IPHONE && !UNITY_EDITOR
 | ||
|         Debug.Log("Reporting score " + score + " on leaderboard " + leaderboardID);
 | ||
|         Social.ReportScore(score, leaderboardID, success =>
 | ||
|         {
 | ||
|             if (success)
 | ||
|             {
 | ||
|                 Debug.Log("[LeaderBoards] Reported score successfully");
 | ||
|             }
 | ||
|             else
 | ||
|             {
 | ||
|                 Debug.Log("[LeaderBoards] Failed to report score");
 | ||
|             }
 | ||
|         });
 | ||
| #endif
 | ||
|     }
 | ||
| 
 | ||
|     /// <summary>
 | ||
|     /// 展示排行榜
 | ||
|     /// </summary>
 | ||
|     public void ShowLeaderboard()
 | ||
|     {
 | ||
| #if UNITY_IPHONE && !UNITY_EDITOR
 | ||
|         Social.ShowLeaderboardUI();
 | ||
| #endif
 | ||
|     }
 | ||
| } |