正确的返回方法类型是什么?

时间:2019-03-24 23:52:54

标签: c# unity3d gamesparks

我有一种使用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.Data类`GSEnumerable中的foreach中的StartController不包含Data的定义,并且没有可访问的扩展名。

foreach中的LeaderboardManager打印正常

我尝试将response.Data返回为ArrayList,但在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;
}

1 个答案:

答案 0 :(得分:0)

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

keyboard = [
    [
        InlineKeyboardButton('yes ', callback_data='yes'),
        InlineKeyboardButton('no ', callback_data='no')
    ]
]

bot.send_animation(
    chat_id=update.message.chat.id,
    animation='file_id',
    caption='go back??',
    reply_markup=InlineKeyboardMarkup(keyboard)
)

然后在LeaderboardDataRequest中调用此操作

public static Action<LeaderboardDataResponse> GetResponce;

在脚本开始时订阅动作

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

和GotData函数:

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

因此,您只需要调用排行榜经理的请求即可获取数据,而另一个类将获得响应

只需检查即可正常