- Home /
Question by
javiercasadogames · Apr 19, 2020 at 04:59 PM ·
jsongoogleleaderboard
How to make a leaderboard in unity using the result JSON of Firebase database
I am using unity3d and Firebase. I can send data to the database, and I can receive it, but I don't know how to use it in the project. I need to transform this json file into an array that has name and score, but I can't get it :(
{
"Elisa" : {
"name" : "Elisa",
"score" : "53"
},
"Javi" : {
"name" : "Javi",
"score" : "12"
},
"Jon" : {
"name" : "Jon",
"score" : "33"
}
}
And I using this class
[Serializable]
public class Points
{
public string name;
public string score;
public Points(string _name, string _score)
{
this.name = _name;
this.score = _score;
}
}
If you want to look at my code, it's this:
using UnityEngine;
using Firebase;
using Firebase.Database;
using Firebase.Unity.Editor;
using System;
public class DatabaseManager : MonoBehaviour
{
DatabaseReference reference;
// Start is called before the first frame update
void Start()
{
// Set this before calling into the realtime database.
FirebaseApp.DefaultInstance.SetEditorDatabaseUrl("https://project-SecretCode.firebaseio.com/");
// Get the root reference location of the database.
reference = FirebaseDatabase.DefaultInstance.RootReference;
}
public void ButtonLoad()
{
ReadDataBase();
}
//leee toda lavase de daton en el apartado score
[ContextMenu("ReadDataBase")]
void ReadDataBase()
{
//reference
FirebaseDatabase.DefaultInstance
.GetReference("Score")
// .GetReference("Score").Child("javi")
.GetValueAsync().ContinueWith(task =>
{
if (task.IsFaulted)
{
// Handle the error...
}
else if (task.IsCompleted)
{
DataSnapshot snapshot = task.Result;
//Debug.Log(snapshot.GetRawJsonValue());
string jsonStr = snapshot.GetRawJsonValue(); //result Json To String
Debug.Log(jsonStr);
}
});
}
}
[Serializable]
public class Points
{
public string name;
public string score;
public Points(string _name, string _score)
{
this.name = _name;
this.score = _score;
}
}
Comment
Your answer

Follow this Question
Related Questions
Google play save doesn't work 1 Answer
Google Play Games Plugin(A simple issue) 0 Answers
Can't see public leaderboard from google play 1 Answer
(Crash) on calling Google Authentication 2 Answers