unity 排行榜功能
1. GameCenter
2. Googleplay
下载插件:
https://github.com/playgameservices/play-games-plugin-for-unity
unity激活:
IEnumerator Start()
{
#if UNITY_ANDROID
GooglePlayGames.PlayGamesPlatform.Activate();
#endif
yield return 0;
Social.localUser.Authenticate(HandleAuthenticated);
}
3. unity的设置和代码
using System.Collections;
using UnityEngine.SocialPlatforms;
using UnityEngine;
using System;
public class RankingManager : MonoBehaviour
{
#if UNITY_ANDROID
public const string ChallengeRankingID = "CgkIhoXyrZ0fEAIQAQ";
public const string TimeLineRankingID = "CgkIhoXyrZ0fEAIQAg";
#else
public const string ChallengeRankingID = "Challenge";
public const string TimeLineRankingID = "Timing";
#endif
public static RankingManager Instance;
/// <summary>
/// 是否初始化成功
/// </summary>
public bool GameCenterState;
public IUserProfile showUser;
public IScore showScore;
/// <summary>
/// 用户数据
/// </summary>
private string userInfo;
/// <summary>
/// 挑战玩法排行榜
/// </summary>
private ILeaderboard ChallengeLB;
/// <summary>
/// 限时玩法
/// </summary>
private ILeaderboard TimingLB;
private bool ChallengeLBloading = false;
private bool TimingLBloading = false;
/// <summary>
/// 初始化
/// </summary>
void Awake()
{
Instance = this;
}
/// <summary>
///
/// </summary>
IEnumerator Start()
{
#if UNITY_ANDROID
GooglePlayGames.PlayGamesPlatform.Activate();
#endif
yield return 0;
Social.localUser.Authenticate(HandleAuthenticated);
}
/// <summary>
/// 初始化 GameCenter 结果回调函数
/// </summary>
/// <param name="success">If set to <c>true</c> success.</param>
private void HandleAuthenticated(bool success)
{
GameCenterState = success;
}
/// <summary>
/// 打开排行榜
/// </summary>
public void ShowLeaderboardUI()
{
if (Social.localUser.authenticated)
{
Social.ShowLeaderboardUI();
}
}
/// <summary>
/// 上传分数
/// </summary>
/// <param name="score"> 分数 </param>
/// <param name="board"> 排行榜ID </param>
/// <param name="callback"> 上传成功返回 </param>
public void ReportScore(long score, string boardID, Action<bool> callback)
{
if (Social.localUser.authenticated)
{
Social.ReportScore(score, boardID, callback);
}
}
}