Unity读取xml(android)

转载地址:http://www.cnblogs.com/gameprogram/archive/2012/05/04/2482432.html

先发两个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);
        }
            
    }

你可能感兴趣的:(xml,unity)