谷歌排行榜看了很多教程,大部分人只提了重点的:初始化,提交分数,显示排行榜的几个方法,很少有完整的可以直接导入项目直接使用的。 既然我已经做好,并且项目需要整理技术文档,索性就搞一篇文章 废话不说 安卓篇后续补IOS 一,环境配置部分。 1,GooglePlayServices接入下载地址 2,导入下载的.Unitypage插件进Unity。  有导入插件经验的就知道有时候会报错,问题不大,都好解决 有这两个主文件夹就行  3,配置谷歌服务。 谷歌游戏服务不支持ios,0.9.50完全移除ios配置。 在unity中Windows下面这个位置。  打开以后是这样的 按下面的图去配置  把你得googleplay上配置的排行文件 和web app client id填入对应位置。 配置文件申请不再赘述,需要注册谷歌开发者账号25$,教程地址 然后点击Setup。 得到以下提示就Ok了。 
下面脚本直接全选复制创建一个Leaderboard名字的C#脚本用的时候先 Leaderboard.Instance.Init();然后就可以用单例调用了,代码里的注释写的很完整,不会的仔细看注释
using System;
using UnityEngine;
using UnityEngine.SocialPlatforms;
using System.Collections.Generic;
using GooglePlayGames;
using GooglePlayGames.BasicApi;
#if UNITY_ANDROID && GOOGLE_PLAY
using GooglePlayGames;
using GooglePlayGames.BasicApi;
#endif
public class Leaderboard : MonoSingleton<Leaderboard>
{
private void Awake()
{
#if UNITY_ANDROID && GOOGLE_PLAY
try
{
if (Application.internetReachability == NetworkReachability.NotReachable)
{
return;
}
PlayGamesClientConfiguration config = new PlayGamesClientConfiguration.Builder()
.Build();
PlayGamesPlatform.InitializeInstance(config);
PlayGamesPlatform.Activate();
SignIn();
}
catch (Exception e)
{
Debug.Log(e.ToString());
}
#endif
}
void SignIn()
{
#if UNITY_EDITOR
m_IsLogin=true;
#else
DoLogin(null);
#endif
}
bool m_IsLogin = false;
private void DoLogin(Action<bool> callback)
{
Action<bool> DidSocialLogin = (bool success) =>
{
if (success)
{
Debug.Log("Authentication successful");
string userInfo = "Username: " + Social.localUser.userName +
"\nUser ID: " + Social.localUser.id +
"\nIsUnderage: " + Social.localUser.underage;
Debug.Log(userInfo);
m_IsLogin = true;
#if UNITY_ANDROID && GOOGLE_PLAY
((GooglePlayGames.PlayGamesPlatform)Social.Active).SetGravityForPopups(Gravity.BOTTOM);
#endif
if (callback != null)
callback(success);
}
else
{
m_IsLogin = false;
Debug.Log("Authentication failed");
}
};
try
{
Action<SignInStatus> DidSocialLogin1 = (SignInStatus success) =>
{
Debug.LogError("unity load:"+ success);
switch (success)
{
case SignInStatus.Success:
Debug.Log("Authentication successful");
string userInfo = "Username: " + Social.localUser.userName +
"\nUser ID: " + Social.localUser.id +
"\nIsUnderage: " + Social.localUser.underage;
Debug.Log(userInfo);
m_IsLogin = true;
#if UNITY_ANDROID && GOOGLE_PLAY
((GooglePlayGames.PlayGamesPlatform)Social.Active).SetGravityForPopups(Gravity.BOTTOM);
#endif
if (callback != null)
callback(true);
break;
case SignInStatus.UiSignInRequired:
break;
case SignInStatus.DeveloperError:
break;
case SignInStatus.NetworkError:
break;
case SignInStatus.InternalError:
break;
case SignInStatus.Canceled:
break;
case SignInStatus.AlreadyInProgress:
break;
case SignInStatus.Failed:
break;
case SignInStatus.NotAuthenticated:
break;
default:
break;
}
};
#if UNITY_ANDROID && GOOGLE_PLAY
PlayGamesPlatform.Instance.Authenticate(SignInInteractivity.NoPrompt, DidSocialLogin1);
#else
#endif
}
catch (Exception e)
{
Debug.Log(e.ToString());
}
}
public void ShowTotalLeaderboard(string id, Action<UIStatus> callback)
{
#if UNITY_ANDROID && GOOGLE_PLAY
PlayGamesPlatform.Instance.ShowLeaderboardUI(id, callback);
#endif
#if UNITY_IOS
#endif
}
public void ShowTotalLeaderboard()
{
Social.ShowLeaderboardUI();
}
ILeaderboard m_Leaderboard;
public void DoLeaderboard(string boardId)
{
DoLogin((success) =>
{
try
{
Debug.Log("Show Leaderboard");
#if UNITY_ANDROID && GOOGLE_PLAY
PlayGamesPlatform.Instance.ShowLeaderboardUI(boardId);
#endif
#if UNITY_IOS
UnityEngine.SocialPlatforms.GameCenter.GameCenterPlatform.ShowLeaderboardUI(boardId, UnityEngine.SocialPlatforms.TimeScope.AllTime);
#endif
ReportScore(DataManager.mChristmasData.SnowNum);
}
catch (Exception e)
{
Debug.Log(e.ToString());
}
});
}
void DidLoadLeaderboard(bool result)
{
Debug.Log("Received " + m_Leaderboard.scores.Length + " scores");
foreach (IScore score in m_Leaderboard.scores)
Debug.Log(score);
}
public void ReportScore(string leaderboard, long score)
{
Debug.Log("Report Score上传分数:" + leaderboard+"="+ score);
#if UNITY_ANDROID
if (m_IsLogin)
{
Social.ReportScore(score, leaderboard, (ret) =>
{
if (ret)
{
Debug.Log("Report Score Successs");
}
});
}
#else
#endif
}
public void ReportScore(long score)
{
#if UNITY_ANDROID
ReportScore(ConstantData.Rank_String_Android, score);
#else
ReportScore(ConstantData.Rank_String_IOS, score);
#endif
}
public float RefHighScoreById( string googlePlayLeaderboardID, int num = 1, Action<List<IScore>> result=null)
{
if (!m_IsLogin)
{
return 0;
}
#if UNITY_ANDROID && GOOGLE_PLAY
PlayGamesPlatform.Instance.LoadScores(
googlePlayLeaderboardID,
LeaderboardStart.TopScores,
num,
LeaderboardCollection.Public,
LeaderboardTimeSpan.AllTime,
(data) =>
{
if (!data.Valid)
return;
string myScores = "PlayGamesPlatform Leaderboard:\n";
List<IScore> userIDList = new List<IScore>();
foreach (var score in data.Scores)
{
if (score.rank <= num)
{
userIDList.Add(score);
}
myScores += "\t" + score.userID + " " + score.formattedValue + " " + score.date + "\n";
}
if (userIDList.Count > 0)
{
result(userIDList);
}
Debug.Log(myScores);
});
#else
#endif
return 0;
}
#if UNITY_ANDROID && GOOGLE_PLAY
public void reportScore(long HighScore, string googlePlayLeaderboardID)
{
if (!m_IsLogin)
{
Debug.LogError("isLogged or not");
return;
}
PlayGamesPlatform.Instance.LoadScores(
googlePlayLeaderboardID,
LeaderboardStart.PlayerCentered,
1,
LeaderboardCollection.Public,
LeaderboardTimeSpan.AllTime,
(data) =>
{
if (data.Valid)
{
int score = (int)data.PlayerScore.value;
Debug.LogError("score from inside leaderboard: " + data.PlayerScore.value);
Debug.LogError("formated score from inside leaderboard: " + data.PlayerScore.formattedValue);
if (score < HighScore)
{
Social.ReportScore(HighScore, googlePlayLeaderboardID, (bool success) =>
{
});
}
}
else
{
Debug.LogError("data invalid in leaderboard");
}
});
}
public void Get(string googlePlayLeaderboardID)
{
ILeaderboard lb = PlayGamesPlatform.Instance.CreateLeaderboard();
lb.id = googlePlayLeaderboardID;
lb.LoadScores(ok =>
{
if (ok)
{
Debug.Log(lb);
}
else
{
Debug.Log("Error retrieving leaderboardi");
}
});
}
void GetNextPage(LeaderboardScoreData data)
{
PlayGamesPlatform.Instance.LoadMoreScores(data.NextPageToken, 10,
(results) =>
{
});
}
#else
#endif
}
代码成就ID填写自己的排行榜ID即可, 提交分数可选择自己对应的reportScore方法提交分数。显示排行榜只用调用showLeaderboard方法即可。 排行榜是谷歌提供的页面,不用自己做对应的UI,成就的图标也可以在Google后台做控制。
没有MonoSingleton这个类的我放下面了
using UnityEngine;
public class MonoSingleton<T> : MonoBehaviour where T : MonoSingleton<T>
{
private static T _instance;
private static object _lock = new object();
private static bool _isInitialized = false;
public static T Instance
{
get
{
lock (_lock)
{
if (_instance == null)
{
_instance = (T)FindObjectOfType(typeof(T));
if (_instance == null)
{
GameObject singleton = new GameObject();
_instance = singleton.AddComponent<T>();
singleton.name = typeof(T).ToString() + "(Singleton) ";
DontDestroyOnLoad(singleton);
Debug.Log("[Singleton] An instance of " + typeof(T) +
" is needed in the scene, so '" + singleton +
"'was created with DontDestroyOnLoad.");
}
else
{
Debug.Log("[Singleton] Using instance already created: " +
_instance.gameObject.name);
}
if (!_isInitialized)
{
_isInitialized = true;
_instance.Init();
}
}
return _instance;
}
}
}
private void Awake()
{
if (_instance == null)
{
_instance = this as T;
}
else if (_instance != this)
{
Debug.LogError("Another instance of " + GetType() + " is already exist! Destroying self...");
DestroyImmediate(this);
return;
}
if (!_isInitialized)
{
DontDestroyOnLoad(gameObject);
_isInitialized = true;
_instance.Init();
}
}
protected virtual void OnDestroy()
{
lock (_lock)
{
_instance = null;
}
}
public virtual void Init(){ }
}
最近在搞Google应用升级Android 11的问题 一直权限拒审 真是服
|