XML data at runtime? Saving static options from editor to runtime?

I’m trying to make an Options EditorWindow where I can toggle certain features on/off. Like floating text damage or something. Bools and floats, basic stuff.

I first (for some reason) I tried using a Static class but of course that failed when all the data reset when I closed the editor… So I decided to try making an XML file that I could push the settings to, then read them later.

That worked fine until I made a Build and the XML file was compiled into oblivion and no longer in my Resources directory.

So my question here is How do I either make this XML data available and persistent, or how could I alternatively approach the ‘options window’? I do not want a class floating around on some GameObject to handle this, there must be a good way to do this behind the scenes.

Thanks

Right, I have one example, which is in russian (good for me, but might not be for others :smiley: )

Basically the guy is looking at how to walk through the joined cells (rooms) through the doors.
Every cell can have a chair, a lamp, a table, etc, which can be thrown arround. As follows, their position, rotation and scale have to be saved as the person leaves the room, and need to be restored as he enters a room

He shows how it works with PlayerPrefs, but then shows how to implement a custom Serializator class which works with XML files.
Afterwards, he explains how to make a menu (which is not really important)

if you are ready to dig through a lot of russian it’s a way out :confused:

How To XML/JSON

To serialize/desrialize your options data to XML you can use the normal .NET C# classes in “System.Xml” See these links:

Alternatively, you could use JSON, which I like more :slight_smile:

On the xml @ resources

I haven’t encountered the problem you describe, not being able to access the XML file in the resources folder. Did you do something like this to load it?

TextAsset xmlAsset = (TextAsset) Resources.Load("YourXMLFile");
XmlDocument document = new XmlDocument();
document.LoadXml(xmlAsset.text);

Default / Custom settings, saving your own xml file

What I generally do is use such on XML file or a scriptable object to contain the “default” settings. Once those are loaded, you create a custom XML file which fill function as the player’s “custom settings” in (for instance) the Application.persistentDataPath.

When initializing your settings, you can then check if the “custom settings” xml file exists and use that instead of the default!

Well it turns out that this was caused by a simple mistake and my code was working aside from this weird ‘gotcha’ in Unity. Thanks for the answers below because there is indeed some good information there that one would consider when dealing with this topic.

The problem was very weird and related to how TextAsset reads versus how Resources.Load() searches. In my case I had to save (Editor) the xml file in a Resources folder with the file extension, then load (Build) via Resources.Load omit the file extension.

I assume this is because TextAsset uses the file extension to confirm it is indeed a data form that it can accept (see docs for file type list) but Resources.Load() needs you to omit the file extension or it will get confused.

http://forum.unity3d.com/threads/how-to-read-compiled-xml-from-resources-load-at-runtime.345956/#post-2238764