重要编辑!
  (我也编辑了代码)
  
  


我发现当我同时执行string[] pos = value.Split(',');和其他命令时,它做的是正确的事情,但这很重要

统一地,字符串中的小数由,表示,所以当我执行string[] pos = value.Split(',');时,他将其发现的前三个逗号分开,如果您检查刚刚修改的Sample数据,并为前三个逗号分开,则会有实际结果

我也设法解决了规模

原始帖子:

我试图学习统一性,我遵循了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>

最佳答案

我发现了99%的解决方案。

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

return new Vector3(float.Parse(pos[0]), float.Parse(pos[2]), float.Parse(pos[4]));

之所以有效,是因为我只是将逗号放在应有的位置,仅此而已。

10-07 19:07
查看更多