如何将原始数据另存为文本文件/ json?

时间:2018-10-03 15:02:10

标签: c# firebase

所以我只有一个星期学习C#,而且我已经在这个问题上工作了8个小时。我将描述我想做的事情,如果有更好的方法,我将不胜感激任何指导。

在下面的代码中,我从Firebase数据库(称为快照)存储信息,然后尝试将其保存为文本文件data.json。在代码的稍后部分,它将读取data.json并从文件中调用allRoundData。

现在在Unity中,应用程序尚未编译,并为allRoundData = loadedData.allRoundData表示“对象引用未设置为对象的实例”。

我还检查了在资源管理器中打开文本文件时没有写入任何内容。

我要从Firebase数据库中提取琐事问题和答案,因此,我这样做很重要,用户不能访问答案。跳过将其写入文本文件并仅声明allRoundData = snapshot.allRoundData是否更安全?问题在于allRoundData是一个字符串数组,而快照是一个对象。有没有一种方法可以轻松地将其转换,或者我该如何以最简单的方式解决它?

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 DataController : MonoBehaviour
{


public class FirebaseStart: MonoBehaviour
{
    void Start()
    {

    // Set up the Editor before calling into the realtime database.


FirebaseApp.DefaultInstance.SetEditorDatabaseUrl("https://FIREBASEDATABASE");

        // 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)
              {
                  string gameDataFileName = "data.json";
                  string filePath = 
Path.Combine(Application.streamingAssetsPath, gameDataFileName);
                     DataSnapshot snapshot = task.Result;
                  // Do something with snapshot...
                  string rawData = snapshot.GetRawJsonValue();
                  StreamWriter sw = new StreamWriter(filePath);
                  sw.WriteLine(rawData);
                  sw.Close();
              }
        });
    }
}

private RoundData[] allRoundData;

private PlayerProgress playerProgress;

// 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 gameDataFileName = "data.json";
    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 个答案:

没有答案