¿Hay algo que considerar al usar la función XmlDocument en unity3d? Tengo este extraño problema: cuando se llama a la función que usa XmlDocument desde Awake() o OnGUI(), el documento se edita correctamente. Pero cuando se llama desde dentro de un evento de botón, aunque obtengo una cadena bien editada antes de guardar el documento, no puede modificar el documento en sí.
Función que edita el archivo (a veces):
public static void addTestProfile () { string path = Application.dataPath + "/Documents/Profiles.xml"; Hashtable tempTable = new Hashtable(); tempTable.Add("user", "chuck II"); tempTable.Add("url", "funny"); tempTable.Add("passwrod", "1234asdf"); General.StoreProfile(tempTable, path); } public static void StoreProfile(Hashtable profile, string path) { Debug.Log("profile to store name: " + profile["password"]); XmlDocument doc = new XmlDocument(); doc.Load(profilesPath); XmlElement element = doc.CreateElement("Profile"); XmlElement innerElement1 = doc.CreateElement("user"); innerElement1.InnerText = profile["user"] as string; element.AppendChild(innerElement1); XmlElement innerElement2 = doc.CreateElement("url"); innerElement2.InnerText = profile["url"] as string; element.AppendChild(innerElement2); XmlElement innerElement3 = doc.CreateElement("password"); innerElement3.InnerText = profile["password"] as string; element.AppendChild(innerElement3); doc.DocumentElement.AppendChild(element); doc.Save(profilesPath); Debug.Log(doc.InnerXml); }Creé un nuevo proyecto solo para probar este problema, el archivo no se edita cuando se llama justo antes de llamar a Application.loadLevel();
Aquí funciona bien y el archivo en sí está editado:
void OnGUI () { General.addTestProfile(); // General is the singleton class that contains the function implementation }Pero de alguna manera esto no funciona:
// GUI Save btn if (GUI.Button(new Rect(255, 20, 60, 35), "Add")) { General.addTestProfile(); // General is the singleton class that contains the function implementation Application.LoadLevel(0); }Cuando imprimo la cadena resultante justo antes de la función save() xmlDocument, muestra el nuevo elemento, pero de alguna manera el archivo xml sigue siendo el mismo. ¿Me estoy perdiendo algo importante tal vez relacionado con la orden de ejecución? ¿Algo así como tiempo de espera?
esto es solo una suposición descabellada, pero ¿podría funcionar?
// PSEUDOCODE bool pressed function OnGUI() if GUI.Button then pressed = true end if if pressed then addTestProfile(); end if end functionRecibí ayuda de este enlace: http://answers.unity3d.com/questions/9538/new-to-unity-3d-gui-button-help-needed-please
Cuando saltó de nuevo a la escena 1, estaba reescribiendo el archivo original.
¡"Debug.Log" apesta! , esta instrucción nunca imprimió la segunda llamada a la función createProfilesFile(). Así que solo faltaba una línea:
if (System.IO.File.Exists(profilesPath)) return;Aquí la función createProfilesFile():
public static void CreateProfilesFile (string path) { Debug.Log("create init"); // This line wasn't called the second time ... if (System.IO.File.Exists(path)) return; // Create a new file specified path XmlTextWriter textWriter = new XmlTextWriter(path,null); // Opens the document textWriter.WriteStartDocument(); // Write comments textWriter.WriteComment("This document contains the profiles that have been created."); textWriter.WriteStartElement("Profiles"); textWriter.WriteStartElement("Profile"); textWriter.WriteStartElement("user"); textWriter.WriteString("foo user"); textWriter.WriteEndElement(); textWriter.WriteStartElement("url"); textWriter.WriteString("foo url"); textWriter.WriteEndElement(); textWriter.WriteStartElement("password"); textWriter.WriteString("foo password"); textWriter.WriteEndElement(); textWriter.WriteEndElement(); textWriter.WriteEndElement(); // Ends the document. textWriter.WriteEndDocument(); // close writer textWriter.Close(); }