Based on the framework, simply added some features
exp
Before the implementation, we have to study the basic of file IO on C#. Lots of the method, I applied 3 commands to the game.
When you tend to read textfile on the C# script, we have to know the executable's current location. We can check the current path through the below command
Console.WriteLine(Path.GetFullPath("./"));
Then we can see the path on the console like below
C:\current\path\
But we don't know the argument of GetFullPath
means. The ./
is frequantly used on shell. If you use Linux of Mac, you can see this when typing pwd
. The meaning of ./
is the current path. we can express file location relatively. Since the C# project executes in the
<ProjectName\>/<ProjectName\>/bin/Debug/net<the version\>/
I made player data on the Assets/PlayerData
directory. then the relative path can be expressed like
../../../../Assets/PlayerData/<File>
If the location is confirmed or not, we can check the file existence with below command
Console.WriteLine(File.Exist("path/file.extension"));
If you inserted right path and filename, you can see true.
After I find the file with its existence, my interest moved to read the text in the file; the basic file IO on C#.
We can read the text with below command
StreamReader sr = new StreamReader("path/file.ext");
string line = sr.ReadLine();
Console.WriteLine(line);
To save the player data locally, I have to write the data on the local directory. Similar with the Reading method, the command can be formed like
StreamWriter sw = new StreamWriter("path/file.ext");
sr.WriteLine("Hello, World!");
When I use StreamReader
, the korean character displayed while converted a lot of ?
. Even I tried the encoding type o StreamReader
like below, it's not solved. How do I solve this problem?
StreamReader sr = new StreamReader("path/text.txt",Encoding.Default);
When you read the text file, you have to check the encoding format of text file. If you write it with MS-notepad, the encoding format set to ANSI
. Convert it to UTF8
, and re-try to read korean.
When I use StreamWriter
, Only the file was created without any lines inside of file. My script can be seen on the bottom. How can I retrieve the lines?
StreamWriter sw = new StreamWriter("path/text.txt");
sw.WriteLine("line");
You have to understance the operation ofStreamWriter
command. The WriteLine()
method work not actually write string to textfile but store the line to buffer. If you searched the usage of StreamWriter
, there are also the following command of sw.Close()
. By closing the StreamWriter, the whole lines on buffer were written on the textfile.