先發兩個Google到的東西:
http://forum.unity3d.com/threads/25352-Loading-an-XML-file
事實上就是可以用.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); } }