问题描述: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)