1

我目前正在为我的 Mac OS X Unity 游戏制作排行榜功能。我首先尝试了 Playfab,但一直收到错误消息“PlayFabException:必须登录才能调用此方法 PlayFab”。我找不到解决此问题的方法。

我有 2 个执行此操作的脚本,这是 PlayFabManger 脚本的代码:

`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);
        }
    }

    }

`

我在另一个脚本中也有一行代码,它在 start 方法中调用并引用上面的脚本,我在其中传入了一个 Playerprefs.GetInt 变量: playFabManager.SendLeaderBoard(PlayerPrefs.GetInt("TtlPoints"));

有人对解决此错误有任何想法吗?是否有更简单的方法可以使用其他扩展(如 firebase 或 pubnub)在 Mac OS X 上实现此排行榜功能?

对不起我的英语,期待您的回音。

4

2 回答 2

2

PubNub 很棒,Beamable 将它们用于 Unity 的晶圆厂排行榜!去看一下。

于 2021-03-11T13:55:15.933 回答
1

我会接受 Firebase 的答案。

如果您采用 Firebase 路线,则没有现成的排行榜解决方案。有一个示例开源存储库,您可以使用它在实时数据库上实现排行榜,实现起来应该相对简单。

您的第二个问题是,虽然实时数据库确实可以在桌面上运行(特别是排行榜并没有给我任何问题),但它目前是一个测试版功能,仅供在开发期间使用。如果您提交了相关的错误,团队将努力修复它,但它可能会优先于任何移动功能。您可以直接使用REST API来实现这一切,但此时 Unity 官方示例和文档将不适用。

于 2021-03-15T20:49:10.063 回答