- Home /
How to save text file with the entered text in the username's input field?
Hi everyone, i created a text file in which i will save the player data; name, age, score, etc... I save the username as entered in the input field, is there anyway to save the file with that name entered in the input field text, so when i load it i know its name easily.
Of course it is possible. Supposing you use File.WriteAllText
or File.WriteAllLines
, you just have to provide the content of the input field as first argument (without forgetting to append the file extension)
File.WriteAllText( inputfield.value + ".txt", content );
Please, provide the code you currently have for an answer fitting your needs.
I got it i try to pass the value entered in the input field in start function, so there is no response. here are the code lines :- void Start() { $$anonymous$$yText = usernameInput.text.ToString (); f = new FileInfo(Application.persistentDataPath + $$anonymous$$yText + ".txt"); if(username != null) usernameInput.text = username; } is there any idea for rena$$anonymous$$g the file outside start function?
This isn't the code used to save nor load the data.
I guess you just have to move the code from the Start
function to your Save
function, before writing the content of the file.
Answer by Samuel014 · Feb 10, 2019 at 10:52 AM
Hi, this is my first answer so sorry if I'm wrong. To save strings u can do this:
public string username;
void Start() { // function where you want to load
LoadUsername();
}
void SaveUsername() { // Here we define the function to save the username
PlayerPrefs.SetString("username", username);
}
void LoadUsername() { // Here we define the function to load the username
username = PlayerPrefs.GetString("username");
}
void SaveHere() { // function where u want to save
SaveUsername();
}
This works in general but it's not a good thing to use this a lot of times in a short amount of time. There's other ways like JSON or Binary but this should do the work as well.
Thanks for answer. I know how to save inputfield data, i ask about how to rename the text file with the player name entered in the input field? Regards.
Your answer
Follow this Question
Related Questions
Keep input field caret active when focused on mobile devices in addition to native input field caret 0 Answers
How to prevent keyboard from showing in Android. 1 Answer
Mobile Keyboard Scales Screen 1 Answer
TMP InputField does not fire onTouchScreenKeyboardStatusChanged event when keyboard becomes visible 0 Answers
Disabling the inputfield above the keyboard or disable it all 0 Answers