Unity3D对TXT文件的操作

系列文章目录

Unity工具



前言

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


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

一、读取txt文档

1-1、TextAsset类读取

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

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、打印结果

在这里插入图片描述
可以全部打印出来,读取所有内容

二、使用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、结果如下

在这里插入图片描述
这也是全部读取内容

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、读取结果

在这里插入图片描述
以上两个都可以设置格式打开文档 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、结果如下

在这里插入图片描述

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、结果如下

在这里插入图片描述
上面两种结果都是一样的,差别有一点点

四、以流形式读取文档

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、结果如下

在这里插入图片描述

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、结果如下

在这里插入图片描述
结果一样的,差别也是有的,但不多

五、写入数据文档

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", "测试测试测试测试测试测试测试测试测试测试");
    }
}

结果如下
在这里插入图片描述

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、结果如下

在这里插入图片描述
最后好像会写入一行空的,不过不影响,读取的时候判断一下就行了

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

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、结果如下

在这里插入图片描述

七、使用流形式写入数据

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

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结果如下

在这里插入图片描述

7-3、Write结果如下

在这里插入图片描述
添加一下解释Write和WriteLine
writeLine:将要输出的字符串与换行控制字符一起输出,当次语句执行完毕时候,光标会移到目前输出字符串的下一行。
write:光标会停在输出字符串的最后一个字符,不会移动到下一行。

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


总结

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

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

  • 28
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Unity 3D中,我们可以使用C#代码来读取配置文件。首先,我们需要创建一个文本文件来存储配置数据。可以使用任何文本编辑器创建一个.cfg或者.txt文件,并将其命名为你喜欢的名称,比如config.cfg。 接下来,我们可以使用Unity的Asset Store中的Simple File Browser插件来让用户选择配置文件。该插件可以方便地让用户在文件系统中选择文件,并获取文件的完整路径。 一旦我们有了配置文件的路径,我们就可以使用C#代码来读取它。我们可以使用Unity中的StreamReader类来读取文本文件的内容。下面是一个示例代码: ``` using UnityEngine; using System.IO; public class ConfigReader : MonoBehaviour { void Start() { string filePath = "选择的配置文件路径"; // 读取配置文件 StreamReader reader = new StreamReader(filePath); string configData = reader.ReadToEnd(); reader.Close(); // 处理配置数据 ProcessConfigData(configData); } void ProcessConfigData(string data) { // 在这里编写处理配置数据的代码 } } ``` 在上面的代码中,我们使用StreamReader打开和读取文件的内容,然后将其保存到一个字符串变量中。一旦我们获得了配置数据,我们可以编写处理数据的代码,根据具体需求进行解析和使用。 读取配置文件后,我们可以使用Split函数等C#字符串操作方法将配置数据分割成适当的键值对或者其他数据结构,然后可以根据需要将其用于游戏的逻辑和功能。 请注意,在使用StreamReader读取文件后,我们应该调用Close方法关闭文件流,以释放资源并避免内存泄漏。 这就是使用Unity 3D读取配置文件的基本方法。根据具体的需求,我们可以根据配置文件的格式进行适当的解析和处理,以满足游戏的需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

心疼你的一切

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值