In the game development. XML is often used as a configuration file for skill configuration, map configuration, character action configuration, and so on.
Unity3d's built-in XML library makes it very easy to create new XML and read XML.
Here is an example of a new XML document. and read it.
Using unityengine;using system.collections;using system.io;using system.xml;using system.text;public class XmlTest: Monobehaviour {XmlElement m_rolemotions = null;//character action; XmlElement m_skills = null;//character skill;//Use the For Initializationvoid Start () {//createxml (); READXML (); Readfiletoxml ();} Update is called once per framevoid Update () {} void Createxml () {string filepath = Application.datapath + "/resources/1013000.xml"; if (! File.exists (filepath)) {//create XML instance; XmlDocument xmldoc = new XmlDocument (); Create character; XmlElement root = xmldoc.createelement ("character"); /*** Create rolemotions start***/XmlElement rolemotions = xmldoc.createelement ("rolemotions"); XmlElement motioninfo = xmldoc.createelement ("Motioninfo"); XmlElement motion = xmldoc.createelement ("Motion"); Motion. SetAttribute ("Clipname", "Enter_ready"); MOtion. SetAttribute ("Isloop", "false"); Motion. SetAttribute ("Moveendtime", "0"); Motion. SetAttribute ("Movestarttime", "0"); Motioninfo.appendchild (motion); Rolemotions.appendchild (Motioninfo); Root. AppendChild (rolemotions); /*** Create rolemotions end***//*** Create skills start***/XmlElement skills = xmldoc.createelement ("skills"); XmlElement skill = xmldoc.createelement ("skill"); Skill. SetAttribute ("Name", "General Attack"); Skill. SetAttribute ("Motion", "Rmt_attack1"); Skills. AppendChild (skill); Root. AppendChild (skills); /*** Create skills end***/xmldoc.appendchild (root); Xmldoc.save (filepath); } else {debug.logerror ("File hava exist"); }} void ReadXml () {string filepath = Application.datapath + "/resources/1013000.xml"; if (! File.exists (filepath)) {Debug.LogError ("XML File not exist"); Return } XmlDocument xmldoc = new XmlDocument (); Xmldoc.load (filepath); Get all child nodes; XmlNodeList nodeList = Xmldoc.selectsinglenode ("character"). ChildNodes; foreach (XmlNode nodeList) {if (child. Name = = "Rolemotions") {m_rolemotions = child as XmlElement; } else if (child. Name = = "Skills") {m_skills = child as XmlElement; }} Debug.Log ("m_rolemotions =" + M_rolemotions.innerxml); Debug.Log ("m_skills =" + m_skills. INNERXML); } void Readfiletoxml () {string filepath = "1013000"; Gameobject obj = resources.load (filepath) as Gameobject; Textasset Xmlasset = Resources.load (filepath,typeof (Textasset)) as Textasset; XmlDocument xmldoc = new XmlDocument (); Xmldoc.loadxml (Xmlasset.text); Get all child nodes; XmlNodeList NodeList = Xmldoc.selectsinglenode ("character"). ChildNodes; foreach (XmlNode nodeList) {if (child. Name = = "Rolemotions") {m_rolemotions = child as XmlElement; } else if (child. Name = = "Skills") {m_skills = child as XmlElement; }} Debug.Log ("m_rolemotions =" + M_rolemotions.innerxml); Debug.Log ("m_skills =" + m_skills. INNERXML); }}
The contents of the new XML document are as follows:
<character> <roleMotions> <motionInfo> <motion clipname= "Enter_ready" isloop= " False "Moveendtime=" 0 "movestarttime=" 0 "/> </motionInfo> </roleMotions> <skills > <skill name= "General attack" motion= "Rmt_attack1"/> </skills></character>
To read the XML result:
Unity3d new XML Read XML