【问题标题】:Is there a way to save data from an array into a text file有没有办法将数组中的数据保存到文本文件中
【发布时间】:2021-02-20 13:20:00
【问题描述】:

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

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

【问题讨论】:

标签: c# unity3d survey


【解决方案1】:

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

[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的类,是为了收集一个类中的所有数据并保存到json并轻松地从json加载回来。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-18
    • 2019-09-07
    • 2012-04-18
    相关资源
    最近更新 更多