- Home /
I decided to try and change encryption method to something a bit easier. More info about the problem here http://forum.unity3d.com/threads/ways-to-encrypt-serialize-my-game-data.349662/
BinaryFormatter to string
Hi
I currently have a BinaryFormatter that serializes player levels and all that stuff. But I also need to be able to upload these levels to a webserver and the webserver can only take strings for data storage. So I can make the deserialized info into a string?
The level file is called by using FileStream. It finds the file at a location and serializes/deserializes it. It then gets all the variables I've set in a class and then assign the needed variables with the right data from the deserialized class.
I am not sure of what code I will show since it is pretty basic but if you want to know how I did the serialization, then go here. It starts around 30-31 minutes in.
Thanks in advance!
EDIT: I realized it was bit short on information so I added a bit more.
I guess I'm not quite clear on what you are asking. You have the option to either "stringify" you serialized data (the file), or your deserialized data (instances of classes).I'm not sure which will work best for you, you'll need to clarify that.
However, I should mention that most unity classes have an overridable function called ToString(), which converts the instance data into a string. I usually use this for debug info, but it should work for your purposes to.
@Glurth What I basically need to do is to deserialize the data from a file and then turn it all into one string so I can upload it to a webserver. I did also try and do something with the .ToString() function but failed. I will try again since I don't remember exactly what I did in order to fail.
Ok, so we are converting instances into "serialized strings". $$anonymous$$eep in $$anonymous$$d you will be doing, in essence, EXACTLY what you need to do in your serialize function, except that rather than keeping everything as binary data, it is converted to strings.
You mentioned you tried this and it failed, but I think you CAN get it working this way once you work out the bugs.
One other thing to consider is how this data will be received by the webserver. If it is unaware of the structure of your data, simply sending data alone may not be sufficient! You might choose to include FIELD NA$$anonymous$$ES also in your "serialized string". This will allow the webserver to handle (deserialize) the data without the need for perfect awareness of your class structure.
Anyone here know, how to store to data to web server, after serialization ?? I did using making prefabs, and store it in the resources folder. Other option is using playerprefs. But i want to store level specific data such as obstacles, enemies positions, its type, etc. There are currently 50 levels in the game. I am finding ideal way of doing it. Links would be appriciable.
Thanx.
Answer by LaneFox · Aug 11, 2015 at 04:50 PM
Here's how I'm doing it. You can probably apply this to your situation.
private static string SerializeDataToString(OptionsData staticOptions)
{
MemoryStream ms = new MemoryStream();
XmlSerializer xs = new XmlSerializer(typeof(OptionsData));
XmlTextWriter tw = new XmlTextWriter(ms, Encoding.UTF8);
xs.Serialize(tw, staticOptions);
ms = (MemoryStream)tw.BaseStream;
string xmlizedString = UtfToString(ms.ToArray());
return xmlizedString;
}
private static string UtfToString(byte[] bytes)
{
UTF8Encoding encoding = new UTF8Encoding();
string constructedString = encoding.GetString(bytes);
return (constructedString);
}
Follow this Question
Related Questions
How to add strings in a list AND be able to save it and load it? Using playerpref OR serialization 2 Answers
BinaryFormatter Deserialization Problem 0 Answers
Using BinaryFormatter for deserialization - ArgumentException: method argument length mismatch 0 Answers
How do you deserialize string into Vector3 using XMLSerializer? 0 Answers