【Unity3D读取数据】(一)Txt文档操作(创建、读取、写入、修改) 您所在的位置:网站首页 unity题库 【Unity3D读取数据】(一)Txt文档操作(创建、读取、写入、修改)

【Unity3D读取数据】(一)Txt文档操作(创建、读取、写入、修改)

2023-08-24 22:27| 来源: 网络整理| 查看: 265

推荐阅读

CSDN主页GitHub开源地址Unity3D插件分享简书地址我的个人博客QQ群:1040082875

大家好,我是佛系工程师☆恬静的小魔龙☆,不定时更新Unity开发技巧,觉得有用记得一键三连哦。

一、前言

在程序开发中,经常会遇到要从外部文件中读取数据的情况,文件类型也比较多。

于是,小魔龙就将常见的文件类型:Txt、Json、Xml、Excel、Csv,读取、修改、保存等常见操作代码总结下来。

一方面可以自己复习,另一方面也希望可以帮助更多的人吧。

这是本系列文章第一篇: 【Unity3D读取数据】(一)Txt文档操作(创建、读取、写入、修改) 【Unity3D读取数据】(二)Json文件操作(创建、读取、解析、修改) 【Unity3D读取数据】(三)Xml文件操作(创建、读取、写入、修改) 【Unity3D读取数据】(四)Excel文件操作(创建、读取、写入、修改) 【Unity3D读取数据】(五)Csv文件操作(创建、读取、写入、修改)

二、唠叨两句

读取txt其实是很简单的,用File类、FileStream类、 SteamReader类、 StreamWriter类都可以读取文件。

主要就是分为两种形式,一种是文件读取的形式,一种就是流数据的形式。

读取txt的类很多,读取的形式也很多,有的整篇读取,有的一行一行读取。

读取是第一步,保存数据是第二步,读取的方式也影响了保存数据的方式,如果是一行一行读取,那么就可以一行一行解析,然后保存到数据集合中,如果是整篇读取,就需要对整篇就行每行解析,然后通过分隔符就行分割保存。

三、读取txt文档

我们新建一个名字叫做TextRead.txt的文档,键入内容后保存。 在这里插入图片描述 要注意的一点是,文档格式设置为UTF-8,不然中文可能显示不太正确。

3-1、通过TextAsset类读取文档

先来一个最简单的读取方法:

using System.Collections; using System.Collections.Generic; using UnityEngine; public class Demo5 : MonoBehaviour { public TextAsset textTxt; void Start() { Debug.Log(textTxt.text); } }

把文档拖到对应的卡槽中: 在这里插入图片描述 运行结果: 在这里插入图片描述

3-2、通过File类读取文件

通过File类的ReadAllText()把文档所有内容读取下来:

using System.Collections; using System.Collections.Generic; using System.IO; using UnityEngine; public class Demo5 : MonoBehaviour { void Start() { string textTxt = File.ReadAllText(Application.streamingAssetsPath + "/TextRead.txt"); Debug.Log(textTxt); } }

也可以使用File类的ReadAllLines()函数,将这个文档按照一行一行进行全部读取:

using System.Collections; using System.Collections.Generic; using System.IO; using UnityEngine; public class Demo5 : MonoBehaviour { void Start() { string[] textTxt = File.ReadAllLines(Application.streamingAssetsPath + "/TextRead.txt"); for (int i = 0; i void Start() { string path = Application.streamingAssetsPath + "/TextRead.txt"; //文件流形式读取文档 using (FileStream fs = new FileStream(path, 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); } } }

还可以通过File类的OpenRead()函数加载文档:

using System.Collections; using System.Collections.Generic; using System.IO; using System.Text; using UnityEngine; public class Demo5 : MonoBehaviour { void Start() { string path = Application.streamingAssetsPath + "/TextRead.txt"; //文件流形式读取文档 using (FileStream fs = File.OpenRead(Application.streamingAssetsPath + "/TextRead.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、以流形式读取文档

通过IO命名空间下的StreamReader类以流形式读取文档:

using System.Collections; using System.Collections.Generic; using System.IO; using System.Text; using UnityEngine; public class Demo5 : MonoBehaviour { void Start() { string path = Application.streamingAssetsPath + "/TextRead.txt"; //流形式读取文档 using (StreamReader sr = new StreamReader(path)) { string content = sr.ReadToEnd(); sr.Close(); sr.Dispose(); Debug.Log(content); } } }

还可以使用File类的OpenText()函数以流形式读取文档:

using System.Collections; using System.Collections.Generic; using System.IO; using System.Text; using UnityEngine; public class Demo5 : MonoBehaviour { void Start() { string path = Application.streamingAssetsPath + "/TextRead.txt"; //流形式读取文档 using (StreamReader sr = File.OpenText(path)) { string content = sr.ReadToEnd(); sr.Close(); sr.Dispose(); Debug.Log(content); } } }

注意看,还是有区别的。。。

四、修改数据保存文档 4-1、通过File类写入数据

还记得怎么读取数据吗? File.ReadAllText()函数及ReadAllLines()函数 那么写入数据就使用: File.WriteAllText()函数及ReadWriteLines()函数 File.WriteAllText()

using System.Collections; using System.Collections.Generic; using System.IO; using System.Text; using UnityEngine; public class Demo5 : MonoBehaviour { void Start() { string path = Application.streamingAssetsPath + "/TextRead.txt"; File.WriteAllText(path, "测试数据"); } }

ReadWriteLines()

using System.Collections; using System.Collections.Generic; using System.IO; using System.Text; using UnityEngine; public class Demo5 : MonoBehaviour { void Start() { string path = Application.streamingAssetsPath + "/TextRead.txt"; string[] content = { "测试数据1", "测试数据2", "测试数据3" }; File.WriteAllLines(path, content); } }

WriteAllText()是将整个文本保存到文档中。 ReadWriteLines()函数是将一个string数组保存到文档中。

4-2、通过文件流的形式写入数据

有读就有写:

using System.Collections; using System.Collections.Generic; using System.IO; using System.Text; using UnityEngine; public class Demo5 : MonoBehaviour { void Start() { string path = Application.streamingAssetsPath + "/TextRead.txt"; string content = "测试文档"; //文件流形式读取文档 using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Write)) { byte[] bytes = Encoding.UTF8.GetBytes(content); fs.Write(bytes, 0, bytes.Length); fs.Close(); } } } 4-3、通过流形式写入数据

有StreamReader类就有StreamWriter类,哎,就是这么方便:

using System.Collections; using System.Collections.Generic; using System.IO; using System.Text; using UnityEngine; public class Demo5 : MonoBehaviour { void Start() { string path = Application.streamingAssetsPath + "/TextRead.txt"; string content = "测试文档"; using (StreamWriter sr = new StreamWriter(path)) { sr.WriteLine(content); sr.Close(); sr.Dispose(); Debug.Log(content); } } } 五、后言

本篇文章讲解了使用各种方法读取文档的方式,有通过File类去读取文档,有通过文件流形式去读取文档,有通过流形式去读取文档。

这些读取的操作都需要引入IO命名空间。

基本所有的文档的读取方式都离开上面的几种方式,接下来分享的读取json文档、xml文档、Excel文档,基本都是用上面的几种读取方式。

如果到后面读取不太懂的,可以在这篇文章再复习一下。



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有