问题描述:unity下实现加载txt文件、xml文件、json格式数据。
解决方案:
1) 实现unity下加载txt文件。
a.个文本格式为utf-8的txt文件,以及读取文件的 LoadTXTFile.cs脚本;
b. LoadTXTFile脚本代码如下:
usingUnityEngine;
usingSystem.Collections;
usingSystem.IO;
publicclass LoadTXTFile : MonoBehaviour {
//存放读取的txt文件中的内容
private string[] _txtStr;
void Start()
{
LoadTXT();
//显示读取到的文本内容
if(_txtStr != null &&_txtStr.Length > 0)
{
foreach(string str in _txtStr)
{
Debug.Log(str);
}
}
}
private void LoadTXT()
{
//设置文件位置,根据自己的需要进行设置
string filePath = Application.dataPath+ "/LoadFileStudy/Resources/studyTXT.txt";
try
{
//读取文件的每一行,并将每一行放入_txtStr数组中
_txtStr =File.ReadAllLines(filePath);
}
catch (IOException e)

本文介绍了在Unity中如何实现加载txt文件的详细步骤。通过创建一个utf-8编码的txt文件和使用LoadTXTFile.cs脚本,可以成功读取并显示文件内容。在运行后,控制台会显示出txt文件的文本内容。
最低0.47元/天 解锁文章
3238

被折叠的 条评论
为什么被折叠?



