Sending leaderboard in C# Unity to Playfab

293 views Asked by At

I have problem in making leaderboard with playfab, I don't know how to Sending my game's score to leaderboard in Playfab

I am not professional at c# and thats why I had to watch Coco Code Youtube to know how to make leaderboard in easy way.

my codes:

this is my score code that allows me to get score number every time move from wall score:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class score : MonoBehaviour
{

public static int Score = 0;

void Start()
{
    Score = 0;

}

void Update()
{
    GetComponent<UnityEngine.UI.Text>().text = Score.ToString();
}
}

and this is my playfab codes PlayFabManager:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using PlayFab;
using PlayFab.ClientModels;

public class PlayFabManager : MonoBehaviour
{

void Start()
{

    Login();
}

void Login()
{
    var request = new LoginWithCustomIDRequest
    {
        CustomId = SystemInfo.deviceUniqueIdentifier,
        CreateAccount = true
    };
    PlayFabClientAPI.LoginWithCustomID(request, OnSuccess, OnError);
}

void OnSuccess(LoginResult result)
{
    Debug.Log("Success login/account create!");
}

void OnError(PlayFabError error)
{
    Debug.Log("Error while logging in/creating account!");
    Debug.Log(error.GenerateErrorReport());
}

public void SendLeaderboard(int score)
{
    var request = new UpdatePlayerStatisticsRequest
    {
        Statistics = new List<StatisticUpdate>
        {
            new StatisticUpdate
            {
                StatisticName = "PlatFormScore",
                Value = score
            }

        }
    };
    PlayFabClientAPI.UpdatePlayerStatistics(request, OnLeaderboardUpdate, OnError);
}
void OnLeaderboardUpdate(UpdatePlayerStatisticsResult result)
{
    Debug.Log("Successfull leaderboard send");
}


}
1

There are 1 answers

0
Maryam On

Go to Playfab website --> Get into your game studio --> click settings icon there --> got to "title setting" --> click on tab API features --> from heading ENABLE API FEATURES--> enable "Allow client to enable statistics" --> save