Unity3D对TXT文件的操作

系列文章目录

Unity工具


文章目录

  • 系列文章目录
  • 前言
  • 一、读取txt文档
    • 1-1、TextAsset类读取
    • 1-2、代码实现
    • 1-2、打印结果
  • 二、使用File类读取
    • 2-1.使用ReadAllText读取代码如下:
    • 2-2、结果如下
    • 2-3、使用ReadAllLines读取代码如下:
    • 2-4、读取结果
  • 三、文件流读取文档
    • 3-1、使用FileStream类读取代码如下:
    • 3-2、结果如下
    • 3-3、File类的OpenRead()读取文档代码如下:
    • 3-4、结果如下
  • 四、以流形式读取文档
    • 4-1、使用StreamReader类读取代码如下:
    • 4-2、结果如下
    • 4-3、使用File类的OpenText()流形式读取代码如下:
    • 4-4、结果如下
  • 五、写入数据文档
    • 5-1、通过File类写入数据
    • 5-2、WriteAllText 全部写入 代码如下:
    • 5-3、WriteAllLines 一行一行写入代码如下:
    • 5-4、结果如下
  • 六、通过文件流的形式写入
    • 6-1、使用FileStream写入代码如下
    • 6-2、结果如下
  • 七、使用流形式写入数据
    • 7-1、使用StreamWriter写入代码如下:
    • 7-2、WriteLine结果如下
    • 7-3、Write结果如下
  • 总结


前言

大家好,我是心疼你的一切,会不定时更新Unity开发技巧,觉得有用记得一键三连哦。
在开发中会遇到各种文件类型,这次是txt文件类型,简单记录一下,方便以后使用


读取txt有好几种方式,有File类、FileStream类、StreamReader类、StreamWriter类
读取txt的类很多,读取的形式也很多,有的整篇读取,有的一行一行读取。按需使用

一、读取txt文档

1-1、TextAsset类读取

新建一个文本文档测试Datatxt.txt
切记要设置编码格式为UTF-8格式
Unity3D对TXT文件的操作_第1张图片
要不然中文会乱码
使用这个类读取的文件,文件不能放在StreamingAssets文件夹里,要不然无法拖拽到脚本上面
文本内容如下
Unity3D对TXT文件的操作_第2张图片

1-2、代码实现

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TextAssetsRead : MonoBehaviour
{
    public TextAsset text; 
    // Start is called before the first frame update
    void Start()
    {
        Debug.Log(text.text);
    }
}

1-2、打印结果

Unity3D对TXT文件的操作_第3张图片
可以全部打印出来,读取所有内容

二、使用File类读取

2-1.使用ReadAllText读取代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
public class File_ReadAllText : MonoBehaviour
{
    public string path=Application.streamingAssetsPath ;
    // Start is called before the first frame update
    void Start()
    {
        string strtext = File.ReadAllText(path + "/Datatxt.txt");
        Debug.Log(strtext);
    } 
}

2-2、结果如下

Unity3D对TXT文件的操作_第4张图片
这也是全部读取内容

2-3、使用ReadAllLines读取代码如下:

using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

public class File_ReadAllLines : MonoBehaviour
{
    public string path = Application.streamingAssetsPath;
    // Start is called before the first frame update
    void Start()
    {
        string[]strtext = File.ReadAllLines(path + "/Datatxt.txt");
       
        foreach (var item in strtext)
        {
            Debug.Log(item );
        }
    }
}

2-4、读取结果

Unity3D对TXT文件的操作_第5张图片
以上两个都可以设置格式打开文档 Encoding设置格式
在这里插入图片描述

三、文件流读取文档

3-1、使用FileStream类读取代码如下:

文件流形式读取文档

using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine;

public class FileStream_Read : MonoBehaviour
{
    public string path = Application.streamingAssetsPath;
    // Start is called before the first frame update
    void Start()
    {
        //文件流形式读取文档
        using (FileStream fs = new FileStream(path + "/Datatxt.txt", FileMode.Open, FileAccess.Read))
        {
            byte[] bytes = new byte[fs.Length];
            fs.Read(bytes, 0, bytes.Length);
            fs.Close();
            string str = Encoding.UTF8.GetString(bytes);
            Debug.Log(str);
        }
    }
}

3-2、结果如下

Unity3D对TXT文件的操作_第6张图片

3-3、File类的OpenRead()读取文档代码如下:

using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine;

public class File_OpenRead : MonoBehaviour
{
    public string path = Application.streamingAssetsPath;
    // Start is called before the first frame update
    void Start()
    {
        //文件流形式读取文档
        using (FileStream fs = File.OpenRead(path + "/Datatxt.txt"))
        {
            byte[] bytes = new byte[fs.Length];
            fs.Read(bytes, 0, bytes.Length);
            fs.Close();
            string str = Encoding.UTF8.GetString(bytes);
            Debug.Log(str);
        }
    }
}

