本案例使用unity版本:unity5.6.1f1
编程工具:VS2015
项目开发中,会使用xml作为软件的配置文件,在这里记录一下关于XML的基本操作。
本案例将XML文件创建在StreamingAssets文件夹中,若工程中不存在该文件夹请自己创建。
(保存到该文件夹,便于在项目发布时,能够将配置文件一并打包到程序中,并且不被压缩便于日后修改)
引用命名空间:
using System.IO;
using System.Xml;
XML的创建
void CreateXML() {
localPath = UnityEngine.Application.streamingAssetsPath + "/"+"MyXML.xml";
if (!File.Exists(localPath) ) {
XmlDocument xml = new XmlDocument();
XmlDeclaration xmldecl = xml.CreateXmlDeclaration("1.0", "UTF-8", "");//设置xml文件编码格式为UTF-8
XmlElement root = xml.CreateElement("Data");//创建根节点
XmlElement info = xml.CreateElement("Info");//创建子节点
info.SetAttribute("Name","小王");//创建子节点属性名和属性值
info.SetAttribute("Age","28");
info.SetAttribute("Phone","12345678");
root.AppendChild(info);//将子节点按照创建顺序,添加到xml
xml.AppendChild(root);
xml.Save(localPath);//保存xml到路径位置
Debug.Log("创建XML成功!");
}
}
调用该方法,运行结果如下图:

创建的XML文件
<Data>
<Info Name="小王" Age="28" Phone="12345678" />
</Data>
XML的写入
void AddXML() {
localPath = UnityEngine.Application.streamingAssetsPath + "/" + "MyXML.xml";
if ( File.Exists(localPath) )
{
XmlDocument xml = new XmlDocument();
xml.Load(localPath);//加载xml文件
XmlNode root = xml.SelectSingleNode("Data");//获取根节点
XmlElement info = xml.CreateElement("Info_2");//创建新的子节点
info.SetAttribute("Name", "小李");//创建新子节点属性名和属性值
info.SetAttribute("Age", "25");
info.SetAttribute("Phone", "87654321");
root.AppendChild(info);//将子节点按照创建顺序,添加到xml
xml.AppendChild(root);
xml.Save(localPath);//保存xml到路径位置
Debug.Log("添加XML成功!");
}
}
在创建MyXML文件后,调用 AddXML() 方法。运行结果如图下图:

写入后的XML文件
<Data>
<Info Name="小王" Age="28" Phone="12345678" />
<Info_2 Name="小李" Age="25" Phone="87654321" />
</Data>
XML的读取
void ReadXML() {
localPath = UnityEngine.Application.streamingAssetsPath + "/" + "MyXML.xml";
if ( File.Exists(localPath) )
{
XmlDocument xml = new XmlDocument();
xml.Load(localPath);//加载xml文件
XmlNodeList nodeList = xml.SelectSingleNode("Data").ChildNodes;
foreach (XmlElement xe in nodeList) {//遍历所以子节点
if (xe.Name== "Info_2" ) {
Debug.Log(xe.GetAttribute("Name"));//获取Name属性值
Debug.Log(xe.GetAttribute("Age"));
Debug.Log(xe.GetAttribute("Phone"));
}
}
Debug.Log("读取XML成功!"+xml.OuterXml);
}
}
在写入XML信息后,在Start()中调用ReadXML()方法。运行结果下图:

XML的更新
public void UpdateXml()
{
localPath = UnityEngine.Application.streamingAssetsPath + "/" + "MyXML.xml";
if ( File.Exists(localPath) )
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(localPath);
XmlNodeList nodeList = xmlDoc.SelectSingleNode("Data").ChildNodes;
foreach ( XmlElement xe in nodeList )
{
//拿到节点中属性Name =小王的节点
if ( xe.GetAttribute("Name") == "小王" )
{
//更新节点属性
xe.SetAttribute("Age", "21");
break;
}
}
xmlDoc.Save(localPath);
Debug.Log("更新XML成功!");
}
}
在Start()中调用UpdateXml()方法。运行结果下图:

更新后的XML信息
<Data>
<Info Name="小王" Age="21" Phone="12345678" />
<Info_2 Name="小李" Age="25" Phone="87654321" />
</Data>
XML的删除
public void DeleteXml()
{
localPath = UnityEngine.Application.streamingAssetsPath + "/" + "MyXML.xml";
if ( File.Exists(localPath) )
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(localPath);
XmlNodeList nodeList = xmlDoc.SelectSingleNode("Data").ChildNodes;
foreach ( XmlElement xe in nodeList )
{
if ( xe.GetAttribute("Name") == "小李" )
{
xe.RemoveAttribute("Phone");
}
if ( xe.GetAttribute("Name") == "小王" )
{
xe.RemoveAll();
}
}
xmlDoc.Save(localPath);
Debug.Log("删除XML成功!");
}
}
在Start()中调用DeleteXml()方法。运行结果下图:

删除后XML信息
<Data>
<Info />
<Info_2 Name="小李" Age="25" />
</Data>
以上对XML的操作基本满足项目开发的需求,本案例记录XML的基本用法和简单实例,可以根据项目需求进行灵活运用。
参考学习: 雨松MOMO 雨松MOMO程序研究院
本文详细介绍如何在Unity中使用XML文件进行配置,包括创建、读取、更新和删除等基本操作,适用于游戏开发和软件配置场景。

892

被折叠的 条评论
为什么被折叠?



