【问题标题】:Troubles with reading text file after Build构建后读取文本文件的问题
【发布时间】:2016-01-20 15:37:01
【问题描述】:

我在 Unity3D 中读取文本文件时遇到问题。 我创建了一个返回类型 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() 中使用这个方法来创建“权重”:

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;
}

现在这根本不起作用,甚至在播放模式下也不起作用。我会得到的运行时错误如下:

FormatException:格式无效。

System.Double.Parse (System.String s, NumberStyles 样式, IFormatProvider 提供程序) (
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) (
位于 Assets/Scripts/Pathfinding/FollowShortestPath.cs:203) FollowShortestPath.Start() (
at 资产/脚本/寻路/FollowShortestPath.cs:54)

第 54 行指的是:

WeightsIH = CreateWeights(text1);

第203行指的是:

floats[j] = float.Parse(strFloats[j]);

我做错了什么?如何在可执行文件中成功读取文本文件?

【问题讨论】:

  • 如您所见,我删除了我的答案。但是你能显示你正在加载和解析的文本文件吗?
  • 确定:link
  • 好的,我找到了。检查我的答案

标签: c# unity3d executable streamreader


【解决方案1】:

您遇到的问题是您正在加载的文本文件格式。

因为你有很多空格

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]);

        }

【讨论】:

  • 感谢您的帮助。就像我说的:我知道在可执行的 Application.dataPath 中不起作用。这就是我将其更改为 Resources.Load 的原因。但话又说回来,这是正确的方法吗?如果是这样,我在修改后的代码中做错了什么?在我看来,它应该返回相同的结果..
  • 我明白了,很抱歉没有直接回答。让我试试,如果我找不到答案,我会删除这个。
  • 我想我找到了问题。
  • 原谅我的错误,但是代码最初没有.txt扩展名,然后它会报错。我最近尝试后不小心复制了代码。我把原来的帖子改了。另外,我尝试了这样的类型转换:WeightsH = CreateWeights((TextAsset)text1);等等。但正如预期的那样,这并没有改变任何东西。
  • 我明白了,然后我将删除这个答案,因为我找不到任何东西。对不起。
猜你喜欢
  • 2013-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-23
  • 2011-09-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多