我有一个程序可以读取目录中任意数量的.txt文件并将它们组装在一个列表中,然后这个列表一次一个地显示给用户。用户在此时做出决定并存储反应时间。最近,文本导入脚本无缘无故地停止工作。我几个月没有改变任何文本导入代码。
我已检查过读取文件的代码,但根本没有识别出任何内容。
这是仅限于FYI的Unity计划的一部分。
TextImport
using UnityEngine;
using System.Collections;
using System.IO;
using System.Collections.Generic;
public class TextImport : MonoBehaviour {
public static List<string> TextLines;
bool FileRead = false;
// Use this for initialization
void Awake() {
TextLines = new List<string>();
string path;
if(Application.platform == RuntimePlatform.WindowsEditor)
{
path = Application.dataPath + "/Resources/";
}
else if (Application.platform == RuntimePlatform.WindowsPlayer)
{
path = Application.dataPath;
}
else if (Application.platform == RuntimePlatform.Android)
{
FileRead = true;
path = "/StroopTest/";
TextAsset Allfiles = Resources.Load("Android/AllText") as TextAsset;
string[] AllLinesAndroid = Allfiles.text.Split("\n"[0]);
foreach (string Line in AllLinesAndroid)
{
TextLines.Add(Line);
break;
}
}
else
{
path = Application.dataPath;
}
if (!FileRead)
{
DirectoryInfo info = new DirectoryInfo(path);
FileInfo[] fileInfo = info.GetFiles("*.txt");//I've changed the "*.txt" to nothing to read all files instead of just .txt files
foreach (FileInfo file in fileInfo)
{
//I created another List here to store the file names that were read, however none were.
string[] AllLines = File.ReadAllLines(file.FullName);
foreach (string line in AllLines)
{
TextLines.Add(line);
}
}
}
}
}
答案 0 :(得分:2)
我建议使用Application.persistentDataPath
。
Application.dataPath
是一个只读目录,如果打包这样做,你可以在那里找到与游戏相关的资产。
Application.persistentDataPath
是一个可写目录,它返回一个路径(不同平台不同),您可以在运行时添加内容(例如:编写文件,下载文件等)。
我希望有所帮助。