3-4、结果如下

Unity3D对TXT文件的操作_第7张图片
上面两种结果都是一样的,差别有一点点

四、以流形式读取文档

4-1、使用StreamReader类读取代码如下:

using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine;

public class StreamReader_Read : MonoBehaviour
{
    public string path = Application.streamingAssetsPath;
    // Start is called before the first frame update
    void Start()
    {
        //流形式读取文档
        using (StreamReader sr = new StreamReader(path + "/Datatxt.txt"))
        {
            string strs = sr.ReadToEnd();
            sr.Close();
            sr.Dispose();
            Debug.Log(strs);
        }
    }
}

4-2、结果如下

Unity3D对TXT文件的操作_第8张图片

4-3、使用File类的OpenText()流形式读取代码如下:

using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

public class File_OpenText : MonoBehaviour
{
    public string path = Application.streamingAssetsPath;
    // Start is called before the first frame update
    void Start()
    {
        //流形式读取文档
        using (StreamReader sr = File.OpenText(path + "/Datatxt.txt"))
        {
            string strs = sr.ReadToEnd();
            sr.Close();
            sr.Dispose();
            Debug.Log(strs);

        }
    }
}

4-4、结果如下

Unity3D对TXT文件的操作_第9张图片
结果一样的,差别也是有的,但不多

五、写入数据文档

5-1、通过File类写入数据

上面用:File.ReadAllText 和 File.ReadAllLines读取的
写入用: File.WriteAllText 和 File.WriteAllLines 写入的

5-2、WriteAllText 全部写入 代码如下:

using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
public class File_WriteAllText : MonoBehaviour
{
    public string path = Application.streamingAssetsPath;
    // Start is called before the first frame update
    void Start()
    {
        //File类写入文档
        File.WriteAllText(path+ "/Datatxt.txt", "测试测试测试测试测试测试测试测试测试测试");
    }
}

结果如下
Unity3D对TXT文件的操作_第10张图片

5-3、WriteAllLines 一行一行写入代码如下:

using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

public class File_WriteAllLines : MonoBehaviour
{
    public string path = Application.streamingAssetsPath;
    // Start is called before the first frame update
    void Start()
    {
        //File类写入文档
        string[] strs = { "测试数据1", "测试数据2", "测试数据3", "测试数据4", "测试数据5" };
        File.WriteAllLines(path + "/Datatxt.txt", strs);
    }
}

5-4、结果如下

Unity3D对TXT文件的操作_第11张图片
最后好像会写入一行空的,不过不影响,读取的时候判断一下就行了

六、通过文件流的形式写入

6-1、使用FileStream写入代码如下

using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine;

public class FileStream_Write : MonoBehaviour
{
    public string path = Application.streamingAssetsPath;
    // Start is called before the first frame update
    void Start()
    {
       
        string content = "测试文档测试文档测试文档测试文档测试文档测试文档";
        //文件流形式写入文档
        using (FileStream fs = new FileStream(path + "/Datatxt.txt", FileMode.Open, FileAccess.Write))
        {
            byte[] bytes = Encoding.UTF8.GetBytes(content);
            fs.Write(bytes, 0, bytes.Length);
            fs.Close();
        }
    }
}

6-2、结果如下

Unity3D对TXT文件的操作_第12张图片

七、使用流形式写入数据

有读就有写,设计的如此完美,哎,就是这么好用

7-1、使用StreamWriter写入代码如下:

using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine;

public class StreamWriter_Write : MonoBehaviour
{
    public string path = Application.streamingAssetsPath;
    // Start is called before the first frame update
    void Start()
    {
        string content = "测试斯柯达监控及安第三季度看金库";
        //流形式写入文档
        using (StreamWriter sr = new StreamWriter(path + "/测试Datatxt.txt"))
        {
            //Write写入之后最后不会有空的一行
            //sr.Write(content);
            //WriteLine 写入之后最后一行有空的一行
            sr.WriteLine(content);
            sr.Close();
            sr.Dispose();
        }
    }
}

7-2、WriteLine结果如下

Unity3D对TXT文件的操作_第13张图片

7-3、Write结果如下

Unity3D对TXT文件的操作_第14张图片
添加一下解释Write和WriteLine
writeLine:将要输出的字符串与换行控制字符一起输出,当次语句执行完毕时候,光标会移到目前输出字符串的下一行。
write:光标会停在输出字符串的最后一个字符,不会移动到下一行。

所以就会出现以上结果一个一行一个两行


总结

本文使用了File类读取/写入,文件流读取/写入,流文件读取/写入
记录一下,以便忘记

不定时更新Unity开发技巧,觉得有用记得一键三连哦。

你可能感兴趣的:(Unity工具,unity,游戏引擎,c#)