Save and Load arrays into an XML file

Hi,

I have to save and load data for my game, i’ve got bools, int, floats, arrays of bools int strings or float, 2d arrays,…
I can’t find anything about that…

I’m going to try to use a part the ArrayPrefs script from the wiki unify:

#region Int Array

    /// <summary>
    /// Stores a Int Array or Multiple Parameters into a Key
    /// </summary>
    public static bool SetIntArray(string key, params int[] intArray)
    {
        if (intArray.Length == 0) return false;

        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        for (int i = 0; i < intArray.Length - 1; i++)
            sb.Append(intArray[i]).Append("|");
        sb.Append(intArray[intArray.Length - 1]);

        try { PlayerPrefs.SetString(key, sb.ToString()); }
        catch (Exception e) { return false; }
        return true;
    }

    /// <summary>
    /// Returns a Int Array from a Key
    /// </summary>
    public static int[] GetIntArray(string key)
    {
        if (PlayerPrefs.HasKey(key))
        {
            string[] stringArray = PlayerPrefs.GetString(key).Split("|"[0]);
            int[] intArray = new int[stringArray.Length];
            for (int i = 0; i < stringArray.Length; i++)
                intArray[i] = Convert.ToInt32(stringArray[i]);
            return intArray;
        }
        return new int[0];
    }

I’ll use that to save each array into a single string, and I will split the 2d arrays into simple arrays wich will be saved into a single string aswell.

Do you think it’s doable?
It’s for IOS, can it be problematic performances-wise ?
Anyone has a better idea or found something which does that already ?

Thanks for the help

I’ll post my research progress…

I guess you can try to look at the .NET XmlSerializer class (System.Xml.XmlSerialization) which should work with built-in arrays. I know I saw a forum post with both a C# and US implementation awhile ago, can’t remember where though. Also I haven’t tried it in iOS, but if it works you should go with that option because PlayerPrefs can be pretty slow if you need to write to the disc continuously.

I had a recent issue with trying to get arrays to save to XML.
Here is my question on Unity Answers regarding that: Array problem - Questions & Answers - Unity Discussions
And here is the original XML connector that I’ve been modifying:
http://www.unifycommunity.com/wiki/index.php?title=Save_and_Load_from_XML