从xml加载ui

梦想游戏人
目录:
Unity

举例加载Text

xml文件

  • <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  • <ROOT>
  • <table type="text" txt="hello world" />
  • <table type="button" callback="OnClicked" />
  • </ROOT>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ROOT>
 <table type="text" txt="hello world"   />
 <table type="button" callback="OnClicked"   />
</ROOT>
  • using UnityEngine;
  • using System.Collections;
  • using System.Xml;
  • using UnityEngine.UI;
  • public class XMLLoader : MonoBehaviour {
  • public GameObject prefab_text;
  • // Use this for initialization
  • void Start () {
  • var doc = new XmlDocument();
  • doc.Load("../ui.xml");
  • var root = doc.DocumentElement;
  • var nodes = root.GetElementsByTagName("table");
  • foreach (XmlNode node in nodes)
  • {
  • switch (((XmlElement)node).GetAttribute("type"))
  • {
  • case "text":
  • {
  • UnityEngine.UI.Text txt = ((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;
  • }
  • }
  • }
  • // Update is called once per frame
  • void Update () {
  • }
  • }
using UnityEngine;
using System.Collections;
using System.Xml;
using UnityEngine.UI;
public class XMLLoader : MonoBehaviour {
    public GameObject prefab_text;

 // Use this for initialization
 void Start () {
        var doc = new XmlDocument();
        doc.Load("../ui.xml");
        var root = doc.DocumentElement;
        var nodes = root.GetElementsByTagName("table");
        foreach (XmlNode node in nodes)
        {
            switch (((XmlElement)node).GetAttribute("type"))
            {
                case "text":
                    {
                        UnityEngine.UI.Text txt = ((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;
            }       
        }
 }
 
 // Update is called once per frame
 void Update () {
 
 }
}
Scroll Up