unity中四种读取txt文件的方法和一种写入txt方法
来源:互联网 发布:可惜没如果知乎 编辑:程序博客网 时间:2024/05/28 15:28
//注意:要读取的文件的编码类型要为utf-8,不然会出现中文乱码或者直接不显示,如果是其它编码方式可以把文件
//另保存为utf-8的格式
using UnityEngine;
using System.Collections;
using System.IO;//用法三的时候需要定义这个
using System.Text;//法二的时候需要使用定义这个
public class GUTexture : MonoBehaviour {
GUIText m_GUIText;
public TextAsset m_TextAsset;
TextAsset m_TextAsset1;
string m_Str;
string m_FileName;
string[] strs;
// Use this for initialization
void Start () {
m_GUIText = gameObject.GetComponent<GUIText>();
m_FileName = "Z800虚拟头盔说明书链接UTF-8.txt";
}
// Update is called once per frame
void Update () {
}
void OnMouseEnter() {
//m_GUIText.text = m_Str;
// ReadFile(m_FileName);//法二
//m_GUIText.text =m_Str;//把读取到的内容放到GUIText组件中显示
// Read();//法三
m_GUIText.text = Resources.Load<TextAsset>("Z800虚拟头盔说明书链接").text;//法一
}
//方法二:通过ReadFile(名字自己定义)方法来读取,传入的是文件路径
void ReadFile(string FileName) {
strs = File.ReadAllLines(FileName);//读取文件的所有行,并将数据读取到定义好的字符数组strs中,一行存一个单元
for (int i = 0; i < strs.Length; i++)
{
m_Str += strs[i];//读取每一行,并连起来
m_Str += "\n";//每一行末尾换行
}
}
/* public void Read()
{
try
{
string pathSource = m_FileName;
using (FileStream fsSource = new FileStream(pathSource,
FileMode.Open, FileAccess.Read))
{
// Read the source file into a byte array.
byte[] bytes = new byte[fsSource.Length];
int numBytesToRead = (int)fsSource.Length;
int numBytesRead = 0;
while (numBytesToRead > 0)
{
int n = fsSource.Read(bytes, numBytesRead, numBytesToRead);
if (n == 0)
break;
numBytesRead += n;
numBytesToRead -= n;
}
numBytesToRead = bytes.Length;
//text = Encoding.Default.GetString(bytes);
m_Str= UTF8Encoding.UTF8.GetString(bytes);
}
}
catch
{
//ioEx.Message
}
}
}
//另保存为utf-8的格式
using UnityEngine;
using System.Collections;
using System.IO;//用法三的时候需要定义这个
using System.Text;//法二的时候需要使用定义这个
public class GUTexture : MonoBehaviour {
GUIText m_GUIText;
public TextAsset m_TextAsset;
TextAsset m_TextAsset1;
string m_Str;
string m_FileName;
string[] strs;
// Use this for initialization
void Start () {
m_GUIText = gameObject.GetComponent<GUIText>();
m_FileName = "Z800虚拟头盔说明书链接UTF-8.txt";
}
// Update is called once per frame
void Update () {
}
void OnMouseEnter() {
//m_GUIText.text = m_Str;
// ReadFile(m_FileName);//法二
//m_GUIText.text =m_Str;//把读取到的内容放到GUIText组件中显示
// Read();//法三
m_GUIText.text = Resources.Load<TextAsset>("Z800虚拟头盔说明书链接").text;//法一
}
//方法二:通过ReadFile(名字自己定义)方法来读取,传入的是文件路径
void ReadFile(string FileName) {
strs = File.ReadAllLines(FileName);//读取文件的所有行,并将数据读取到定义好的字符数组strs中,一行存一个单元
for (int i = 0; i < strs.Length; i++)
{
m_Str += strs[i];//读取每一行,并连起来
m_Str += "\n";//每一行末尾换行
}
}
//方法三: 下面这个是通过io来读取txt文件的方法
/* public void Read()
{
try
{
string pathSource = m_FileName;
using (FileStream fsSource = new FileStream(pathSource,
FileMode.Open, FileAccess.Read))
{
// Read the source file into a byte array.
byte[] bytes = new byte[fsSource.Length];
int numBytesToRead = (int)fsSource.Length;
int numBytesRead = 0;
while (numBytesToRead > 0)
{
int n = fsSource.Read(bytes, numBytesRead, numBytesToRead);
if (n == 0)
break;
numBytesRead += n;
numBytesToRead -= n;
}
numBytesToRead = bytes.Length;
//text = Encoding.Default.GetString(bytes);
m_Str= UTF8Encoding.UTF8.GetString(bytes);
}
}
catch
{
//ioEx.Message
}
}
*/
/* 法四
using System.IO;
using System.Text;
Debug.Log(File.ReadAllText("C:\\Users\\zxy\\Desktop\\Z800虚拟头盔说明书链接.txt", Encoding.Default)); //ReadAllText方法第一个参数是要读取txt文件的路径,第二个参数
//第二个参数是编码方式,这里采用默认
*/
/*一种以追加的方式写入txt方法
using System.IO;
using System.Text;
File.AppendAllText("C:\\Users\\zxy\\Desktop\\Z800虚拟头盔说明书链接.txt", "我被写进来了",Encoding.Default);
//第一个参数是要写入的文件路径,第二个参数是要写入的文本,第三个参数是编码方式
*/
}
阅读全文
0 0
- unity中四种读取txt文件的方法和一种写入txt方法
- 读取TXT文件,和生成TXT文件的方法
- 读取TXT文件,和生成TXT文件的方法
- Asp.net读取和写入txt文件方法(实例)!
- C++中Txt文件读取和写入(方法汇总)
- Asp.net读取和写入txt文件方法
- c++逐行读取写入txt文件的方法
- c++逐行读取写入txt文件的方法
- php逐行读取txt文件写入数组的方法
- php逐行读取txt文件写入数组的方法
- php逐行读取txt文件写入数组的方法
- 读取TXT文件的方法
- Javascript写入txt和读取txt文件
- Javascript写入txt和读取txt文件
- Javascript写入txt和读取txt文件
- Javascript写入txt和读取txt文件
- C++写入txt文件方法
- 读取TXT文件内容的方法
- This function or variable may be unsafe(vs 错误)
- Eclipse在windows下的MinGW
- Nodejs爬虫实战项目之链家
- 12月15日学习笔记—面向对象
- java生成随机验证码完整源码分享
- unity中四种读取txt文件的方法和一种写入txt方法
- [操作系统] 文件系统管理和优化
- tomcat配置以及eclipse插件配置
- 74HC595
- 创建Archives遇到问题
- UE4开发交流群,欢迎加入493523372
- coursera-斯坦福-机器学习-吴恩达-第7周笔记-支持向量机SVM
- Java 解压压缩文件,springMVC 接收压缩文件
- as修改主题的插件