0

我有一种使用 Gamespark Api 获取排行榜数据的方法。

我想将该数据返回到另一个类以查看和更新​​ Unity 预制件。

从我的排行榜类中,我可以很好地循环数据,但我很困惑我需要什么返回类型才能使用来自另一个类的相同循环代码

public class LeaderboardManager : MonoBehaviour
{
public GSEnumerable<LeaderboardDataResponse._LeaderboardData> LeaderboardRequest (string leaderboard, int entryCount = 50, bool social = false)
{
    GSEnumerable<LeaderboardDataResponse._LeaderboardData> data = null;

    new LeaderboardDataRequest()
        .SetLeaderboardShortCode(leaderboard)
        .SetEntryCount(entryCount)
        .SetSocial(social)
        .Send((response) => {
            if (!response.HasErrors)
            {
                Debug.Log("Found Leaderboard Data...");
                foreach (LeaderboardDataResponse._LeaderboardData entry in response.Data)
                {
                    int rank = (int)entry.Rank;
                    string playerName = entry.UserName;
                    string score = entry.JSONData["SCORE"].ToString();
                    Debug.Log("Rank:" + rank + " Name:" + playerName + " \n Score:" + score);
                }
                return response;
            }
            else
            {
                Debug.Log("Error Retrieving Leaderboard Data...");
                return;
            }
    });

    return data;
}

}

public class StartController : MonoBehaviour {

#region Variables

public string leaderboardName;

private GSEnumerable<LeaderboardDataResponse._LeaderboardData> leaderboardResults;

    void Start () {
        leaderboardResults = NetworkManager.Instance.leaderboard.LeaderboardRequest(leaderboardName, 10, true);

        foreach (LeaderboardDataResponse._LeaderboardData entry in leaderboardResults.Data)
        {
            int rank = (int)entry.Rank;
            string playerName = entry.UserName;
            string score = entry.JSONData["SCORE"].ToString();
            Debug.Log("Rank:" + rank + " Name:" + playerName + " \n Score:" + score);
        }

    }

我得到的错误是leaderboardResults.DataStartController类 GSEnumerable 的 foreach 中不包含 Data 的定义并且没有可访问的扩展名。

在印刷品foreach中还可以LeaderboardManager

我试图返回response.DataasArrayList并且在return data; Anonymous function converted to void returning delegate cannot return value

public ArrayList LeaderboardRequest (string leaderboard, int entryCount = 50, bool social = false)
{
    //GSEnumerable<LeaderboardDataResponse._LeaderboardData> data = null;

    new LeaderboardDataRequest()
        .SetLeaderboardShortCode(leaderboard)
        .SetEntryCount(entryCount)
        .SetSocial(social)
        .Send((response) => {
            if (!response.HasErrors)
            {
                Debug.Log("Found Leaderboard Data...");
                foreach (LeaderboardDataResponse._LeaderboardData entry in response.Data)
                {
                    int rank = (int)entry.Rank;
                    string playerName = entry.UserName;
                    string score = entry.JSONData["SCORE"].ToString();
                    Debug.Log("Rank:" + rank + " Name:" + playerName + " \n Score:" + score);
                }
                GSEnumerable<LeaderboardDataResponse._LeaderboardData> data = response.Data;
                return data;
            }
            else
            {
                Debug.Log("Error Retrieving Leaderboard Data...");
                return;
            }
    });

    return null;
}
4

1 回答 1

0

好的,那么还有另一种解决方案。您可以创建静态动作:

public static Action<LeaderboardDataResponse> GetResponce;

然后在 LeaderboardDataRequest 调用这个动作

new LeaderboardDataRequest()
    .SetLeaderboardShortCode(leaderboard)
    .SetEntryCount(entryCount)
    .SetSocial(social)
    .Send((response) => {
        GetResponce?.Invoke(response);
        //Do something if you need here
});

在您的脚本中开始订阅操作

void Start()
{
     LeaderboardManager.GetResponce += GotData;
}

和 GotData 函数:

public void GotData(LeaderboardDataResponse response)
{
    if (!response.HasErrors)
    {
        foreach (var entry in response.Data)
        {
            int rank = (int)entry.Rank;
            string playerName = entry.UserName;
            string score = entry.JSONData["SCORE"].ToString();
            Debug.Log("Rank:" + rank + " Name:" + playerName + " \n Score:" + score);
        }
    }
    else
    {
        Debug.LogError(response.JSONData);
    }
}

所以,你只需要调用你的排行榜管理器请求来获取数据,另一个类就会得到响应

刚刚检查它工作正常

于 2019-03-26T09:06:57.880 回答