我正在尝试使用 LitJson 从 Json 文件中读取数据并在 Unity 中构建网格。Json 数据的一部分是采用二维数组形式的多边形(如:)u'polygon0': [[2, 18.2], [6, 18], [4, 20]],u'polygon1': [[10, 18], [15, 18], [12, 15]],u'polygon2': [[4.746822657198926, 14.948995797109236],[4.500912085144255, 10.506835760070645],[15.500912085144254,10.506835760070645],[16.6, 15.3]]
。
现在我可以通过以下方式从特定的 json 文件中读取:
//read rectangular
for (int k = 0; k < 4; k++)
{
pol4 npol = new pol4();//pol4 is float(4,2)
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 2; j++)
{
npol.pol[i, j] = float.Parse(data[10 + k][i][j].ToString());//data is LitJson.JsonData type
}
}
it.pol4s.Add(npol);
}
但我想阅读任意数量的任意边多边形。所以我定义了一个列表来存储这些:public List<float[,]> polygons =new List<float[,]>();
并尝试将它们作为二维数组的列表读取:
int polyIndex = 0;
while (true){
string index = polyIndex.ToString();
string key = "polygon" + index;
polyIndex++;
try
{
Debug.Log(data[key]);//data is LitJson.JsonData type
}
catch (Exception e)
{
Console.WriteLine("{0} Exception caught.", e);
break;
}
JsonData poly =data[key];
int polySize = poly.Count;
float[,] po = new float[polySize, 2];
for (int i = 0; i < polySize; i++)
{
po[i,0] = float.Parse(poly[i][0].ToString);
po [i, 1] = float.Parse(poly [i] [1].ToString);
}
it.polygons.Add (po);
}
我收到以下错误:“float.Parse(string)”的最佳重载方法匹配有一些无效参数;完整错误:错误消息
我想知道如何存储这个坐标。