Save system not working for text
i am making a game and the text doesnt save with the save system please help!!!
Save system: using UnityEngine; using System.Collections; using System; using System.Runtime.Serialization.Formatters.Binary; using System.IO; using UnityEngine.UI; public class SaveSystem : MonoBehaviour { public static string text; public static Text charText;
public void SaveState()
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath + "/PlayerData.dat");
PlayerData data = new PlayerData();
data.text = UIInputField.text;
data.charText = UIInputField.charText;
bf.Serialize(file, data);
file.Close();
}
public void LoadState()
{
if(File.Exists(Application.persistentDataPath + "/PlayerData.dat"))
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + "/PlayerData.dat", FileMode.Open);
PlayerData data = (PlayerData) bf.Deserialize(file);
file.Close();
UIInputField.text = data.text;
UIInputField.charText = data.charText;
}
}
}
[Serializable]
class PlayerData
{
public string text;
public Text charText;
}
UIInputField(works): using UnityEngine; using System.Collections; using UnityEngine.UI;
public class UIInputField : MonoBehaviour {
public static Text charText;
public static string text;
// Use this for initialization
void Start ()
{
charText = GameObject.Find("CharText").GetComponent<Text>();
}
// Update is called once per frame
public void CharacterField(string inputFieldString)
{
charText.text = inputFieldString;
text = inputFieldString;
charText.text = text;
}
public void load()
{
charText.text = text;
}
}
Answer by TBruce · Apr 18, 2016 at 07:49 PM
You are trying to save the whole Text component in the PlayerData class. You should instead try saving just the relevant data from the charText component and restoring that info in LoadState().
UIInputField.charText is a component. You cannot save and restore a component but you can save and restore parts of it for example you can save and restore
[Serializable]
class PlayerData
{
public string text;
public string charText;
}
and then
Save data.text = UIInputField.charText.text;
Restore UIInputField.charText.text = data.text;
Now if you want to save and restore anything else from UIInputField.charText you will need to do a little bit more coding. For example lets say you wanted to save the text color you might do something like this
string ColorToHex(Color color)
{
string hex = "#" + ((int)(color.a * 255)).ToString("X2") +
((int)(color.r * 255)).ToString("X2") +
((int)(color.g * 255)).ToString("X2") +
((int)(color.b * 255)).ToString("X2");
return hex;
}
Color HexToColor(string hex)
{
if (hex[0] == '#')
{
hex = hex.Remove(0,1);
}
byte a = byte.Parse(hex.Substring(0,2), System.Globalization.NumberStyles.HexNumber);
byte r = byte.Parse(hex.Substring(2,2), System.Globalization.NumberStyles.HexNumber);
byte g = byte.Parse(hex.Substring(4,2), System.Globalization.NumberStyles.HexNumber);
byte b = byte.Parse(hex.Substring(6,2), System.Globalization.NumberStyles.HexNumber);
return new Color((float)r/255.0f, (float)g/255.0f, (float)b/255.0f, (float)a/255.0f);
}
<u>Save</u>
data.textColor = ColorToHex(UIInputField.charText.color);
<u>Restore</u>
UIInputField.charText.color = HexToColor(data.textColor);
Yayyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy Thank you so much your the best it worked!!!!!!!!!!
@$$anonymous$$avina
i need help please but the script for save system is too long for me to publish
Your answer
![](https://koobas.hobune.stream/wayback/20220612075110im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
[SOLVED] "IOException: Sharing violation on path" Trying to save multiple files 4 Answers
Save system not working 1 Answer
How to make ammo appear on screen in c sharp 2 Answers
Serialize a list to save it 3 Answers
license management 0 Answers