- Home /
Save/Copy a .txt file a runtime
I am making a Card Game and my Game creates a .txt file in the assets folder and logs all the cards that were played during the game into the file as the game is being played. After the game has finished a "Save Game" button appears. What I want it to do, is to open a prompt for the user to select a file location in his files to save the Game log to. After that I want to create a text file in that location and fill it with the content of the text file from the assets folder. I have tried the following:
public static void SaveFileToUserLocation()
{
var path = EditorUtility.SaveFilePanel("Save game as .txt", "C:/", "My Game #" + PlayerPrefs.GetInt("SpielNr") + ".txt", "txt");
File.Move("Assets / gameInfo.txt", path);
}
and:
public static void SaveFileToUserLocation()
{
var path = EditorUtility.SaveFilePanel("Save game as .txt", "C:/", "My Game #" + PlayerPrefs.GetInt("SpielNr") + ".txt", "txt");
File.Create(path).Dispose();
string FileContent = File.ReadAllText("Assets / gameInfo.txt");
File.WriteAllText(path, FileContent);
}
That doesn't work though, because it requires UnityEditor to work, which I can use in the finished Build.
What is the best way to do this? Thanks for any help in advance!
Answer by ElijahShadbolt · Nov 07, 2017 at 10:39 PM
You can save a file manually in code (see question Create Text File), so you could make your own dialog box in Unity's UI, but if you're happy with the built-in Windows dialog, you could try using SaveFileDialog. (untested)
If I wanted it to look really nice, I'd probably make my own dialog box. But another solution that's worth knowing about is this cross-platform one on the wiki: http://wiki.unity3d.com/index.php?title=ImprovedFileBrowser
(better fix your link there @Bonfire-Boy )
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Application.persistentDataPath vs Project files 0 Answers
.txt file issue, first line is skipped 2 Answers