Read Json to build meshes in Unity

1k Views Asked by At

I am trying to use LitJson to read data from a Json file and build meshes in Unity. Part of the Json data are polygons which take the form of 2-D arraies(like: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]]).

Now I am able to read from a specific json file by:

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

    }

But I want to read any number of any sides polygons. So I defined a list to store these:public List<float[,]> polygons =new List<float[,]>(); and try to read them as a list of 2-D array:

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

And I get the following error:The best overloaded method match for "float.Parse(string)" has some invalid arguments; full errors:error message

I wonder how can I get this coordinates stored.

1

There are 1 best solutions below

1
On BEST ANSWER

Replace

        po[i,0] = float.Parse(poly[i][0].ToString);
        po [i, 1] = float.Parse(poly [i] [1].ToString);

with

        po[i,0] = float.Parse(poly[i][0].ToString());
        po [i, 1] = float.Parse(poly [i] [1].ToString());

i.e. invoke the ToString method.