- 最后登录
- 2016-8-29
- 注册时间
- 2012-8-25
- 阅读权限
- 90
- 积分
- 23585
- 纳金币
- 20645
- 精华
- 62
|
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();
- }
复制代码 |
|