我尝试使用Unity使用Firebase实时数据库开发排行榜系统。
我可以存储和加载得分值,一切正常。 但是,当我尝试按顺序检索分数时,我无法做到。我的确如下所述: https://firebase.google.com/docs/database/unity/retrieve-data#sort_data
我的firebase控制台中的数据结构如下:
我的“orderByChild”使用代码如下:
// Set the leaderboard reference
m_leaderBoardDBReference = mDatabaseReference.Child ("Users");
// Get the data, order by child "highScore"
m_leaderBoardDBReference.OrderByChild("highScore").GetValueAsync().ContinueWith(task => {
if (task.IsFaulted) {
// Handle the error...
Debug.Log("Data " + lbAddress + " not found");
}else if (task.IsCompleted) {
DataSnapshot snapshot = task.Result;
// Print the raw json value of the leaderboard data
Debug.Log("Leaderboard Raw Json Value\n");
Debug.Log(snapshot.GetRawJsonValue());
}
});
我得到以下结果:
Leaderboard Raw Json Value
{"user3":{"highScore":49},"user4":{"highScore":0},"user5":{"highScore":69},"user6":{"highScore":155}}
换句话说,不正确排序。
我还尝试了如下的“OrderByValue”版本:
// Set the leaderboard reference
m_leaderBoardDBReference = mDatabaseReference.Child ("Contests/AprilMay");
// Get the data, order by value
m_leaderBoardDBReference.OrderByValue().GetValueAsync().ContinueWith(task => {
if (task.IsFaulted) {
// Handle the error...
Debug.Log("Data " + lbAddress + " not found");
}else if (task.IsCompleted) {
DataSnapshot snapshot = task.Result;
// Print the raw json value of the leaderboard data
Debug.Log("Leaderboard Raw Json Value\n");
Debug.Log(snapshot.GetRawJsonValue());
}
});
结构:
输出:
Leaderboard Raw Json Value
{"dummy":0,"user3":240,"user5":69,"user6":155}