- Home /
Serialization
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.
Is it possible to Serialize a Javascript array?
Is it possible to Serialize a 2D array?
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.
Answer by whydoidoit · Jun 08, 2012 at 07:26 AM
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
}
I'm only trying to save an array in an object, and not entire objects, so I am going to go with your first answer for now($$anonymous$$us the "http://"). As to the first answer I have two questions. First what library does Convert.FromBase64String() come from? I looked for System.Convert but Unity is having none of it.
Second is it safe to assume that you would have PlayerPrefs.SetString("total", data); after var data = Convert.ToBase64String(o.GetBuffer()); //Convert the data to a string
then have a var data = PlayerPrefs.GetString("total"); before var ins = new $$anonymous$$emoryStream(Convert.FromBase64String(data)); //Create an input stream from the string?
Convert - I'm thinking that is System? Yep - checked System.Convert.FromBase64String.aspx)
Those other assumptions are dead right
Good to know I'm going somewhere now, but when import System.Convert; it throws $$anonymous$$ identifier: 'Convert'. at me, when using Convert.FromBase64String.
Not sure what's so difficult about writing System.IO
... Anyway, I fixed it for you.
Answer by tomka · Jun 08, 2012 at 07:15 AM
See http://unity3d.com/support/documentation/ScriptReference/SerializeField
From memory you can serialize a 2D array. If that doesn't work for you, store it as a 1D array.
Your answer
Follow this Question
Related Questions
Saving players progress in ScriptableObject asset 1 Answer
PlayerPrefs v. XML File for Saving Data? 1 Answer
Save/Load Animation State of Instantiated Prefabs 0 Answers
Need experienced opinion: Saving a players inventory: XML or PlayerPrefs? 1 Answer
SerializationException: serializationStream supports seeking, but its length is 0 1 Answer