栏目分类:
子分类:
返回
终身学习网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
终身学习网 > IT > 软件开发 > 游戏开发 > Unity3D

unity获取xml文件内容

Unity3D 更新时间:发布时间: 百科书网 趣学号

我是将xml放在了unity Resources文件夹下,如图

然后在xml写下你要获取指定物体得内容

接下来就是脚本上如何获取了
Dictionary dicxml;
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
}

写的不好,请多见谅,有问题留言

转载请注明:文章转载自 www.051e.com
本文地址:http://www.051e.com/it/1069225.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 ©2023-2025 051e.com

ICP备案号:京ICP备12030808号