• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by SoundPuppy · Apr 18, 2016 at 06:30 PM · serializebinarycs

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;
     }
 }
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by TBruce · Apr 18, 2016 at 07:49 PM

@SoundPuppy

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().

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SoundPuppy · Apr 20, 2016 at 03:07 PM 0
Share

@$$anonymous$$avina so delete the data.text?

avatar image TBruce SoundPuppy · Apr 20, 2016 at 07:26 PM 0
Share

@SoundPuppy

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);
avatar image SoundPuppy TBruce · Apr 21, 2016 at 03:17 PM 0
Share

Yayyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy Thank you so much your the best it worked!!!!!!!!!!

avatar image SoundPuppy · Apr 20, 2016 at 04:53 PM 0
Share

@$$anonymous$$avina Please reply

avatar image SoundPuppy · Apr 23, 2016 at 12:45 PM 0
Share

@$$anonymous$$avina

i need help please but the script for save system is too long for me to publish

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

55 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Save Transform and other Information into Binary File 0 Answers

Problemas con Input.GetKeyDown() 0 Answers

How to convert .bin file to unity project?,How to get unity code from .bin file 0 Answers

[SOLVED] "IOException: Sharing violation on path" Trying to save multiple files 3 Answers

Unity3d files convert to base64 encode 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges