【问题标题】:Move an object with a list of points from a text file从文本文件中移动带有点列表的对象
【发布时间】:2020-05-09 12:07:44
【问题描述】:

我正在尝试将我的 Cube 从坐标来自文本文件的一个点移动到另一个点。

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

    }

}

这可行,但问题是当我用 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)

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

【问题讨论】:

  • 尝试使用Debug.LogError("'" + columnx[0] "'")before transform.position = ... 更准确地查看数组中的内容。
  • 我得到了 0.10340200' UnityEngine.Debug:LogError(Object) cube:Update()(在 Assets/Scenes/cube.cs:52),这是我的 columnx 的第一个值
  • 单引号'是数组的数字部分还是LogError格式的一部分?
  • 对不起,我粘贴错了,它是'0.10340200'(之前有5个空格)我不知道为什么我还有一些空间......我的文本文件看起来像这样0.10340200 0.01262700 0.46301100(但有很多行和列之间的空白大小不同...这是原因吗?
  • 将文本文件发布到我们可以下载或展示的地方......这样更容易分析,您将很快找到解决方案

标签: c# unity3d format formatexception


【解决方案1】:

我解决了这个问题!我只是放了一个空格来分割而不是 5 或 6(这取决于是否有 - 或没有)......我打印了我的列,它们都在工作!感谢那 ! 但现在我正在尝试使用 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 循环在很短的时间内执行(远小于 1 帧)。这可能是一个新问题。
【解决方案2】:

我想你有一个这样的文件,带有制表符,数字之间有空格

        0.10340200    0.01262700   0.46301100



    0.10340200        0.01262700 0.46301100        
        0.10340200 0.01262700 0.46301100         

我建议您直接使用 Vector3 的列表并转换为浮点数而不是单数,因为 Vector3 是 3 个浮点数的向量,因此如果您转换为单数,则会再次进行另一次转换(并且您会失去精度)。 .

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

【讨论】:

  • 控制台打印:ArgumentOutOfRangeException:索引超出范围。必须是非负数且小于集合的大小。在 transform.position = Vector3.MoveTowards(transform.position line
  • 我没有错误,所以你确定vec列表中有数据吗?添加 Debug.Log(vec.Length)。我不明白你为什么有错误..所以如果你的解决方案没问题..没问题调查更多
  • 当我打印数据时,它会出现同样的错误,我的文件就像你展示的那样,没有表格,只有空格,但我认为这不是问题
猜你喜欢
  • 2016-10-26
  • 1970-01-01
  • 1970-01-01
  • 2015-05-17
  • 1970-01-01
  • 2023-03-21
  • 2023-04-06
  • 2011-01-16
  • 1970-01-01
相关资源
最近更新 更多