x


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.

  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.
more ▼

asked Jun 08 '12 at 03:47 AM

easilyBaffled gravatar image

easilyBaffled
92 12 28 38

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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
}
more ▼

answered Jun 08 '12 at 07:26 AM

whydoidoit gravatar image

whydoidoit
33.1k 11 23 100

No http:// in the http://System.IO line - I can't stop it inserting it!

If you want generic level serialization then you can also have a go with the Unity Serializer on my blog

Jun 08 '12 at 07:28 AM whydoidoit

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(minus 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 MemoryStream(Convert.FromBase64String(data)); //Create an input stream from the string?

Jun 10 '12 at 03:21 AM easilyBaffled

Convert - I'm thinking that is System? Yep - checked [System.Convert.FromBase64String](http://msdn.microsoft.com/en-us/library/system.convert.frombase64string(v=vs.80).aspx)

Those other assumptions are dead right

Jun 10 '12 at 03:38 AM whydoidoit

Good to know I'm going somewhere now, but when import System.Convert; it throws Unknown identifier: 'Convert'. at me, when using Convert.FromBase64String.

Jun 10 '12 at 01:43 PM easilyBaffled

Not sure what's so difficult about writing System.IO... Anyway, I fixed it for you.

Jun 10 '12 at 03:01 PM syclamoth
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Jun 08 '12 at 07:15 AM

tomka gravatar image

tomka
283 3 6 9

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x295
x178
x133

asked: Jun 08 '12 at 03:47 AM

Seen: 2066 times

Last Updated: Jun 21 '12 at 02:50 PM