我正在尝试在 Unity 中读取文本文件。我有问题。
在桌面中,当我生成 Stand Alone 时,我需要手动复制文本文件。我不知道如何包含在我的应用程序中。
在 Web 应用程序(和 Android)中,我手动复制了文件,但我的游戏找不到它。
这是我的“阅读”代码:
public static string Read(string filename) {
//string filePath = System.IO.Path.Combine(Application.streamingAssetsPath, filename);
string filePath = System.IO.Path.Combine(Application.dataPath, filename);
string result = "";
if (filePath.Contains("://")) {
// The next line is because if I use path.combine I
// get something like: "http://bla.bla/bla\filename.csv"
filePath = Application.dataPath +"/"+ System.Uri.EscapeUriString(filename);
//filePath = System.IO.Path.Combine(Application.streamingAssetsPath, filename);
WWW www = new WWW(filePath);
int timeout = 20*1000;
while(!www.isDone) {
System.Threading.Thread.Sleep(100);
timeout -= 100;
// NOTE: Always get a timeout exception ¬¬
if(timeout <= 0) {
throw new TimeoutException("The operation was timed-out ("+filePath+")");
}
}
//yield return www;
result = www.text;
} else {
#if !UNITY_WEBPLAYER
result = System.IO.File.ReadAllText(filePath);
#else
using(var read = System.IO.File.OpenRead(filePath)) {
using(var sr = new StreamReader(read)) {
result = sr.ReadToEnd();
}
}
#endif
}
return result;
}
我的问题是:
如何将我的“文本文件”包含为游戏资源?
我的代码有问题吗?