• 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 /
avatar image
Question by Jammer3000 · Dec 13, 2012 at 02:22 AM · guitextload

How to save and load text in a GUI TextArea

Hi I have this script that works great, but the problem is that I need it to have a "save" and "load" buttons, so that when the user is done typing in the text he can click save, then when he comes back he can click load and it will load the previous text that was saved, i'm not very good at code so if you could take it easy on me please, thanks.

 var stringToEdit : String = "Hello World\nI've got 2 lines...";
 
 function OnGUI () {
     // Make a multiline text area that modifies stringToEdit.
     stringToEdit = GUI.TextArea (Rect (100, 50, 500, 400), stringToEdit, 200);
 }
Comment

People who like this

0 Show 0
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
Best Answer

Answer by save · Dec 13, 2012 at 02:32 AM

You can use PlayerPrefs to save and load data.

The quick version

 var stringToEdit : String = "Hello World\nI've got 2 lines...";
 
 function OnGUI () { 
     stringToEdit = GUI.TextArea (Rect (100, 50, 500, 400), stringToEdit, 200);
     if (GUI.Button(Rect(100, 30, 60, 20), "Save"))
         PlayerPrefs.SetString("Text Area", stringToEdit);
     if (GUI.Button(Rect(170, 30, 60, 20), "Load"))
         stringToEdit = PlayerPrefs.GetString("Text Area");
 }


Although this is not completely optimized as every time you click a button the disk will be read or written. What you could do is to have a variable in between which gets stored to disk when needed (for instance when player leaves the application).

The cached version

 var stringToEdit : String = "Hello World\nI've got 2 lines...";
 private var cachedString : String;

 function Start () {
     LoadString();
 }

 function OnGUI () { 
     stringToEdit = GUI.TextArea (Rect (100, 50, 500, 400), stringToEdit, 200);
     if (GUI.Button(Rect(100, 30, 60, 20), "Save"))
         cachedString = stringToEdit;
     if (GUI.Button(Rect(170, 30, 60, 20), "Load"))
         stringToEdit = cachedString;
 }

 function OnApplicationQuit () {
     SaveString();
 }

 function SaveString () {
     PlayerPrefs.SetString("Text Area", stringToEdit);
 }
 function LoadString () {
     cachedString = PlayerPrefs.GetString("Text Area");
     stringToEdit = cachedString;
 }

Comment
MountDoomTeam
IwanYupa
Rukas90

People who like this

3 Show 4 · 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 Jammer3000 · Dec 13, 2012 at 12:27 PM 0
Share

Wow thanks works great, but would you know how to put in a variable or something that can allow me to change the gui style for the textfield in the inspector? And thanks for the link for PlayerPrefs, its would like it if more people did what you did and didn't just write the code, but provides a way to learn what they wrote, thanks.

avatar image save · Dec 13, 2012 at 12:41 PM 0
Share

Glad you found it useful. You can use a GUI Skin to change the appearance of GUI elements. You can create a GUI Skin by right-clicking within your Project view (Create > GUI Skin). To apply the skin you have to assign it through the inspector to a variable in your script. In your case it would look like this:

 var myGUISkin : GUISkin; //Drag and drop your GUI Skin from the Project view to the Inspector
 var stringToEdit : String = "Hello World\nI've got 2 lines...";

 function OnGUI () {
     GUI.skin = myGUISkin; //Assign the new skin to the GUI
     stringToEdit = GUI.TextArea (Rect (100, 50, 500, 400), stringToEdit, 200);
     if (GUI.Button(Rect(100, 30, 60, 20), "Save"))
        PlayerPrefs.SetString("Text Area", stringToEdit);
     if (GUI.Button(Rect(170, 30, 60, 20), "Load"))
        stringToEdit = PlayerPrefs.GetString("Text Area");
 }

What you most likely want to change is the Text Area element on the GUI Skin when you've created it within Project view.

avatar image Rukas90 save · Apr 20, 2016 at 01:31 PM 0
Share

But how to delete the saved data?
For example:

Everytime i press the button (1) I add number to the GUIText and save the data,

GUIT.guiText.text += 1;
PlayerPrefs.SetString("OrderNumber", GUIT.guiText.text);

but when i press the other button (2) i want to delete the text and save the deleted data:

GUIT.guiText.text == "";
PlayerPrefs.SetString("OrderNumber", GUIT.guiText.text);

But it doesn't delete the text. It doesn't delete the saved data. How to reset it?


I load saved data like this:

var GT : GUIText;
function Awake ()
{
GT.guiText.text = PlayerPrefs.GetString("OrderNumber");
}

avatar image Rukas90 Rukas90 · Apr 20, 2016 at 01:40 PM 0
Share

Never mind, just randomly figured the problem myself.. :D

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.

Update about the future of Unity Answers

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta later in June. Please note, we are aiming to set Unity Answers to read-only mode on the 31st of May in order to prepare for the final data migration.

For more information, please read our full announcement.

Follow this Question

Answers Answers and Comments

11 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

Related Questions

How to make text appear when moving next to an object? 3 Answers

How to load a scene on collision 1 Answer

Level Button And Quit Button Android 2 Answers

Draw GUI Text if clicked out of webplayer 1 Answer

GUI button not showing up 1 Answer


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