重要的编辑!!!!!! (我也编辑了代码)
我发现,当我和其他人一起做string[] pos = value.Split(',');
时,它做的是正确的事,但这是成功的事
统一地,字符串中的小数由,
表示,因此当我执行string[] pos = value.Split(',');
时,他除以发现的前三个逗号,并检查样本数据我刚刚进行了修改,并且将前三个逗号分开,就会得到实际结果
我也设法解决了问题
原始帖子:
我正在尝试学习统一性,我遵循this guide是为了拥有一个好的保存/加载系统(请注意,我删除了value = value.Replace(" ","");
行是因为它不需要):>
// Note: value is guaranteed to be a string of numbers in the format: "(1,2,3)"
public Vector3 StringToVector(string value)
{
value = value.Trim('(', ')');
value = value.Replace(" ","");
string[] pos = value.Split(',');
return new Vector3(float.Parse(pos[0]), float.Parse(pos[1]), float.Parse(pos[2]));
}
我认为这里存在错误,因为如果我在其他功能中使用上述功能,则X轴可以正常工作,而其他两个(Y和Z) 则不会 :
public virtual void Load(string[] values)
{
// in in the variable values[] i have in these positions:
// values[0] objectname, values[1] (x,y,z) position, values[2] (x,y,z) scale
transform.localPosition = SaveGameManager.Instance.StringToVector(values[1]);
transform.localScale = SaveGameManager.Instance.StringToVector(values[2]);
}
样本数据
string position = "(190,0, 2,5, 180,0)";
string scale = "(5, 5, 5)";
预期结果
-位置:<190, 2.5, 180>
-比例:<5, 5, 5>
实际结果
-位置:<190,0 0,0 2,0>
-比例:<5, 5, 5>
答案 0 :(得分:0)
我发现了99%的解决方案。
null
在value = value.Trim('(', ')');
value = value.Replace(" ","");
string[] pos = value.Split(',');
return new Vector3(float.Parse(pos[0]), float.Parse(pos[1]), float.Parse(pos[2]));
value = value.Replace(" ","");
并替换为pos[0] = pos[0] + ',' + pos[1];
pos[2] = pos[2] + ',' + pos[3];
pos[4] = pos[4] + ',' + pos[5];
与return new Vector3(float.Parse(pos[0]), float.Parse(pos[1]), float.Parse(pos[2]));
之所以有效,是因为我只是在其应有的地方放了一个逗号,仅此而已。
答案 1 :(得分:0)
统一地,字符串中的小数表示为
,
仅当您的计算机使用正确的语言环境时,这才是正确的。 Unity在float.ToString()
中使用Vector3.ToString()
,float.ToString()
尊重您PC的设置。
尝试将Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US", false);
放置到场景中任何对象的“唤醒”功能中,以强制将美国区域设置和句点用作小数点分隔符。
编辑:如果您想继续使用逗号作为小数点分隔符,则应该可以:
string[] numbers = value
.Replace("(", "")
.Replace(")", "")
.Replace(", ", "/")
.Split('/');
Vector3 v = new Vector3(float.Parse(numbers[0]), float.Parse(numbers[1]), float.Parse(numbers[2]));