1存储在哪个文件下
在unity中一定是使用各平台都可读可写的路径
1 Resources可读 不可写 打包后找不到 x
2 application.streamingAssetsPath可读pc端可写 x
3application.dataPath 打包后找不到 x
4 Application.persistentDataPath 可读可写找得到 √
cs
string path = Application.persistentDataPath + "/PlayerInfo2.xml";
print(Application.persistentDataPath);
2存储xml文件
关键类 XmlDocument 用于创建节点 存储文件
关键类 XmlDeclaration 用于添加版本信息
关键类 XmlElement 节点类
3 存储有5步
1创建文本对象
cs
XmlDocument xml =new XmlDocument();
2 添加固定版本信息
cs
XmlDeclaration xmlDec = xml.CreateXmlDeclaration("1.0", "UTF-8", "");
3添加根节点
cs
XmlElement root = xml.CreateElement("root");
xml.AppendChild(root);
4为根节点添加子节点
cs
XmlElement name =xml.CreateElement("name");
name.InnerText = "shan";
root.AppendChild(name);
XmlElement atk = xml.CreateElement("atk");
atk.InnerText = "10";
root.AppendChild(atk);
4 添加列表
cs
XmlElement listInt =xml.CreateElement("listInt");
for(int i = 1; i <= 3; i++)
{
XmlElement childNode = xml.CreateElement("int");
childNode.InnerText = i.ToString();
listInt.AppendChild(childNode);
}
root.AppendChild(listInt);
4 添加有属性的子节点
cs
XmlElement itemList = xml.CreateElement("itemList");
for(int i = 1; i <=2; i++)
{
XmlElement childNode =xml.CreateElement("Item");
//加属性
childNode.SetAttribute("id",i.ToString());
childNode.SetAttribute("num",(i*10).ToString());
itemList.AppendChild(childNode);
}
root.AppendChild(itemList);
5保存
cs
xml.Save(path);
4 修改xml文件
cs
if(File.Exists(path))
{
XmlDocument newXml=new XmlDocument();
newXml.Load(path);
//一种得法
//newXml.SelectSingleNode("Root").SelectSingleNode("atk");
//第二种得法
XmlNode node= newXml.SelectSingleNode("root/atk");
print(node.InnerText);
//得到自己的父节点
XmlNode root2=newXml.SelectSingleNode("root");
//移除子节点方法
root2.RemoveChild(node);
//添加节点
XmlElement speed = newXml.CreateElement("moveSpeed");
speed.InnerText = "20";
root2.AppendChild(speed);
//改了要保存
newXml.Save(path);
}
一种得法
cs
newXml.SelectSingleNode("Root").SelectSingleNode("atk");
第二种得法
cs
XmlNode node= newXml.SelectSingleNode("root/atk");
print(node.InnerText);
得到自己的父节点
cs
XmlNode root2=newXml.SelectSingleNode("root");
移除子节点方法
cs
root2.RemoveChild(node);
添加节点
cs
XmlElement speed = newXml.CreateElement("moveSpeed");
speed.InnerText = "20";
root2.AppendChild(speed);
改了要保存
cs
newXml.Save(path);