Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从文本文件中移动具有点列表的对象_C#_Unity3d_Format_Formatexception - Fatal编程技术网

C# 从文本文件中移动具有点列表的对象

C# 从文本文件中移动具有点列表的对象,c#,unity3d,format,formatexception,C#,Unity3d,Format,Formatexception,我正在尝试将多维数据集从一个文本文件的坐标点移动到另一个点 public class cube : MonoBehaviour { // Speed public float speed = 3.0f; // Start is called before the first frame update void Start() { print("cube says hi"); } // Update is called o

我正在尝试将多维数据集从一个文本文件的坐标点移动到另一个点

public class cube : MonoBehaviour
{
    // Speed
    public float speed = 3.0f;

    // Start is called before the first frame update
    void Start()
    {
       print("cube says hi");

    }

    // Update is called once per frame
    void Update()
    {

        string path = "Assets/Ressources/test.txt";
        var sr = new StreamReader(path);
        List<string> columnx = new List<string>();
        List<string> columny = new List<string>();
        List<string> columnz = new List<string>();

        using (sr)
        {
            while (!sr.EndOfStream)
            {
                string line = sr.ReadLine();
                var values = line.Split(new string[] { "      " }, System.StringSplitOptions.RemoveEmptyEntries);

                columnx.Add(values[0]);
                columny.Add(values[1]);
                columnz.Add(values[2]);

            }


        }


        float step = speed * Time.deltaTime;
        transform.position = Vector3.MoveTowards(transform.position,
                                            new Vector3(
                                                Convert.ToSingle("1.45", CultureInfo.InvariantCulture), Convert.ToSingle("3.258", CultureInfo.InvariantCulture), Convert.ToSingle("4.256", CultureInfo.InvariantCulture)
                                            ), step);

    }

}
公共类多维数据集:单行为
{
//速度
公共浮子速度=3.0f;
//在第一帧更新之前调用Start
void Start()
{
打印(“立方体说你好”);
}
//每帧调用一次更新
无效更新()
{
字符串path=“Assets/resources/test.txt”;
var sr=新的StreamReader(路径);
List columnx=新列表();
List columny=新列表();
List columnz=新列表();
使用(sr)
{
而(!sr.EndOfStream)
{
字符串行=sr.ReadLine();
var values=line.Split(新字符串[]{”“},System.StringSplitOptions.RemoveEmptyEntries);
columnx.Add(值[0]);
添加(值[1]);
第Z列添加(值[2]);
}
}
浮动步长=速度*时间.deltaTime;
transform.position=Vector3.向(transform.position,
新矢量3(
Convert.ToSingle(“1.45”,CultureInfo.InvariantCulture),Convert.ToSingle(“3.258”,CultureInfo.InvariantCulture),Convert.ToSingle(“4.256”,CultureInfo.InvariantCulture)
),步骤);
}
}
这是可行的,但问题是当我用columnx[0]columny[0]和columnz[0]替换“1.45”、“3.25”和“4.25”时,我得到了

FormatException: Input string was not in a correct format.
System.Number.ParseSingle (System.String value, System.Globalization.NumberStyles options, System.Globalization.NumberFormatInfo numfmt) (at <437ba245d8404784b9fbab9b439ac908>:0)
FormatException:输入字符串的格式不正确。
System.Number.ParseSingle(System.String值、System.Globalization.NumberStyles选项、System.Globalization.NumberFormatInfo numfmt)(位于:0)

我想用第一个元素进行测试,这样我就可以进行for循环,但它甚至不能用0进行测试。

我解决了这个问题!我只是用一个空格来分隔,而不是5或6(这取决于是否有a-)。。。我打印了我的专栏,他们都在工作!谢谢你! 但现在我尝试使用for循环将对象从一个向量移动到另一个向量:

 for (int i =0; i< columnx.Count ; i++)
        {

            position = new Vector3(Convert.ToSingle(columnx[i], CultureInfo.InvariantCulture), Convert.ToSingle(columny[i], CultureInfo.InvariantCulture), Convert.ToSingle(columnz[i], CultureInfo.InvariantCulture));


            transform.position = Vector3.MoveTowards(currentPosition, position, step);


        }
for(int i=0;i

但看起来立方体在最后一点立即运行

我想你有一个这样的文件,带有标签,数字之间的空格

        0.10340200    0.01262700   0.46301100



    0.10340200        0.01262700 0.46301100        
        0.10340200 0.01262700 0.46301100         

我建议您直接使用Vector3列表,并将其转换为Float而不是single,因为Vector3是一个3 Float的向量,因此如果您转换为single,则会再次执行另一个转换(并且会降低精度)

List vec=new List();
字符串path=“Assets/file.txt”;
var fileLines=System.IO.File.ReadAllLines(路径);
foreach(文件行中的var行)
{
var result=line.Split(新字符[]{'\t','},StringSplitOptions.RemoveEmptyEntries);
如果(result.Length==3)
{
var x=float.Parse(结果[0],CultureInfo.InvariantCulture);
var y=float.Parse(结果[1],CultureInfo.InvariantCulture);
var z=float.Parse(结果[2],CultureInfo.InvariantCulture);
矢量添加(新矢量3(x,y,z));
}
}
浮动步长=速度*时间.deltaTime;
transform.position=Vector3.向(transform.position,
vec[0],阶跃);

尝试在
transform.position=…
之前使用
Debug.LogError(“““+columnx[0]”)来更准确地查看数组中的内容。我得到了0.10340200“UnityEngine.Debug:LogError(Object)cube:Update()(位于Assets/Scenes/cube.cs:52),我的列X的第一个值是数组的数字部分还是LogError格式的一部分后的单引号
?对不起,我粘贴错了,它是“0.10340200”(之前有5个空格),我不知道为什么我还有一些空格。。。我的文本文件看起来像0.10340200 0.01262700 0.46301100(但行和列之间有许多不同的空白大小…这是原因吗?将您的文本文件发布到某个地方,我们可以加载或显示它..这将更易于分析,您将快速找到解决方案控制台打印:ArgumentOutOfRangeException:索引超出范围。必须为非负数且小于集合的大小。)在transform.position=Vector3.moveToward(transform.position行)处,我没有错误,因此您确定vec列表中有数据吗?添加Debug.Log(vec.Length).我不明白你为什么会有错误..所以如果你的解决方案是正确的..没有问题进行更多的调查当我打印数据时,它会产生相同的错误,我的文件就像你显示的一样,没有表格,只有空格,但我不认为这是问题所在这是因为for循环在很短的时间内执行(远小于1帧).这可能是一个新问题。
    List<Vector3> vec = new List<Vector3>();

    string path = "Assets/file.txt";

    var fileLines = System.IO.File.ReadAllLines(path);
    foreach (var line in fileLines)
    {
        var result = line.Split(new char[] { '\t', ' ' }, StringSplitOptions.RemoveEmptyEntries);
        if (result.Length == 3)
        {
            var x = float.Parse(result[0], CultureInfo.InvariantCulture);
            var y = float.Parse(result[1], CultureInfo.InvariantCulture);
            var z = float.Parse(result[2], CultureInfo.InvariantCulture);
            vec.Add(new Vector3(x, y, z));
        }

    }

    float step = speed * Time.deltaTime;
    transform.position = Vector3.MoveTowards(transform.position,
                                        vec[0], step);