我有一种使用 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
在StartController
类 GSEnumerable 的 foreach 中不包含 Data 的定义并且没有可访问的扩展名。
在印刷品foreach
中还可以LeaderboardManager
我试图返回response.Data
asArrayList
并且在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;
}