- Home /
Using an XML file to tweak variables after build?
I was wondering whether or not this was possible?
Would I be able to, once a game has been built, simply edit an XML file stored in the same game_data folder that would contain, say, Speeds of characters, jump heights etc and not have to rebuild the whole game just to change one variable?
(A better example) -> So upon loading of the *.exe file, the game reads my XML file and applies the variable tweaks in the game, to save me having to rebuild the game every time I change something as small as a characters move speed for example.
I remember friends used to do this with Flash games, and I was wondering if it's possible to do this with a Unity game.
As ever, I muchly appreciate any and all help/insight into my inquiry, and a massive thanks to you all!
Answer by Landern · Oct 15, 2014 at 12:52 PM
Of course you can, XML is just text. You can deserialize it into an object when you load a scene, hell you could create your own in game editor to serialize it back to xml if you wanted(in case you have beta testers or something).
An example from a previously answered question(http://answers.unity3d.com/questions/651714/learning-how-to-deserialize-xml-file.html). The links explain my favorite way of designing xml documents, which is to decorate the classes with attributes that describe the xml structure, after that, (de)serializing is quite simple.
public static T DeserializeSettingsTemplate<T>(string path)
{
using(TextReader r = new StreamReader(path))
{
XmlSerializer s = new XmlSerializer(typeof(T));
return (T)s.Deserialize(r);
}
}
public static void SerializeSettingsTemplate(object dataToSerialize, string path)
{
using(TextWriter w = new StreamWriter(path))
{
XmlSerializer s = new XmlSerializer(dataToSerialize.GetType());
s.Serialize(w, dataToSerialize);
}
}
Thank you so much for answering my question again! You rock! I'll always be forever grateful!
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Serialize Variables from Items in an Array 1 Answer
Flip over an object (smooth transition) 3 Answers