Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by easilyBaffled · Jun 08, 2012 at 03:47 AM · playerprefsserializationsaving

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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
 }
Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image whydoidoit · Jun 08, 2012 at 07:28 AM 0
Share

No http:// in the 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

avatar image easilyBaffled · Jun 10, 2012 at 03:21 AM 0
Share

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?

avatar image whydoidoit · Jun 10, 2012 at 03:38 AM 0
Share

Convert - I'm thinking that is System? Yep - checked System.Convert.FromBase64String.aspx)

Those other assumptions are dead right

avatar image easilyBaffled · Jun 10, 2012 at 01:43 PM 0
Share

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

avatar image syclamoth · Jun 10, 2012 at 03:01 PM 0
Share

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

Show more comments
avatar image
1

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges