I am populating a few elements like a ScrollView with data from a few XML files. By clicking a button, the most recent files are being fetched from a server. I am trying to display them directly in the GUI, but I always have to restart the application in order to see the last downloaded version. Downloading and writing works properly.
Here's some code:
First the downloading/updating process
public bool update() {
try {
// Download + Save
WebClient wc = new WebClient();
wc.Credentials = new NetworkCredential("anonymous", "");
wc.DownloadFile(_address_employeesXML, _filePathToXMLImport + "/employees.xml");
XMLReader test = new XMLReader(logController);
test.readXMLFiles();
Debug.Log("Update successful");
} catch (Exception e) {
#if UNITY_EDITOR
EditorUtility.DisplayDialog("Error", e.Message, "OK");
#endif
XMLReader test = new XMLReader(logController);
//test.readXMLFiles();
Debug.LogException(e);
return false;
}
return true;
}
then the read process:
public bool readEmployees()
{
string pathToEmployeeXML = _filePathToXMLImport + "/employees.xml";
if (File.Exists(pathToEmployeeXML))
{
using (XmlReader reader = XmlReader.Create(pathToEmployeeXML))
{
while (reader.Read())
{
// Only detect start elements.
if (reader.IsStartElement())
{
switch(reader.Name)
{
case "employee":
XmlReader inner = reader.ReadSubtree();
inner.ReadToDescendant("firstName");
string firstName = inner.ReadElementString();
inner.ReadToDescendant("lastName");
string lastName = inner.ReadElementString();
generateEmployee(firstName, lastName);
break;
}
}
}
}
return true;
}
return false;
}
I found this thread: Reloading XML asset in Unity Which unfortunately is not really solving my problem.
I also tried using AssetDatabase.Refresh, but didn't really find out how to implement it to be honest.
EDIT:
So now I changed it up to use File.IO and to load the files async. I still need to restart the app to see the changes though.
public class XMLController : MonoBehaviour {
public MessageLogsController logController;
public ApplicationStateSaver saver;
const string _address = "ftp://127.0.0.1:2121/";
const string _address_maintenancePlansXML = _address + "maintenancePlans.xml";
const string _address_employeesXML = _address + "employees.xml";
const string _address_materialsXML = _address + "materials.xml";
const string _address_machinesXML = _address + "machines.xml";
const string maintenancePlans = "maintenancePlans";
const string employees = "employees";
const string materials = "materials";
const string machines = "machines";
// Use this for initialization
void Start () {
XMLReader XMLReader = new XMLReader(logController);
StartCoroutine(GetXML(_address_maintenancePlansXML, maintenancePlans));
StartCoroutine(GetXML(_address_employeesXML, employees));
StartCoroutine(GetXML(_address_materialsXML, materials));
StartCoroutine(GetXML(_address_machinesXML, machines));
XMLReader.readXMLFiles();
saver.Initialize();
}
public void WriteInformationToXML()
{
XMLWriter writer = new XMLWriter();
writer.writeXML();
}
IEnumerator GetXML(string link, string file_name) {
UnityWebRequest www = UnityWebRequest.Get(link);
yield return www.SendWebRequest();
Debug.Log("Success");
if(www.isNetworkError || www.isHttpError) {
Debug.Log(www.error);
}
else {
// Show results as text
Debug.Log(www.downloadHandler.text);
string savePath = string.Format("{0}/{1}.xml", Application.persistentDataPath + "/Files/XML/Import/", file_name);
System.IO.File.WriteAllText(savePath, www.downloadHandler.text);
Debug.Log("Files written");
}
}