Search Unity

Saving all InputField text in scene into a binary file and loading them back up.

Discussion in 'Scripting' started by Skyfall106, Jan 11, 2020.

  1. Skyfall106

    Skyfall106

    Joined:
    Mar 5, 2017
    Posts:
    132
    Hey everyone, I was wondering if anyone would be able to help me with a problem I am having with saving input field text to binary. I'm making a roadmapping tool with Unity and I am trying to save all of the text of the input fields into a binary file via a list, and load them back into the game when the load button is pressed, I'm fairly sure that the input fields saved properly, as there is a file on my computer, but I'm not sure how to load them all back up.

    The Code I have currently seems to save fine but when trying to load the data it doesn't change any of the input field text, Now I know I've done the LoadPlan() method wrong, its because I can't figure out how to load it properly, hence why I am here. If anyone could help out I would be extremely grateful.

    Here is my code:

    SaveSystem.cs

    Code (CSharp):
    1. public static void SaveUser(FieldController controller)
    2.    {
    3.        BinaryFormatter formatter = new BinaryFormatter();
    4.  
    5.        string path = Application.persistentDataPath + "/user.frostbyte";
    6.        FileStream stream = new FileStream(path, FileMode.Create);
    7.  
    8.        UserData data = new UserData(controller);
    9.  
    10.        formatter.Serialize(stream, data);
    11.        stream.Close();
    12.    }
    13.  
    14. public static UserData LoadData()
    15.    {
    16.        string path = Application.persistentDataPath + "/user.frostbyte";
    17.        if(File.Exists(path))
    18.        {
    19.            BinaryFormatter formatter = new BinaryFormatter();
    20.            FileStream stream = new FileStream(path, FileMode.Open);
    21.  
    22.            UserData data = formatter.Deserialize(stream) as UserData;
    23.            stream.Close();
    24.  
    25.            return data;
    26.        }
    27.        else
    28.        {
    29.            Debug.LogError("Save file not found in " + path);
    30.            return null;
    31.        }
    32.    }
    UserData.cs

    Code (CSharp):
    1. public static List<string> inputFieldText = new List<string>();
    2.  
    3.    public UserData (FieldController fieldController)
    4.    {
    5.        foreach (InputField inputField in GameObject.FindObjectsOfType<InputField>())
    6.        {
    7.            foreach (TextMeshProUGUI text in inputField.GetComponentsInChildren<TextMeshProUGUI>())
    8.            {
    9.                if (text.gameObject.name != "Placeholder")
    10.                    inputFieldText.Add(text.text);
    11.            }
    12.        }
    13.    }
    FieldController.cs

    Code (CSharp):
    1. public void SavePlan()
    2.    {
    3.        SaveSystem.SaveUser(this);
    4.    }
    5.  
    6.    public void LoadPlan()
    7.    {
    8.        UserData data = SaveSystem.LoadData();
    9.  
    10.        foreach (InputField inputField in GameObject.FindObjectsOfType<InputField>())
    11.        {
    12.            foreach (TextMeshProUGUI text in inputField.GetComponentsInChildren<TextMeshProUGUI>())
    13.            {
    14.                if (text.gameObject.name != "Placeholder")
    15.                    text.text = UserData.inputFieldText[0];
    16.            }
    17.        }
    18.    }
     
  2. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    At a quick glance, you are not doing anything with the data you load. You declare it as data here
    Code (CSharp):
    1. UserData data = SaveSystem.LoadData();
    and then set the text field to UserData.inputFieldText[0]. I would assume that should be data.inputFieldText[0].