如何从该Firebase数据库而不是本地文件中提取Json?

时间:2018-10-03 19:44:51

标签: c# firebase firebase-realtime-database

通过询问如何解决特定的逻辑,我已经询问了四个与此相关的问题,但是12个小时后,我仍然陷入困境。我正在附上我的代码,如果有人可以帮助我找出解决方法,我将非常感激。

因此,此代码读取data.json并将其分配给字符串dataAsJson,然后使用Json加载子项allRoundData(在数据库/ json中)并将其分配给字符串数组allRoundData。

现在程序可以编译了,但是我正试图从Firebase数据库中提取信息。在代码中,您可以看到DataSnapshot快照是从数据库中提取的(出于隐私原因更改了URL),但是我不知道如何将其插入allRoundData而不是本地文件中。

我已经尝试了几种方法,但是不管我得到的错误是“ allRoundData = loadedData.allRoundData;”。没有指向对象的实例。同样,该程序按原样进行编译,但是当将Firebase数据库对象指向该程序时,代码会分崩离析。我已经将data.json完全按原样导入了Firebase数据库,所以我不确定问题是否出在数据库方面,还是我将Firebase数据转换为字符串数组allRoundData的逻辑。

我不知道如何调试程序,我正在尝试学习,但是由于这个问题我坚持了很长时间,以致于我无法专注于其他任何事情。请有人告诉我该怎么做。我查看了所有可以找到的与Firebase,数据库等相关的文档,但是没有任何效果。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using System.IO;

using Firebase;
using Firebase.Unity.Editor;
using Firebase.Database;


public class FirebaseStart : MonoBehaviour
{

public static void Firebase()
{
    // Set up the Editor before calling into the realtime database.
    FirebaseApp.DefaultInstance.SetEditorDatabaseUrl("FIREBASEWEBSITEURL");

    // Get the root reference location of the database.
    DatabaseReference reference = 
FirebaseDatabase.DefaultInstance.RootReference;

    FirebaseDatabase.DefaultInstance
     .GetReference("allRoundData")
      .GetValueAsync().ContinueWith(task => {
          if (task.IsFaulted)
          {
                  // Handle the error...
              }
          else if (task.IsCompleted)
          {
              DataSnapshot snapshot = task.Result;
              // Do something with snapshot...

           }
      });
}
}

public class DataController : MonoBehaviour
{

private RoundData[] allRoundData;

private PlayerProgress playerProgress;
private string gameDataFileName = "data.json";

// Use this for initialization
void Start()
{
    DontDestroyOnLoad(gameObject);
    LoadGameData();
    LoadPlayerProgress();

    SceneManager.LoadScene("MenuScreen");
}

public RoundData GetCurrentRoundData()
{
    return allRoundData[0];
}

public void SubmitNewPlayerScore(int newScore)
{
    if (newScore > playerProgress.highestScore)
    {
        playerProgress.highestScore = newScore;
        SavePlayerProgress();
    }
}

public int GetHighestPlayerScore()
{
    return playerProgress.highestScore;
}
// Update is called once per frame
void Update()
{

}

private void LoadPlayerProgress()
{
    playerProgress = new PlayerProgress();

    if (PlayerPrefs.HasKey("highestScore"))
    {
        playerProgress.highestScore = PlayerPrefs.GetInt("highestScore");
    }
}
private void SavePlayerProgress()
{
    PlayerPrefs.SetInt("highestScore", playerProgress.highestScore);
}

private void LoadGameData()
{
    string filePath = Path.Combine(Application.streamingAssetsPath, gameDataFileName);

    if (File.Exists(filePath))
    {
        string dataAsJson = File.ReadAllText(filePath);
        GameData loadedData = JsonUtility.FromJson<GameData>(dataAsJson);

        allRoundData = loadedData.allRoundData;
    }
    else
    {
        Debug.LogError("Cannot load game data!");
    }
}

}

0 个答案:

没有答案
相关问题