举例加载Text
xml文件
<?xmlversion="1.0"encoding="UTF-8"standalone="yes"?> <ROOT> <tabletype="text"txt="helloworld"/> <tabletype="button"callback="OnClicked"/> </ROOT>
usingUnityEngine; usingSystem.Collections; usingSystem.Xml; usingUnityEngine.UI; publicclassXMLLoader:MonoBehavIoUr{ publicGameObjectprefab_text; //Usethisforinitialization voidStart(){ vardoc=newXmlDocument(); doc.Load("../ui.xml"); varroot=doc.DocumentElement; varnodes=root.GetElementsByTagName("table"); foreach(XmlNodenodeinnodes) { switch(((XmlElement)node).GetAttribute("type")) { case"text": { UnityEngine.UI.Texttxt=((GameObject)(Instantiate(prefab_text,transform.position,transform.rotation))).GetComponent<Text>(); txt.text=((XmlElement)node).GetAttribute("txt"); txt.GetComponent<Transform>().parent=gameObject.transform; //=((XmlElement)node).GetAttribute("txt"); } break; } } } //Updateiscalledonceperframe voidUpdate(){ } }