Topic: Save Playerprefs OnlineMapsDrawPoly shapes in XML and load on startup

Hi, im trying to save and load polygons.
When I load saved polygons I realise that are not in the exact position.
Is there any other way to store points of each polygon to double values?
It will be more accurate with double values instead float.

 
         // Save Polygons to Playerprefs
        foreach (OnlineMapsDrawingPoly shape in SavedPolys)
        {
            OnlineMapsXML shapeNode = xml.Create("Poly");

            shapeNode.Create("Name", shape.name);
            shapeNode.Create("Color", shape.borderColor);
            shapeNode.Create("Width", shape.borderWidth);

            List<string> points = new List<string>();

            foreach (Vector2 r in shape.points)
            {
                points.Add(r.ToString());
            }

            shapeNode.Create("Points", string.Join(",", points));
        }

        PlayerPrefs.SetString("Shapes", xml.outerXml);
        PlayerPrefs.Save();



       // Load Polygons  from Playerprefs
        if (!PlayerPrefs.HasKey("Shapes")) return;

        string xmlContent = PlayerPrefs.GetString("Shapes");
        OnlineMapsXML xml = OnlineMapsXML.Load(xmlContent);

        if (xml != null) {

            foreach (OnlineMapsXML shapeNode in xml)
            {
               if (shapeNode.name == "Poly")
                {

                    string pointsStr = shapeNode.Get<string>("Points");

                    pointsStr = pointsStr.Replace(")", "");
                    pointsStr = pointsStr.Replace("(", "");

                    string[] pointsArray = pointsStr.Split(',');

                    List<Vector2> points = new List<Vector2>();
                    for (int i = 0; i < pointsArray.Length; i += 2)
                    {
                        float x = float.Parse(pointsArray[i]);
                        float y = float.Parse(pointsArray[i + 1]);
                        points.Add(new Vector2(x, y));
                    }

                    OnlineMapsDrawingPoly shape = new OnlineMapsDrawingPoly(points);

                    shape.name = shapeNode.Get<string>("Name");
                    shape.borderColor = shapeNode.Get<Color>("Color");
                    shape.borderWidth = shapeNode.Get<float>("Width");

                    OnlineMapsDrawingElementManager.AddItem(shape);

                    SavedPolys.Add(shape);
                }
        }

Re: Save Playerprefs OnlineMapsDrawPoly shapes in XML and load on startup

Hi.

Yes, do the following:
1. Use OnlineMapsVector2d instead of Vector2, everywhere when working with polygon points.
2. Use double.Parse instead of float.Parse, when reading points from xml.

Kind Regards,
Infinity Code Team.

Boost your productivity a lot and immediately using Ultimate Editor Enhancer. Trial and non-commerce versions available.

Re: Save Playerprefs OnlineMapsDrawPoly shapes in XML and load on startup

That's great!
Thank you