GPG Tutorial – Achievements and Leaderboards (Code)

This post contains all the code that’s been written in the YouTube video.

You can read it at your own pace, although watching the video prior to looking at this code example is recommended.

 

PlayGamesScript.cs

using GooglePlayGames;
using GooglePlayGames.BasicApi;
using UnityEngine;

public class PlayGamesScript : MonoBehaviour {

	// Use this for initialization
	void Start () {
        PlayGamesClientConfiguration config = new PlayGamesClientConfiguration.Builder().Build();
        PlayGamesPlatform.InitializeInstance(config);
        PlayGamesPlatform.Activate();

        SignIn();
	}

    void SignIn()
    {
        Social.localUser.Authenticate(success => { });
    }

    #region Achievements
    public static void UnlockAchievement(string id)
    {
        Social.ReportProgress(id, 100, success => { });
    }

    public static void IncrementAchievement(string id, int stepsToIncrement)
    {
        PlayGamesPlatform.Instance.IncrementAchievement(id, stepsToIncrement, success => { });
    }

    public static void ShowAchievementsUI()
    {
        Social.ShowAchievementsUI();
    }
    #endregion /Achievements

    #region Leaderboards
    public static void AddScoreToLeaderboard(string leaderboardId, long score)
    {
        Social.ReportScore(score, leaderboardId, success => { });
    }

    public static void ShowLeaderboardsUI()
    {
        Social.ShowLeaderboardUI();
    }
    #endregion /Leaderboards

}

 

ManagerScript.cs

using UnityEngine;

public class ManagerScript : MonoBehaviour {

    public static ManagerScript Instance { get; private set; }
    public static int Counter { get; private set; }

	// Use this for initialization
	void Start () {
        Instance = this;
	}

    public void IncrementCounter()
    {
        Counter++;
        UIScript.Instance.UpdatePointsText();
    }

    public void RestartGame()
    {
        PlayGamesScript.AddScoreToLeaderboard(GPGSIds.leaderboard_leaderboard, Counter);
        Counter = 0;
        UIScript.Instance.UpdatePointsText();
    }
	
}

 

UIScript.cs

using UnityEngine;
using UnityEngine.UI;

public class UIScript : MonoBehaviour {

    public static UIScript Instance { get; private set; }

	// Use this for initialization
	void Start () {
        Instance = this;
	}

    [SerializeField]
    private Text pointsTxt;

    public void GetPoint()
    {
        ManagerScript.Instance.IncrementCounter();
    }

    public void Restart()
    {
        ManagerScript.Instance.RestartGame();
    }

    public void Increment()
    {
        PlayGamesScript.IncrementAchievement(GPGSIds.achievement_incremental_achievement, 5);
    }

    public void Unlock()
    {
        PlayGamesScript.UnlockAchievement(GPGSIds.achievement_standard_achievement);
    }

    public void ShowAchievements()
    {
        PlayGamesScript.ShowAchievementsUI();
    }

    public void ShowLeaderboards()
    {
        PlayGamesScript.ShowLeaderboardsUI();
    }

    public void UpdatePointsText()
    {
        pointsTxt.text = ManagerScript.Counter.ToString();
    }
}

 

About the author 

Matt Rešetár

Matt is an app developer with a knack for teaching others. Working as a freelancer and most importantly developer educator, he is set on helping other people succeed in their Flutter app development career.

You may also like

  • Hello,
    I have done everything as you said and built apk and released app as alpha. But when click on Leaderboard button nothing happens.But my game is listed in “Play Games app of google” and there you can play actual game.I tried scoring some value but leaderboard didn’t added it..I don’t what went wrong.Please help me solve this issue.

  • thank you

    but i have Problam

    Assets/ManagerScript.cs(21,49): error CS0117: `GPGSIds’ does not contain a definition for `leaderboard_leaderboard’

    Assets/UIScript.cs(28,48): error CS0117: `GPGSIds’ does not contain a definition for `achievement_incremental_achievement’

    Assets/UIScript.cs(33,45): error CS0117: `GPGSIds’ does not contain a definition for `achievement_standard_achievement’

    can you help me

  • anybody getting errors like: ‘GPGSIds’ does not contain a definition for ‘leaderboard_leaderboard’
    ?

  • {"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}
    >