谷歌排行榜接入————最白话,手把手教你做系列。

谷歌排行榜看了很多教程,大部分人只提了重点的:初始化,提交分数,显示排行榜的几个方法,很少有完整的可以直接导入项目直接使用的。而且只能够做个本地排序上传一个最高分。对于我这样的小白当然是选择&()&…&*%&%¥%¥%¥…&
这里就给个Unity里接入谷歌排行的详细步骤。
接入谷歌排行榜。
仍然分为两个部分。
一,环境配置部分。
1,GooglePlayServices接入下载地址
2,导入下载的.Unitypage插件进Unity。
这里写图片描述
导入插件后出现以下文件,不同版本会有细微差别。比如
0.9.50版本插件
这里写图片描述
和0.9.42插件

这里写图片描述
3,配置谷歌服务。
谷歌游戏服务不支持ios,0.9.50完全移除ios配置。
在unity中Windows下面这个位置。
0.9.50版本
这里写图片描述
0.9.42版本
这里写图片描述
如果你的配置出现的不可点击的问题。以下情况:
这里写图片描述
则需要把你的buildsetting里面的环境切换成android。
点击Android setup 出现以下页面。
这里写图片描述
把你得googleplay上配置的排行文件 和web app client id填入对应位置。
配置文件申请不再赘述,需要注册谷歌开发者账号25$,教程地址
然后点击Setup。
得到以下提示就Ok了。
这里写图片描述
以上第一步结束,环境配置完成。

二,代码部分。

public class Leaderboard : MonoBehaviour
{

    public static Leaderboard instance;
    //相关应排行榜id,可根据自己需求定义。
    public string endless_bestkill="CgkIjerK29sZEAIQAQ";
    public string endless_hilevel="CgkIjerK29sZEAIQBQ";
    public string warfog_bestkill="CgkIjerK29sZEAIQAg";
    public string warfog_hilevel="CgkIjerK29sZEAIQBg";
    public string warfog_survival="CgkIjerK29sZEAIQBA";
    public string hiarea_bestkill="CgkIjerK29sZEAIQAw";
    public string hiarea_hilevel="CgkIjerK29sZEAIQBw";

    protected bool isLogged;

    void Awake()
    {
        if (instance != null)
        {
            Destroy(gameObject);
            return;
        }

        DontDestroyOnLoad(gameObject);

        instance = this;
        PlayGamesClientConfiguration config = new PlayGamesClientConfiguration.Builder()
            // requests an ID token be generated.  This OAuth token can be used to
            //  identify the player to other services such as Firebase.
            .RequestIdToken()
            .Build();
        PlayGamesPlatform.InitializeInstance(config);
        // 查看Google服务输出信息 recommended for debugging:
        PlayGamesPlatform.DebugLogEnabled = true;
        // Activate the Google Play Games platform
        //激活谷歌服务 
        PlayGamesPlatform.Activate();
        //判断用户是否登录
        SignIn();
        Debug.LogError("google play servers sign in");
    }

    // Use this for initialization
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {

    }

    void SignIn()
    {
        // 认证用户,判定用户是否登录:
        Social.localUser.Authenticate((bool success) =>
        {
            if (success)
            {
                isLogged = true;
            }
            else
            {
                isLogged = false;
            }
        });
    }

    //取本地的HighScore与对应排行榜线上的分数作比较时,使用此方法,googlePlayLeaderboardID为排行榜ID
    public void reportScore(long HighScore,string googlePlayLeaderboardID)
    {
        if (!isLogged) {
            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");
                }
            });
    }
    //在本地已经做了排序处理,只提交自己需要提交的相关分数时 使用此方法,leaderboardIdToUse为对应排行榜ID
    public void reportScore(long score)
    {
        try
        {
            Social.ReportScore(score, leaderboardIdToUse, (bool success) =>
            {

            });
        }
        catch
        {

        }
    }


    //显示排行榜
    public void showLeaderboard()
    {

     //显示对应ID排行榜
        //PlayGamesPlatform.Instance.ShowLeaderboardUI(googlePlayLeaderboardID);
        //显示所有排行榜
        Social.ShowLeaderboardUI();
    }

    //解锁一个成就 对应地方引用该方法即可解锁该成就
    public static void UnlockAnAchievement(string _achievementID)
    {


        Social.ReportProgress(_achievementID, 100.0f, (bool success) =>
        {

        });
    }


    //显示成就页面
    public static void ShowAchievementUI()
    {
        Social.ShowAchievementsUI();
    }

}

代码成就ID填写自己的排行榜ID即可, 提交分数可选择自己对应的reportScore方法提交分数。显示排行榜只用调用showLeaderboard方法即可。
排行榜是谷歌提供的页面,不用自己做对应的UI,成就的图标也可以在Google后台做控制。
以上。

发布了27 篇原创文章 · 获赞 26 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/qq_39860954/article/details/79229824