- Home /
Loading XML causes game freeze
How would you load an XML file without locking up the whole game interface?
at the moment my code is like this. It compiles fine and does its job. Its just it takes a few seconds before anything can animate again.
var xmldoc = new System.Xml.XmlDocument();
var iospath : String = Application.dataPath.Substring (0, Application.dataPath.Length - 5);
iospath = iospath.Substring(0, iospath.LastIndexOf("/"));
iospath = iospath + "/Documents";
var fileFullName : String;
fileFullName = iospath + "/MinorItem.xml";
if (!File.Exists(fileFullName)) {
//Debug.Log ("File not exists");
var tWWW = new WWW("https://whatever.com/xml/xmlGetMinorItem.asp");
yield tWWW;
Debug.Log(tWWW.text);
var swtr : StreamWriter = new StreamWriter(fileFullName);
Debug.Log(swtr.Encoding);
swtr.Write(tWWW.text);
swtr.AutoFlush = true;
swtr.Close();
Debug.Log ("File exists:" + File.Exists(fileFullName)) ;
}
var srdr : StreamReader = File.OpenText(fileFullName);
var xmlString : String = "";
xmlString = srdr.ReadToEnd();
xmldoc.LoadXml(xmlString);
var node : System.Xml.XmlNode;
var nodeList : System.Xml.XmlNodeList;
nodeList= xmldoc.SelectNodes(GalleryXMLPath);
var t : GameObject;
var sname : String;
var productcode : String;
var textData :TextMesh;
node = nodeList.ItemOf[0];
var iTotal : Int16 = int.Parse(node.Item["TOTALPAGES"].InnerText);
PagesCount = iTotal ;
}
Answer by CHPedersen · Jun 14, 2011 at 06:25 AM
Loading an XML-file is not a task that requires Unity API, so you're free to multithread it. You can make the XmlDocument
a class variable, then move the code that calls its LoadXml-function into a different function, then start a thread that executes that function. This will load your XML and leave the mainthread free to keep rendering frames at the same time.
that doesn't work, you will get exception at the moment when you pass "www" var to new thread's method
Answer by DaveA · Jun 14, 2011 at 08:23 AM
See also http://answers.unity3d.com/questions/59394/parsing-large-xml-files-on-the-fly-best-practice.html
Your answer
Follow this Question
Related Questions
Editing an XML File 1 Answer
How Load XML File in java? 2 Answers
Save and load arrays into an XML file 1 Answer
A node in a childnode? 1 Answer
Problem loading XML files after build using .NET libraries 1 Answer