- Home /
Different approach to loading XML data
Hi,
Recently, I had a problem that my tool (custom Editor Window) wasn't updating data I modified in XML file. I had to change focus from Unity to something else (for example internet browser) and then back to Unity to have the data updated in the tool. I found the source of the problem. However, I don't know why it wasn't working as I wanted. Here's what I did:
//Loading XML file
TextAsset textAsset = Resources.Load<TextAsset>("File");
doc = new XmlDocument();
doc.LoadXml(textAsset.text);
// (...)
//Saving XML file
doc.Save("Assets/Resources/File.xml");
It did worked to save newly made changes to the file but the tool didn't update the data. I was looking for an answer when I changed the first three lines with:
doc = new XmlDocument();
doc.Load("Assets/Resources/File.xml");
and the tool magically started updating all the changes.
My question is essentially: Why my first approach didn't work? Why loading the xml through Resources was forcing me to change system window focus? Is it because of some Unity Engine Resources reloading magic? Or did I just do that wrong way?
Answer by fafase · Jun 17, 2015 at 09:22 PM
http://docs.unity3d.com/ScriptReference/AssetDatabase.Refresh.html
You need to call that one when you change an asset by code.