Unity讀取XML


先發兩個Google到的東西:

http://forum.unity3d.com/threads/25352-Loading-an-XML-file

http://www.paultondeur.com/2010/03/23/tutorial-loading-and-parsing-external-xml-and-json-files-with-unity-part-1-xml/

 

事實上就是可以用.net帶的XML解析庫來解析。但是如果在web上發布就要帶上這個庫,會增加1M的資源,所以官方推薦另一個庫。

http://unity3d.com/support/documentation/Images/manual/Mono.Xml.zip

官方的說法:

http://unity3d.com/support/documentation/Manual/Reducing%20File%20size.html

 

本來用WWW來尋址。。結果發現一導出android的APK包,就找不到相對地址了。。於是用了Resource文件夾來存放資源。

using System.Xml;

public void LoadGoodsFromXML(string fileName)
    {
        //www = new WWW("file://"+Application.dataPath+"/xml/"+fileName);
        
        //while (www.isDone != true);
        
        //Debug.Log(www.url);
        
        string data = Resources.Load(fileName.Split('.')[0]).ToString();
        
        XmlDocument xmlDoc = new XmlDocument();
        
        //while(www.isDone != true);
        
        xmlDoc.LoadXml(data);
        
        XmlNodeList nodeList = xmlDoc.SelectNodes("Fridge/Units/Unit");
        numGoods = nodeList.Count;
        Debug.Log(numGoods);
        
        Goods good;
        int numCount;
        foreach (XmlNode node in nodeList)
        {
            good  = new Goods();
            good.name = node.SelectSingleNode("Name").InnerText;
            Debug.Log(good.name);
            //www = new WWW("file://"+Application.dataPath+"/Images/VegetableShop/"+node.SelectSingleNode("IconID").InnerText);
            good.texture = (Texture2D)Resources.Load(node.SelectSingleNode("IconID").InnerText.Split('.')[0]);
            numCount = int.Parse(node.SelectSingleNode("Num").InnerText);
            goodsList.Add(good);
            goodsNumMap.Add(good.name, numCount);
        }
            
    }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM