Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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.

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

[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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges