Unity - Google Play 服务排行榜获取用户排名的问题
Unity - Problem with Google Play Services Leaderboard get user rank
我的 Google Play 服务排行榜有问题,我已经卡住了好几天了。
我正在使用 Play Games Plugin for Unity。
我正在尝试获取特定级别的 Userrank。因此,我正在通过 LoadScores 函数加载分数,这似乎工作正常。
问题似乎是,我在“LoadScores”函数中获得了正确的排名,但总体 GetUserRank
函数仍然是 returning rank = 0,无论实际的 Userrank 是多少是。
我的猜测是,这毕竟是一个时间问题,并且在 LoadScores 函数计算出正确的排名 return 之前排名 return。
public int GetUserRank(string levelID)
{
int rank = 0;
string user = PlayGamesPlatform.Instance.localUser.id;
string leaderboard = ReturnLeaderboard(levelID);
Social.LoadScores(leaderboard, scores =>
{
if (scores.Length > 0)
{
Debug.Log("Retrieved " + scores.Length + " scores");
//Filter the score with the user name
for (int i = 0; i < scores.Length; i++)
{
if (user == scores[i].userID)
{
rank = scores[i].rank;
// This prints out the actual rank of the user and seems to work
print("Rank in LoadScoresFunction: " + rank);
break;
}
}
}
else
Debug.Log("Failed to retrieved score");
});
//Here the rank is always 0, no matter the outcome of the LoadScores Function above
print("Rank in PlayGamesController: " + rank);
return rank;
}
我通过以下方式从其他脚本引用此函数:
int rank = PlayGamesController.Instance.GetUserRank(levelID);
任何ideas/hints非常感谢。
谢谢!
听起来像 LoadScores
被输出 async 所以你 return rank;
在得到结果之前,所以它保持 0
.
您可能不得不使用回调代替直接分配值:
public void GetUserRank(string levelID, Action<int> onResult)
{
var user = PlayGamesPlatform.Instance.localUser.id;
var leaderboard = ReturnLeaderboard(levelID);
Social.LoadScores(leaderboard, scores =>
{
if (scores.Length > 0)
{
Debug.Log("Retrieved " + scores.Length + " scores");
//Filter the score with the user name
for (int i = 0; i < scores.Length; i++)
{
if (user == scores[i].userID)
{
var rank = scores[i].rank;
// This prints out the actual rank of the user and seems to work
print("Rank in LoadScoresFunction: " + rank);
onResult?.Invoke (result);
break;
}
}
}
else
Debug.Log("Failed to retrieved score");
onResult?.Invoke(-1);
});
}
然后像这样使用它使用回调方法
private void OnReceivedResult(int rank)
{
// Do something with rank
}
...
PlayGamesController.Instance.GetUserRank(levelID, OnReceivedResult);
或者使用 lambda 表达式
PlayGamesController.Instance.GetUserRank(levelID, rank => {
// Do something with rank
});
我的 Google Play 服务排行榜有问题,我已经卡住了好几天了。
我正在使用 Play Games Plugin for Unity。
我正在尝试获取特定级别的 Userrank。因此,我正在通过 LoadScores 函数加载分数,这似乎工作正常。
问题似乎是,我在“LoadScores”函数中获得了正确的排名,但总体 GetUserRank
函数仍然是 returning rank = 0,无论实际的 Userrank 是多少是。
我的猜测是,这毕竟是一个时间问题,并且在 LoadScores 函数计算出正确的排名 return 之前排名 return。
public int GetUserRank(string levelID)
{
int rank = 0;
string user = PlayGamesPlatform.Instance.localUser.id;
string leaderboard = ReturnLeaderboard(levelID);
Social.LoadScores(leaderboard, scores =>
{
if (scores.Length > 0)
{
Debug.Log("Retrieved " + scores.Length + " scores");
//Filter the score with the user name
for (int i = 0; i < scores.Length; i++)
{
if (user == scores[i].userID)
{
rank = scores[i].rank;
// This prints out the actual rank of the user and seems to work
print("Rank in LoadScoresFunction: " + rank);
break;
}
}
}
else
Debug.Log("Failed to retrieved score");
});
//Here the rank is always 0, no matter the outcome of the LoadScores Function above
print("Rank in PlayGamesController: " + rank);
return rank;
}
我通过以下方式从其他脚本引用此函数:
int rank = PlayGamesController.Instance.GetUserRank(levelID);
任何ideas/hints非常感谢。
谢谢!
听起来像 LoadScores
被输出 async 所以你 return rank;
在得到结果之前,所以它保持 0
.
您可能不得不使用回调代替直接分配值:
public void GetUserRank(string levelID, Action<int> onResult)
{
var user = PlayGamesPlatform.Instance.localUser.id;
var leaderboard = ReturnLeaderboard(levelID);
Social.LoadScores(leaderboard, scores =>
{
if (scores.Length > 0)
{
Debug.Log("Retrieved " + scores.Length + " scores");
//Filter the score with the user name
for (int i = 0; i < scores.Length; i++)
{
if (user == scores[i].userID)
{
var rank = scores[i].rank;
// This prints out the actual rank of the user and seems to work
print("Rank in LoadScoresFunction: " + rank);
onResult?.Invoke (result);
break;
}
}
}
else
Debug.Log("Failed to retrieved score");
onResult?.Invoke(-1);
});
}
然后像这样使用它使用回调方法
private void OnReceivedResult(int rank)
{
// Do something with rank
}
...
PlayGamesController.Instance.GetUserRank(levelID, OnReceivedResult);
或者使用 lambda 表达式
PlayGamesController.Instance.GetUserRank(levelID, rank => {
// Do something with rank
});