Hi everyone.
Ok, I had already looked all around web and I can’t see how I can solve this. Also, I have to say I am a beginner yet in codding, so please, if you could explain what is going on I will really appreciate it.
I am trying to save some data with Unity Serialization, which I am learning from this tutorial from Unity Site (here)
In-scene I have three Input Fields which I am trying to take its data referring to them with “public Text xxx;”. I placed the code into a GameObject which I called “SaveLoad”, and am placing The Text from the input field into the references I created into the script.
I discovered that unity can’t serialize Input Field, Text, GameObject, etc, but can do with strings. So I was wondering, is there a way for convert or transform this Text into string, if yes, how can I do that?
The Code:
using UnityEngine;
using System.Collections;
using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using UnityEngine.UI;
public class saveLoad : MonoBehaviour {
public Text companyName;
public Text ownerName;
public Text location;
public void saveData()
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath + "/slot1.dat");
gameData gd = new gameData();
gd.companyName = companyName;
gd.ownerName = ownerName;
gd.location = location;
bf.Serialize (file, gd);
file.Close();
}
public void loadData()
{
if (File.Exists (Application.persistentDataPath + "/slot1.dat"))
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open (Application.persistentDataPath + "/slot1.dat", FileMode.Open);
gameData gd = (gameData)bf.Deserialize(file);
file.Close();
companyName = gd.companyName;
ownerName = gd.ownerName;
location = gd.location;
}
}
}
[Serializable]
class gameData
{
public Text companyName;
public Text ownerName;
public Text location;
}
Thanks.