- Home /
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);
}
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;
}
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.
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.
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");
}
Never $$anonymous$$d, just randomly figured the problem myself.. :D
Your answer
Follow this Question
Related Questions
How to make text appear when moving next to an object? 3 Answers
Intro GUI Text Script... 3 Answers
Changing 3d text through script 1 Answer
Simple Backpack "Pick up and drop" 1 Answer