I Searching for a script witch can read a textfile from the following path: System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments) + “/My Games/test/demo/Saves/”
Also I want that the user can choose the file to load because the file
in my demo is name like this: (filePath + “/save_” + dateTime (dateTime = DateTime.Now.ToString(“dd-MM-yyyy_HH-mm-ss”);))
But how can i make it that the user can choose the save file beacause there can be 1000files or just 2 and there have all different names. I can’t find it there.
I think it is called File Dialog or something like this.
How can I open it and how can I get the string from the path in to a string.
At the moment my Loadding System looks like that but i like to have every readed String in his own String. How can i get every single String from the Array to a seperated string?
In c it is with line[4] or so but this doesen’t work in C#
string[ ] lines;
var list = new List();
var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
using (var streamReader = new StreamReader(fileStream))
{
string line;
while ((line = streamReader.ReadLine()) != null)
{
list.Add(line);
}
}
lines = list.ToArray();
ps. Sorry for my bad english. I’m from germany and doesen’t write/speak much english.
At the moment my Loadding System looks like that but i like to have every readed String in his own String. How can i get every single String from the Array to a seperated string?
string[ ] lines;
var list = new List();
var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
using (var streamReader = new StreamReader(fileStream))
{
string line;
while ((line = streamReader.ReadLine()) != null)
{
list.Add(line);
}
}
lines = list.ToArray();
Next, if all you wanted to do is read all the lines into an array, a method exists for doing that:
Thing is, I didn’t get the impression that’s what you were trying to do… usually one wants to do something with every line of text. You also were asking about a file browser.
This is the File Browser where you can choose the saved file. Then the file from the choosen path in the browser schould be read by the script and the text from the file schould be saved in a float or bool var to use it for the game.
I highly recommend you to use BinaryFormatter (in case you want to serialize as binary), or XMLSerializer (as text) for such use-cases.
Otherwise you’d have to parse each object and variable manually, which will definitely lead to bugs and some major headache.
As for the file browser - you can re-create that via Unity UI, and some scripts. It’s not that hard.
this is my Load script where i want to load the file data into the var. And this is the Problem
public void onLoad(){
filePath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments) + "/My Games/GameTest/Saves/save-test.save";
var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
using (var streamReader = new StreamReader(fileStream))
{
while ((line = streamReader.ReadLine()) != null)
{
// process the line
Debug.Log(line);
}
}
}