简体   繁体   中英

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

I am making a quiz with both unity and c#. At the end there is a survey with toggle questions and an in input field and I am using arrays to save the data from the survey. Is there any way I can take the whatever I store in my array and put it in a text file so I can open it later? Here is my code for the survey:

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

You can save your array data as json object and save it as text file. If you want your custom classes to save to json too, you should add **[System.Serializable] which you added already. Then for saving any serialized object you can create a serialized class that you can add every variable. Here is sample workflow.

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

The reason there is a class named SaveObject is collecting all data in one class and save to json and load it back from json easily.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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