[내일 배움 캠프 Unity 4기] 04.25 TIL

김용준·2024년 4월 25일
0

내일배움캠프

목록 보기
4/47

Goals

  1. Finalize the project
  2. Learn the tools to introduce the project

Status of personal project

Based on the framework, simply added some features

  1. Local storage to save player's data
  2. Write item list (*.csv) and Link it to the game
  3. Dungeon system to earn exp

Local storage to save player's data

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.

Finding method (w/ directory expression)

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.

Reading method

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);
Writing method

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!");

Q & A

Q1.

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);

A1.

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.

Q2.

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");

A2.

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.

profile
꿈이큰개발자지망생

0개의 댓글