繁体   English   中英

有没有办法将数组中的数据保存到文本文件中

[英]Is there a way to save data from an array into a text file

我正在对 unity 和 c# 进行测验。 最后有一个带有切换问题和输入字段的调查,我正在使用 arrays 来保存调查中的数据。 有什么办法可以将我存储在数组中的任何内容放入文本文件中,以便以后打开它? 这是我的调查代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UIScript07 : MonoBehaviour
{
    public GameObject[] questionGroupArr;
    public QAClass07[] qaArr;
    public GameObject AnwerPanel;
    void Start()
    {
        qaArr = new QAClass07[questionGroupArr.Length];
    }

    
    void Update()
    {

    }
    public void SubmitAnswer()
    {
        for (int i = 0; i < qaArr.Length; i++)
       {
           qaArr[i] = ReadQuestionAndAnswer(questionGroupArr[i]);
        }
    }
     QAClass07 ReadQuestionAndAnswer(GameObject questionGroup)
    {
        QAClass07 result = new QAClass07();
        GameObject q = questionGroup.transform.Find("Question").gameObject;
        GameObject a = questionGroup.transform.Find("Answer").gameObject;

        result.Question = q.GetComponent<Text>().text;

        if (a.GetComponent<InputField>() != null)
        {
           result.Answer = a.transform.Find("Text").GetComponent<Text>().text;
        }
        else if (a.GetComponent<InputField>()== null)
        {
            string s = "";
            int counter = 0;

            for (int i = 0; i < a.transform.childCount; i++)
            {
                if (a.transform.GetChild(i).GetComponent<Toggle>().isOn)
                {
                    if (counter != 0)
                    {
                        s = s + ",";
                    }
                    s = s + a.transform.GetChild(i).Find("Label").GetComponent<Text>().text;
                    counter++;

                }
                if (i == a.transform.childCount - 1)
                {
                    s = s + ".";
                }
            }
            result.Answer = s;

        }
        return result;
    }

    [System.Serializable]
    public class QAClass07
    {
        public string Question = "";
        public string Answer = "";
    }
}

您可以将阵列数据保存为 json object 并将其保存为文本文件。 如果您希望您的自定义类也保存到 json,您应该添加您已经添加的 **[System.Serializable]。 然后,为了保存任何序列化的 object,您可以创建一个序列化的 class,您可以添加每个变量。 这是示例工作流程。

[Serializable]
public class SaveObject
{
    public GameObject[] questionGroup;
    public string playerName;
    public QAClass07[] qaArr;
    public GameObject AnswerPanel;
}

[System.Serializable]
public class QAClass07
{
    public string Question = "";
    public string Answer = "";
}

public void Save()
{
    var saveObject = new SaveObject()
    {
        AnswerPanel = GetAnswerPanel(),
        playerName = GetPlayerName(),
        qaArr = GetqaArr,
        questionGroup = GetquestionGroup
    };

    string saveText = JsonUtility.ToJson(saveObject);
    File.WriteAllText(Application.persistentDataPath + "/SAVES/save.txt", saveText);
}

public void LoadQ()
{
    if (File.Exists(Application.persistentDataPath + "/SAVES/save.txt"))
    {
        isLoading = true;
        string saveText = File.ReadAllText(Application.persistentDataPath + "/SAVES/save.txt");

        var saveObject = JsonUtility.FromJson<SaveObject>(saveText);

        // You can load back all objects from saveObject in same way. Load and Get methods are random. You get the point
        LoadAnswerPanel(saveObject.AnswerPanel);
    }
}

有一个名为SaveObject的 class 的原因是将所有数据收集到一个 class 并保存到 json 并轻松从 Z466DEEC76ECDF5FCA54D4352 加载回来。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM