In 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 easy to create new XML and read XML.
Here is an example of creating a new XML document and reading it.
[CSharp]View Plaincopy
- Using Unityengine;
- Using System.Collections;
- Using System.IO;
- Using System.Xml;
- Using System.Text;
- Public class Xmltest:monobehaviour {
- XmlElement m_rolemotions = null; character movements;
- XmlElement m_skills = null; Character skills;
- // Use this for initialization
- void Start () {
- //createxml ();
- //readxml ();
- Readfiletoxml ();
- }
- //Update is called once per frame
- void Update () {
- }
- void Createxml ()
- {
- string filepath = Application.datapath + "/resources/1013000.xml";
- if (! File.exists (filepath))
- {
- //Create an XML instance;
- XmlDocument xmldoc = new XmlDocument ();
- //create character;
- XmlElement root = xmldoc.createelement ("character");
- /*** creating 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);
- /*** creating Rolemotions end***/
- /*** Creating 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);
- /*** Creating 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 child in 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 child in 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 new XML document reads as follows:
[HTML]View Plaincopy
- <character>
- <rolemotions>
- <motioninfo>
- <motion clipname="Enter_ready" isloop="false" moveendtime="0" movestarttime ="0" />
- </motioninfo>
- </rolemotions>
- <skills>
- <skill Name= "Generalattack" motion="Rmt_attack1" />
- </Skills>
- </character>
To read the XML result:
Unity3d new XML Read XML