I am currently making a a leaderboard feature for my Mac OS X Unity game. I first tried Playfab but I kept getting an error saying "PlayFabException: Must be logged in to call this method PlayFab". I can't find a way to resolve this.
I have 2 scripts that does this, this is the code for the PlayFabManger script:
`using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using PlayFab;
using PlayFab.ClientModels;
using System;
public class PlayFabManager : MonoBehaviour
//public static PlayFabManager instance;
// Start is called before the first frame update
void Start()
{
//instance = this;
if (string.IsNullOrEmpty(PlayFabSettings.staticSettings.TitleId))
{
/*
Please change the titleId below to your own titleId from PlayFab Game Manager.
If you have already set the value in the Editor Extensions, this can be skipped.
*/
PlayFabSettings.staticSettings.TitleId = "F9F3D";
}
Login();
}
// Update is called once per frame
void Update()
{
}
void Login()
{
var request = new LoginWithCustomIDRequest
{
CustomId = SystemInfo.deviceUniqueIdentifier,
CreateAccount = true
};
PlayFabClientAPI.LoginWithCustomID(request, OnSuccess, OnError);
}
//private void OnLoginSuccess(LoginResult result)
//{
// //>> Call Client API here <<
// var getStoreItemsRequest = new GetStoreItemsRequest { StoreId = "[YourStoreId]" };// Please change this value to your own storeId from PlayFab Game Manager
// PlayFabClientAPI.GetStoreItems(getStoreItemsRequest, OnGetSuccess, OnError);
//}
void OnSuccess(LoginResult result)
{
print("Successful login create");
}
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)
{
print("Successful leaderboard sent");
}
void OnError(PlayFabError error)
{
print("Error while logging in/creating account!");
print(error.GenerateErrorReport());
}
public void GetLeaderBoard()
{
var request = new GetLeaderboardRequest
{
StatisticName = "PlatformScore",
StartPosition = 0,
MaxResultsCount = 10
};
PlayFabClientAPI.GetLeaderboard(request, OnLeaderboardGet, OnError);
}
private void OnLeaderboardGet(GetLeaderboardResult result)
{
foreach (var item in result.Leaderboard)
{
print(item.Position + " " + item.PlayFabId + " " + item.StatValue);
}
}
}
`
I also have a line of code in another script that is called at the start method and refers to the above script, where I pass in a Playerprefs.GetInt variable:
playFabManager.SendLeaderBoard(PlayerPrefs.GetInt("TtlPoints"));
Does anyone have any idea on resolving this error? Is there an easier way to achieve this leaderboard feature on Mac OS X using other extensions such as firebase or pubnub?
Sorry for my English, looking forward to hearing from you.
PubNub awesome, Beamable uses them for fabs leaderboards for Unity! Check them out.