构建后读取文本文件的问题
Troubles with reading text file after Build
我在 Unity3D 中读取文本文件时遇到问题。
我创建了一个 returns 类型为 float[][] 并将流阅读器作为参数的方法:
public float[][] CreateWeights(StreamReader reader){
int n = 0;
float[][] Weights = new float[50][];
while((!reader.EndOfStream)){
string text = reader.ReadLine();
if (text == null)
break;
string[] strFloats = text.Split (new char[0]);
float[] floats = new float[strFloats.Length];
for(int i = 0; i<strFloats.Length; i++){
floats[i] = float.Parse(strFloats[i]);
}
Weights[n] = floats;
n++;
}
return Weights;
}
我在 void Start() 中使用这个方法来创建 "weights":
float[][] WeightsIH;
float[][] WeightsHO;
void Start(){
FileInfo theSourceFile = new FileInfo(Application.dataPath + "/Resources/WeightsIH.txt");
StreamReader reader = theSourceFile.OpenText();
FileInfo theSourceFile2 = new FileInfo(Application.dataPath + "/Resources/WeightsHO.txt");
StreamReader reader2 = theSourceFile2.OpenText();
WeightsIH = CreateWeights(reader);
WeightsHO = CreateWeights(reader2);
Yhidden = new float[50][];
HiddenOutput = new float[50][];
Xoutput = new float[1];
}
这在 Unity 的播放模式下会很好地工作。但是,在创建可执行文件后,将找不到这些文件,我明白这一点。所以为了让它工作,我知道我需要使用 Resources.Load 并且我有:
void Start(){
TextAsset text1 = Resources.Load("WeightsIH") as TextAsset;
TextAsset text2 = Resources.Load("WeightsHO") as TextAsset;
WeightsIH = CreateWeights(text1);
WeightsHO = CreateWeights(text2);
Yhidden = new float[50][];
HiddenOutput = new float[50][];
Xoutput = new float[1];
}
当然参数类型不能再是streamReader了,我改成了TextAsset作为参数。这是它的变化:
public float[][] CreateWeights(TextAsset textAsset){
float[][] Weights = new float[50][];
string[] linesFromFile = textAsset.text.Split("\n"[0]);
for(int i = 0; i<linesFromFile.Length; i++){
string[] strFloats = linesFromFile[i].Split (new char[0]);
float[] floats = new float[strFloats.Length];
for(int j = 0; j<strFloats.Length; j++){
floats[j] = float.Parse(strFloats[j]);
}
Weights[i] = floats;
}
return Weights;
}
现在这根本行不通,甚至在播放模式下也不行。我得到的 运行-time 错误如下:
FormatException: Invalid format.
System.Double.Parse (System.String s, NumberStyles style,
IFormatProvider provider) (
at
/Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System/Double.cs:209)
System.Single.Parse (System.String s) (
at
/Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System/Single.cs:183)
FollowShortestPath.CreateWeights (UnityEngine.TextAsset textAsset)
(
at Assets/Scripts/Pathfinding/FollowShortestPath.cs:203)
FollowShortestPath.Start () (
at
Assets/Scripts/Pathfinding/FollowShortestPath.cs:54)
第54行指的是:
WeightsIH = CreateWeights(text1);
第203行是指:
floats[j] = float.Parse(strFloats[j]);
我做错了什么?如何在可执行文件中成功读取文本文件?
您遇到的问题是您正在加载的文本文件格式。
因为你有很多空格
string[] strFloats = text.Split (new char[0]);
会导致一些字符串为空。
要解决此问题,请从文本文件中删除多余的空格或使用:
for(int j = 0; j<strFloats.Length; j++){
if (string.IsNullOrEmpty (strFloats [j]))
continue;
floats[j] = float.Parse(strFloats[j]);
}
我在 Unity3D 中读取文本文件时遇到问题。 我创建了一个 returns 类型为 float[][] 并将流阅读器作为参数的方法:
public float[][] CreateWeights(StreamReader reader){
int n = 0;
float[][] Weights = new float[50][];
while((!reader.EndOfStream)){
string text = reader.ReadLine();
if (text == null)
break;
string[] strFloats = text.Split (new char[0]);
float[] floats = new float[strFloats.Length];
for(int i = 0; i<strFloats.Length; i++){
floats[i] = float.Parse(strFloats[i]);
}
Weights[n] = floats;
n++;
}
return Weights;
}
我在 void Start() 中使用这个方法来创建 "weights":
float[][] WeightsIH;
float[][] WeightsHO;
void Start(){
FileInfo theSourceFile = new FileInfo(Application.dataPath + "/Resources/WeightsIH.txt");
StreamReader reader = theSourceFile.OpenText();
FileInfo theSourceFile2 = new FileInfo(Application.dataPath + "/Resources/WeightsHO.txt");
StreamReader reader2 = theSourceFile2.OpenText();
WeightsIH = CreateWeights(reader);
WeightsHO = CreateWeights(reader2);
Yhidden = new float[50][];
HiddenOutput = new float[50][];
Xoutput = new float[1];
}
这在 Unity 的播放模式下会很好地工作。但是,在创建可执行文件后,将找不到这些文件,我明白这一点。所以为了让它工作,我知道我需要使用 Resources.Load 并且我有:
void Start(){
TextAsset text1 = Resources.Load("WeightsIH") as TextAsset;
TextAsset text2 = Resources.Load("WeightsHO") as TextAsset;
WeightsIH = CreateWeights(text1);
WeightsHO = CreateWeights(text2);
Yhidden = new float[50][];
HiddenOutput = new float[50][];
Xoutput = new float[1];
}
当然参数类型不能再是streamReader了,我改成了TextAsset作为参数。这是它的变化:
public float[][] CreateWeights(TextAsset textAsset){
float[][] Weights = new float[50][];
string[] linesFromFile = textAsset.text.Split("\n"[0]);
for(int i = 0; i<linesFromFile.Length; i++){
string[] strFloats = linesFromFile[i].Split (new char[0]);
float[] floats = new float[strFloats.Length];
for(int j = 0; j<strFloats.Length; j++){
floats[j] = float.Parse(strFloats[j]);
}
Weights[i] = floats;
}
return Weights;
}
现在这根本行不通,甚至在播放模式下也不行。我得到的 运行-time 错误如下:
FormatException: Invalid format.
System.Double.Parse (System.String s, NumberStyles style, IFormatProvider provider) (
at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System/Double.cs:209) System.Single.Parse (System.String s) (
at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System/Single.cs:183) FollowShortestPath.CreateWeights (UnityEngine.TextAsset textAsset) (
at Assets/Scripts/Pathfinding/FollowShortestPath.cs:203) FollowShortestPath.Start () (
at Assets/Scripts/Pathfinding/FollowShortestPath.cs:54)
第54行指的是:
WeightsIH = CreateWeights(text1);
第203行是指:
floats[j] = float.Parse(strFloats[j]);
我做错了什么?如何在可执行文件中成功读取文本文件?
您遇到的问题是您正在加载的文本文件格式。
因为你有很多空格
string[] strFloats = text.Split (new char[0]);
会导致一些字符串为空。
要解决此问题,请从文本文件中删除多余的空格或使用:
for(int j = 0; j<strFloats.Length; j++){
if (string.IsNullOrEmpty (strFloats [j]))
continue;
floats[j] = float.Parse(strFloats[j]);
}