Unity | Local reading (IO) external txt file (episode 2)

See the first episode for basic logic

​​​​​​​​Unity | Read external txt content (episode 1)

1. Read the txt file in the form of local IO reading

1. We need a txt and its address, I made an external command to read

string dirName = "/指令外配.txt";
string path;
void Awake()
{
    path = Application.streamingAssetsPath +"/UDP"+ dirName;
}

2. Read line by line

The File class deals with text, and there is a method in it called ReadAllLines (fill in the address here) , which reads data line by line

 private IEnumerator ReadOrder()
    {
        //读取这个地址的txt
        //然后把读下来的每一行都分开保存在string[] textLines里
        string[] textLines = File.ReadAllLines(path);
    }

2. Write txt file with local IO

string s ="想写入的内容";
File.WriteAllText(path, s);        //把s里的内容都写入path在的txt里

string[] str =  { "注意", "这里是", "数组" };
File.WriteAllLines(path, str);    //把数组写入path在的txt里,而且有几个数组就是几行

3. Other related methods

//打开这个地址的txt,如果没有找到,就在这创建一个
FileStream fileStream = new FileStream(地址, FileMode.OpenOrCreate);

string s = File.ReadAllText(地址)        //读取所有内容

Guess you like

Origin blog.csdn.net/weixin_49427945/article/details/130740000