纳金网
标题:
Unity3d中写入XML文件
[打印本页]
作者:
王者再临
时间:
2014-11-30 07:48
标题:
Unity3d中写入XML文件
Unity3d中写入XML文件
using UnityEngine;
using System.Collections;
using System.IO;
using System.Xml;
public class PlayerXMLWriter
{
string _filePath;
XmlDocument _xmlDoc;
XmlElement _elRoot;
public PlayerXMLWriter(string filePath)
{
_filePath=filePath;
_xmlDoc=new XmlDocument();
if(File.Exists(_filePath)){
_xmlDoc.Load(_filePath);
_elRoot = _xmlDoc.DocumentElement;
_elRoot.RemoveAll();
}
else
{
_elRoot = _xmlDoc.CreateElement("xxx");
_xmlDoc.AppendChild(_elRoot);
}
}
public void SaveXMLFile()
{
_xmlDoc.Save(_filePath);
}
// Use this for initialization
public void AddXMLElement(string playerName,string playerScore)
{
XmlElement elPlayer = _xmlDoc.CreateElement("xxx");
_elRoot.AppendChild(elPlayer);
XmlElement elName = _xmlDoc.CreateElement("xxx");
elName.InnerText = playerName;
elPlayer.AppendChild(elName);
XmlElement elScore = _xmlDoc.CreateElement("xxxx");
elScore.InnerText = playerScore;
elScore.SetAttribute("xxx","xxxx");
elPlayer.AppendChild(elScore);
}
}
在unity中使用:
string fileName="playerData.xml";
string folderName="Data";
void Start()
{
string filePath=Application.dataPath+Path.DirectorySeparatorChar+fileName;
PlayerXMLWriter myPlayerXMLWriter=new PlayerXMLWriter(filePath);
myPlayerXMLWriter.AddElement("matt","55");
myPlayerXMLWriter.SaveXMLFile();
}
复制代码
欢迎光临 纳金网 (http://go.narkii.com/club/)
Powered by Discuz! X2.5