unity | 本地读取(IO)外配txt文件(第二集)

基础逻辑请看第一集

​​​​​​Unity | 读取外配txt内容(第一集)_菌菌巧乐兹的博客-CSDN博客

一、用本地IO读取的形式读取txt文件

1.我们需要一个txt和它的地址,我弄了一个外配的指令需要读取

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

2.一行一行的读取

File类都是处理文字的,里面有个方法是ReadAllLines(这里填地址),是一行一行的读取数据

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

二、用本地IO写入txt文件

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

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

三、其他相关的方法

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

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

猜你喜欢

转载自blog.csdn.net/weixin_49427945/article/details/130740000