
我是将xml放在了unity Resources文件夹下,如图
然后在xml写下你要获取指定物体得内容
接下来就是脚本上如何获取了
Dictionary
public int dicindex = 0;
XmlNodeList node;
XmlDocument xmlDoc;
//解析xml
void parseXml()
{
filePath = Application.dataPath + “/Resources/item.xml”;
if (File.Exists(filePath))
{
xmlDoc = new XmlDocument();
xmlDoc.Load(filePath);
node = xmlDoc.SelectSingleNode(“item”).ChildNodes;
for (int i = 0; i < node.Count; i++)
{
dicxml.Add(dicindex++, node[i].Name);
}
}
}
//通过点击UI获取物体名称,这个函数我是挂在了button上面
public void Room_Introduces()
{
var button = UnityEngine.EventSystems.EventSystem.current.currentSelectedGameObject;
string buttonname = button.name;
Debug.Log(buttonname);
#region xml获取物体名称
if (dicxml.ContainsValue(button.name))
{
Debug.Log(buttonname+"_________________");
XmlElement xmlElem = xmlDoc.DocumentElement;//获取根节点
XmlNodeList xnl = xmlElem.GetElementsByTagName(button.name);//取节点名
for (int i = 0; i < xnl.Count; i++)
{
foreach (XmlElement i1 in xnl[i].ChildNodes)
{
bhit = true;
if (i1.Name == “name”)
{
name1 = i1.InnerText;
Debug.Log(name);
}
if (i1.Name == “id”)
{
id1 = i1.InnerText;
}
if (i1.Name == “year”)
{
year = i1.InnerText;
Debug.Log(year);
}
}
}
}
else
{
Debug.Log(“已存在”);
}
#endregion
}
写的不好,请多见谅,有问题留言