在 Unity 中使用 Firebase SDK,当我尝试为我制作的一组三个小游戏制作一个简单的排行榜时。我遇到的问题是使用访问数据库GetValueAsync(),ContinueWith()它似乎ContinueWith()每次都完全跳过。这是我的代码:
string scoreDisplay(string game, string pos)
{
FirebaseApp.DefaultInstance.SetEditorDatabaseUrl("https://shape-arcade-leaderboard.firebaseio.com/");
string score = "error";
FirebaseDatabase.DefaultInstance.RootReference.GetValueAsync().ContinueWith(task =>
{
if (task.IsCompleted)
{
DataSnapshot data = task.Result;
score = data.Child("scores").Child(game).Child(pos).Value.ToString();
return score;
}
else if (task.IsFaulted)
{
score = "errorfaulted";
return score;
}
else
{
return score;
}
});
return null;
}
Run Code Online (Sandbox Code Playgroud)
因此,该函数采用相关小游戏的名称以及要从中检索数据的位置子项。它试图访问我的数据库并拉下它的快照,此时它应该进入ContinueWith()函数。然后,如果完成,它应该访问数据库和相关的子游戏,然后访问该游戏中的子位置并返回其值。如果出错,它应该只返回“errorfaulted”,否则它应该只返回“error”。然而,相反,它只是在底部返回 null 因为它似乎从未运行过ContinueWith()。
我知道在Stack Overflow Question 上提出了一个类似的问题, 但除了等待下一个 beta 版本然后建议的解决方法之外,它似乎没有答案。变通办法似乎对我也不起作用 - 我尝试索引数据库并确保其中有数据,但没有任何区别。无论如何,我希望有人能帮助我,我将不胜感激!