Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

266
Visualizações
La función Unity XmlDocument no siempre funciona

¿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?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

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 function

Recibí ayuda de este enlace: http://answers.unity3d.com/questions/9538/new-to-unity-3d-gui-button-help-needed-please

over 4 years ago · Santiago Trujillo Relatório

0

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(); }
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda