【问题标题】:How to make a leaderboard in unity using the result JSON of Firebase database如何使用 Firebase 数据库的结果 JSON 统一制作排行榜
【发布时间】:2020-04-15 00:48:26
【问题描述】:

我正在使用 unity3d 和 Firebase。 可以向数据库发送数据,也可以接收,但是不知道在项目中如何使用。 我需要将此 json 文件转换为具有名称和分数的数组,但我无法得到它:(

{
    "Elisa" : {
      "name" : "Elisa",
      "score" : "53"
    },
    "Javi" : {
      "name" : "Javi",
      "score" : "12"
    },
    "Jon" : {
      "name" : "Jon",
      "score" : "33"
    }
}

我使用这个类

[Serializable]
public class Points
{
    public string name;
    public string score;



    public Points(string _name, string _score)
    {
        this.name = _name;
        this.score = _score;
    }
}

如果你想看我的代码,是这样的:

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;
    }
}

【问题讨论】:

    标签: json firebase unity3d firebase-realtime-database leaderboard


    【解决方案1】:

    您可以使用 JsonUtility.FromJson<T> 将 Json 字符串转换为 Unity 对象:

    所以添加类似这个函数的东西:

    public static Points FromJSON(string jsonString)
    {
        return JsonUtility.FromJson<Points>(jsonString);
    }
    

    1. 似乎您需要添加一个类来保存数组,就像在您的代码中为一个玩家保存的那样:
    class PointsArray {
        Points[] allPlayerPoints;
    }
    
    1. 您的 json 当前是一个带有 key:value 的字典,需要有 [ ] 才能成为一个数组。 (然后不要重复名称):
    {
        [
            {
              "name" : "Elisa",
              "score" : "53"
            },
            {
              "name" : "Javi",
              "score" : "12"
            },
            {
              "name" : "Jon",
              "score" : "33"
            }
        ]
    }
    

    【讨论】:

    • 非常感谢。但我不知道如何使它具有另一种格式。 52/5000 base fire 使用以下格式创建数据库:Image
    猜你喜欢
    • 2020-08-19
    • 1970-01-01
    • 1970-01-01
    • 2017-01-01
    • 2021-03-20
    • 1970-01-01
    • 2021-10-06
    • 2022-07-04
    • 1970-01-01
    相关资源
    最近更新 更多