I am trying to save a Javascript array of string Arrays. I need to save this every time I do work in the game. So it looks like serialization is the best method, short of going through a massive for loop and using ArrayPrefs2 on every single damn array. So this serialization thing, seems like a smart way to do it. I have three questions.

  1. Is it possible to Serialize a Javascript array?
  2. Is it possible to Serialize a 2D array?
  3. How do you actually go about Serialization?
    I really have no clue, I see a lot of talk about it, mostly in C#, but I’m working in JavaScript, so if someone could show me an example of Babies first Serialization or point me in the direction of a good tutorial I would be very thankful.

You can use BinaryFormatter to store 2D arrays - I presume you want your array stored in a string for this example:

#pragma strict
import System.Runtime.Serialization.Formatters.Binary;
import System.Runtime.Serialization;
import System.IO;
var ar = new float[2,2];
function Start () {
	var o = new MemoryStream(); //Create something to hold the data
	ar[1,1] = 9;  //Set a value to test
	var bf = new BinaryFormatter(); //Create a formatter
	bf.Serialize(o, ar); //Save the array
	var data = Convert.ToBase64String(o.GetBuffer()); //Convert the data to a string

	//Reading it back in
	var ins = new MemoryStream(Convert.FromBase64String(data)); //Create an input stream from the string
	//Read back the data
	var x : float[,] = bf.Deserialize(ins);
	print(x[1,1]); //Make sure we have the same value


From memory you can serialize a 2D array. If that doesn’t work for you, store it as a 1D array.