Unity3d New xml read xml, unity3dxml
In game development, Xml is often used as a configuration file for skill configuration, map configuration, and character action configuration. The built-in Xml library of Unity3d allows us to easily create Xml and read Xml.
The following is an example of creating an Xml document and reading 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 this for initializationvoid Start () {// CreateXml (); // ReadXml (); ReadFileToXml ();} // Update is called once per framevoid Update () {} void CreateXml () {string filepath = Application. da TaPath + "/Resources/1013000.xml"; if (! File. exists (filepath) {// create an xml instance; XmlDocument xmlDoc = new XmlDocument (); // create a 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); // obtain all subnodes; 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); // obtain all subnodes; 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 content of the newly created Xml document is 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>
Read Xml results:
How to read xml files after unity3d is released to Android?
We recommend that you use this address as the XML address on your mobile phone Application. persistentDataPath to view the original post>
Adoption
How does unity3d momomoxml write data to an xml file?
What does Momo. xml mean? If you only want to read and write xml, read my post ~~
Tieba.baidu.com/p/2825006787