Google Play Games plugin for Unity:
https://github.com/playgameservices/play-games-plugin-for-unity#paste-the-game-resources-into-the-plugin-setup-dialog
Setting Up Google Play Games Services:
https://developers.google.com/games/services/console/enabling
准备
确保你的Android SDK中包含以下内容:
• Google Play Services
• Android Support Library
• Local Maven repository for Support Libraries (Also known as Android Support Repository)
• Google Repository
• Android 6.0 (API 23) (this does not affect the min SDK version).
使用Android SDK Manager 来确认你的SDK内容
如果下载过慢,可以尝试使用以下站点:
导入SDK
1.将GooglePlayGamesPlugin-0.9.42.unitypackage文件导入到你的项目中
导入项目后请务必至少修改一次包名(Package Name),确保你已看到此界面:
*如果已经是正式包名,则添加任意字符,Resolving 结束后再改回正式包名,
正式包名必须与你在后台填写的包名一致。
*如果Resolving结束后弹出以下提示,请选择NO
*确保Resolving结束后Plugins > Android目录下存在此文件 gpgs-plugin-support-0.9.42
2.导入后菜单栏将出现SDK选项,选择Window > Google Play Games > Setup > Android Setup
面板中
directory to save constants以及Constants class name使用默认值即可
Resources Definition 在后台中:
你还需要从后台生成Client ID 并填入。
使用
这里我们只使用到了排行榜功能:
1.AndroidManifest文件中需加入以下代码
2.实现登录以及排行榜的方法,包括登录、注销、上传分数以及显示排行榜:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using GooglePlayGames;
using UnityEngine.SocialPlatforms;
///
/// Google play manager.
/// create by liufeng on 18-01-11
///
public static class GooglePlayManager {
public delegate void GPDelegate(bool success,string uname);
static GPDelegate authenticatingCallback = null;
///
/// 初始化SDK
///
public static void Init () {
PlayGamesPlatform.Activate();
}
///
/// 是否登录
///
/// The authenticated.
public static bool Authenticated(){
return Social.localUser.authenticated;
}
///
/// 登录
///
/// The authenticating.
/// 回调
public static void Authenticating(GPDelegate cb = null){
authenticatingCallback = cb;
Social.localUser.Authenticate((bool success) =>
{
if (success)
{
Debug.Log("Authentication success : " + Social.localUser.userName);
}
else
{
Debug.Log("Authentication failed");
}
if (cb != null) cb(success, Social.localUser.userName);
});
}
///
/// 注销
///
public static void SignOut(){
((PlayGamesPlatform)Social.Active).SignOut();
}
///
/// 上传分数
///
/// 分数.
/// 排行榜id.
public static void PostScore(int scores,string lbid){
if(!Authenticated()){
Debug.Log("没有登录");
return;
}
Social.ReportScore(scores,lbid, (bool success) => {
// handle success or failure
Debug.Log("post score : " + success);
});
}
///
/// 显示排行榜
///
/// 排行榜id,传空字符串则显示全部排行榜
public static void ShowLeaderboard(string lbid = ""){
if (!Authenticated())
{
Debug.Log("没有登录");
return;
}
if(lbid == ""){
Social.ShowLeaderboardUI();
}else{
PlayGamesPlatform.Instance.ShowLeaderboardUI(lbid);
}
}
}
GooglePlayTest为测试方法,将此类放到任意场景中,可快速测试SDK的各项功能是否正确
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GooglePlayTest : MonoBehaviour {
const int height = 70;
const int width = 200;
// Use this for initialization
void Start () {
GooglePlayManager.Init();
}
int high;
void OnGUI()
{
high = 10;
if (CreateBtn("登录"))
{
GooglePlayManager.Authenticating(AuthenticatingCallBack);
}
if (CreateBtn("注销"))
{
GooglePlayManager.SignOut();
}
if (CreateBtn("上传分数"))
{
GooglePlayManager.PostScore(123,"CgkImoOim_EUEAIQAQ");
}
if (CreateBtn("显示排行榜"))
{
GooglePlayManager.ShowLeaderboard("CgkImoOim_EUEAIQAQ");
}
}
public bool CreateBtn(string btnname)
{
bool b = GUI.Button(new Rect(Screen.width / 2 - width / 2, high, width, height), btnname);
high += height + 5;
return b;
}
public void AuthenticatingCallBack(bool success, string uname)
{
if(success){
NGUIDebug.Log("Authenticating success : " + uname);
Debug.Log("Authenticating success : " + uname);
}else{
NGUIDebug.Log("Authenticating failed " );
Debug.Log("Authenticating failed " );
}
}
}
这里可以在应用商店中搜索"google框架安装器",大部分商店中都有,安装后就可以使用google play服务了。