GooglePlayGame 服务在 Unity 中无法正常工作

GooglePlayGame services not working properly in Unity

我导入了 Google Play 游戏服务插件

然后使用一些教程来实现它

但是

仍然无法计算 成就排行榜 未显示

登录正常

但我不明白为什么这些不起作用

using System.Collections;
using System.Collections.Generic;
using Google;
using GooglePlayGames;
using GooglePlayGames.BasicApi;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.SocialPlatforms;

public class LeaderBoardController : MonoBehaviour
{
    public static LeaderBoardController instance;

    public const string leaderBoardId = "CgkIlY-bvcoNEAIQAQ";

    // Achievements ID
    public const string score5 = "CgkIlY-bvcoNEAIQAg";
    public const string passTheScore30 = "CgkIlY-bvcoNEAIQAw";
    public const string unlockTheGreenBird = "CgkIlY-bvcoNEAIQBQ";
    public const string unlockTheDawnLevel = "CgkIlY-bvcoNEAIQBg";
    public const string becomeAcenturian = "CgkIlY-bvcoNEAIQBw";
    public const string unlockTheRedBird = "CgkIlY-bvcoNEAIQCA";
    public const string unlockTheMountainsLevel = "CgkIlY-bvcoNEAIQCQ";
    public const string unlockTheLakeLevel = "CgkIlY-bvcoNEAIQCg";
    public const string unlockTheSpringLevel = "CgkIlY-bvcoNEAIQCw";
    public const string unlockTheFarmLevel = "CgkIlY-bvcoNEAIQDA";
    public const string unlockTheWinterLevel = "CgkIlY-bvcoNEAIQDQ";
    public const string unlockTheEveningLevel = "CgkIlY-bvcoNEAIQDg";
    public const string unlockTheDarkLevel = "CgkIlY-bvcoNEAIQDw";
    public const string unlockTheRainyLevel = "CgkIlY-bvcoNEAIQEA";
    public const string becomeTheUltimateFlappy = "CgkIlY-bvcoNEAIQEQ";

    void Awake ()
    {
        MakeSingleton ();
    }

    // Use this for initialization
    void Start ()
    {
        PlayGamesPlatform.Activate ();

    }

    void OnEnable ()
    {
        SceneManager.sceneLoaded += this.OnLoadCallBack;
    }

    void OnLoadCallBack (Scene scene, LoadSceneMode sceneMode)
    {
        ReportScoreLocal (GameController.instance.GetHighScore ());
        ReportProgressLocal (GameController.instance.GetHighScore ());
    }

    void OnDisable ()
    {
        SceneManager.sceneLoaded -= this.OnLoadCallBack;
    }

    // making C# script singleton
    void MakeSingleton ()
    {
        if (instance != null)
        {
            Destroy (gameObject);
        }
        else
        {
            instance = this;
            DontDestroyOnLoad (gameObject);
        }
    }

    public void ConnectGooglePlayGames ()
    {
        if (Social.localUser.authenticated)
        {
            PlayGamesPlatform.Instance.ShowAchievementsUI ();
        }
        else
        {
            Social.localUser.Authenticate ((bool success) =>
            {

            });
        }
    }

    public void OpenLeaderBoard ()
    {
        if (Social.localUser.authenticated)
        {
            PlayGamesPlatform.Instance.ShowLeaderboardUI (leaderBoardId);
        }
    }

    void ReportScoreLocal (int score)
    {
        if (Social.localUser.authenticated)
        {
            Social.ReportScore (score, leaderBoardId, (bool success) =>
            {

            });
        }
    }

    void ReportProgressLocal (int score)
    {
        if (Social.localUser.authenticated)
        {
            if (score >= 5 && score < 30)
            {
                Social.ReportProgress (score5, (double) score, (bool success) =>
                {

                });
            }

        }
    }
}

添加成就后是否更新Android设置?还是在 Google 播放服务仪表板中重新发布更改?

另外,改变

PlayGamesPlatform.Instance.ShowAchievementsUI ();

Social.ShowAchievementsUI ();

并改变

PlayGamesPlatform.Instance.ShowLeaderboardUI (leaderBoardId);

Social.ShowLeaderboardUI();

有关详细信息,请参阅以下链接:

Google Play Games Services Tutorial (Unity) #1 - ACHIEVEMENTS and LEADERBOARDS

Achievements & Leaderboard - Google Play Services - Subway Skater - 23

希望对你有帮助:)

为了使我的代码正常工作,我必须根据 @Ehsan Mohammadi 的 建议更改一些代码。

首先修改启动方法

void Start ()
{
    PlayGamesPlatform.Activate ();
}

void Start ()
{
    PlayGamesClientConfiguration config = new PlayGamesClientConfiguration.Builder ().Build ();
    PlayGamesPlatform.InitializeInstance (config);
    PlayGamesPlatform.Activate ();
}

更改以下语句

PlayGamesPlatform.Instance.ShowAchievementsUI ();

Social.ShowAchievementsUI ();

PlayGamesPlatform.Instance.ShowLeaderboardUI (leaderBoardId);

Social.ShowLeaderboardUI();

进行这些更改后,我的代码工作正常。