- Home /
Sate Saving
The game I'm making is going to be very complicated, there will be several things that move and hundreds of separate variables, it would take weeks of work to save it all using PlayerPrefs. Is there an easier way to do saving, like saving the state that the scene is in?
This may be able to help http://program$$anonymous$$gtutorials.thinkific.com/courses/5-methods-to-saving-and-loading-your-game-data-in-unity3d
If you use the coupon code SAVE$$anonymous$$E you get 50% off.
Answer by Slobdell · Aug 08, 2013 at 12:04 AM
I'm going ahead assuming that your using C#. FIrst off there are some libraries that will do this. See here http://forum.unity3d.com/threads/138678-Unity-Save-Game-Level-Serialization. For myself I've never needed the entire scene saved. So what I do is create a class that stores all the data I'm looking to save. Then using the XMLSerializer class for C#, see here http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer.aspx, I serialize my class into XML which is just text and save it as a string to one SINGLE playerprefs variable. I can explain further if you need, let me know
Actually that X$$anonymous$$LSerializer link I sent you looks like more than you need. This how I serialize my player class. I have one going the other way too makes converting back and forth as simple as calling one method.
public static String toX$$anonymous$$L(Player player){
XmlSerializer x = new XmlSerializer(typeof(Player));
$$anonymous$$emoryStream memoryStream = new $$anonymous$$emoryStream();
XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, new System.Text.UTF8Encoding(false));
x.Serialize(xmlTextWriter, player);
memoryStream = ($$anonymous$$emoryStream)xmlTextWriter.BaseStream;
return System.Text.Encoding.UTF8.GetString(memoryStream.ToArray());
}
Also, something I learned the hard way, this line is key "new XmlTextWriter(memoryStream, new System.Text.UTF8Encoding(false));" . The false turns of BO$$anonymous$$, Using different encoding gave me all kinds of errors due to Unity encoding acceptance and byte order marks which I don't even know what that really means but it's bad in this case.
It works :) thanks, ill put you in the credits for my game!
you just saved me hours of coding to try to use PlayerPrefs to do this, Thanks so much.
Your answer
![](https://koobas.hobune.stream/wayback/20220613111735im_/https://answers.unity.com/themes/thub/images/avi.jpg)