简体   繁体   中英

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

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

public static void SaveUser(FieldController controller)
   {
       BinaryFormatter formatter = new BinaryFormatter();

       string path = Application.persistentDataPath + "/user.frostbyte";
       FileStream stream = new FileStream(path, FileMode.Create);

       UserData data = new UserData(controller);

       formatter.Serialize(stream, data);
       stream.Close();
   }

public static UserData LoadData()
   {
       string path = Application.persistentDataPath + "/user.frostbyte";
       if(File.Exists(path))
       {
           BinaryFormatter formatter = new BinaryFormatter();
           FileStream stream = new FileStream(path, FileMode.Open);

           UserData data = formatter.Deserialize(stream) as UserData;
           stream.Close();

           return data;
       }
       else
       {
           Debug.LogError("Save file not found in " + path);
           return null;
       }
   }

UserData.cs

public static List<string> inputFieldText = new List<string>();

   public UserData (FieldController fieldController)
   {
       foreach (InputField inputField in GameObject.FindObjectsOfType<InputField>())
       {
           foreach (TextMeshProUGUI text in inputField.GetComponentsInChildren<TextMeshProUGUI>())
           {
               if (text.gameObject.name != "Placeholder")
                   inputFieldText.Add(text.text);
           }
       }
   }

FieldController.cs

public void SavePlan()
   {
       SaveSystem.SaveUser(this);
   }

   public void LoadPlan()
   {
       UserData data = SaveSystem.LoadData();

       foreach (InputField inputField in GameObject.FindObjectsOfType<InputField>())
       {
           foreach (TextMeshProUGUI text in inputField.GetComponentsInChildren<TextMeshProUGUI>())
           {
               if (text.gameObject.name != "Placeholder")
                   text.text = UserData.inputFieldText[0];
           }
       }
   }

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

public static void SaveUser(FieldController controller)
   {
       BinaryFormatter formatter = new BinaryFormatter();

       string path = Application.persistentDataPath + "/user.frostbyte";
       FileStream stream = new FileStream(path, FileMode.Create);

       UserData data = new UserData(controller);

       formatter.Serialize(stream, data);
       stream.Close();
   }

public static UserData LoadData()
   {
       string path = Application.persistentDataPath + "/user.frostbyte";
       if(File.Exists(path))
       {
           BinaryFormatter formatter = new BinaryFormatter();
           FileStream stream = new FileStream(path, FileMode.Open);

           UserData data = formatter.Deserialize(stream) as UserData;
           stream.Close();

           return data;
       }
       else
       {
           Debug.LogError("Save file not found in " + path);
           return null;
       }
   }

UserData.cs

public static List<string> inputFieldText = new List<string>();

   public UserData (FieldController fieldController)
   {
       foreach (InputField inputField in GameObject.FindObjectsOfType<InputField>())
       {
           foreach (TextMeshProUGUI text in inputField.GetComponentsInChildren<TextMeshProUGUI>())
           {
               if (text.gameObject.name != "Placeholder")
                   inputFieldText.Add(text.text);
           }
       }
   }

FieldController.cs

public void SavePlan()
   {
       SaveSystem.SaveUser(this);
   }

   public void LoadPlan()
   {
       UserData data = SaveSystem.LoadData();

       foreach (InputField inputField in GameObject.FindObjectsOfType<InputField>())
       {
           foreach (TextMeshProUGUI text in inputField.GetComponentsInChildren<TextMeshProUGUI>())
           {
               if (text.gameObject.name != "Placeholder")
                   text.text = UserData.inputFieldText[0];
           }
       }
   }

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