【发布时间】: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] "'")beforetransform.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