基于Unity3D的Xml文件的存储的实现

来源:互联网 发布:程序员应该去的网站 编辑:程序博客网 时间:2024/06/08 08:30

在unity的开发中,经常会用到Xml来存储数据文件。如下我将带领大家来实现一套Xml文件的存储系统。

代码如下:

using UnityEngine;using System.Collections;using System.Collections.Generic;using System.Xml;using System.IO;using System.Text;public class XmlMgr : MonoBehaviour {void OnGUI(){if(GUI.Button(new Rect(10,10,200,30),"Create XML")){CreateXml();}if(GUI.Button(new Rect(10,50,200,30),"Update XML")){UpdateXml();}if(GUI.Button(new Rect(10,90,200,30),"Add XML")){AddXml();}if(GUI.Button(new Rect(10,130,200,30),"Delete XML")){DeleteXml();}if(GUI.Button(new Rect(10,170,200,30),"Show XML")){ShowXml();}}//创建XML文件public void CreateXml(){string filepath = Application.dataPath + @"/XmlData.xml";if(!File.Exists (filepath)){ XmlDocument xmlDoc = new XmlDocument(); XmlElement root = xmlDoc.CreateElement("transforms");  XmlElement elmNew = xmlDoc.CreateElement("rotation");  elmNew.SetAttribute("id","0");      elmNew.SetAttribute("name","backkom");         XmlElement rotation_X = xmlDoc.CreateElement("x");              rotation_X.InnerText = "0";              XmlElement rotation_Y = xmlDoc.CreateElement("y");              rotation_Y.InnerText = "1";              XmlElement rotation_Z = xmlDoc.CreateElement("z");              rotation_Z.InnerText = "2";     rotation_Z.SetAttribute("id","1");             elmNew.AppendChild(rotation_X);             elmNew.AppendChild(rotation_Y);             elmNew.AppendChild(rotation_Z); root.AppendChild(elmNew);             xmlDoc.AppendChild(root);             xmlDoc.Save(filepath);  Debug.Log("createXml OK!");}}//更新XML文件public void UpdateXml(){string filepath = Application.dataPath + @"/XmlData.xml";if(File.Exists (filepath)){ XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(filepath); XmlNodeList nodeList=xmlDoc.SelectSingleNode("transforms").ChildNodes; foreach(XmlElement xe in nodeList) {if(xe.GetAttribute("id")=="0"){xe.SetAttribute("id","2");foreach(XmlElement x1 in xe.ChildNodes){if(x1.Name=="z"){ x1.InnerText="update";}}break;} } xmlDoc.Save(filepath); Debug.Log("UpdateXml OK!");}}//添加XML文件节点public void AddXml(){string filepath = Application.dataPath + @"/XmlData.xml";if(File.Exists (filepath)){XmlDocument xmlDoc = new XmlDocument();xmlDoc.Load(filepath);XmlNode root = xmlDoc.SelectSingleNode("transforms");XmlElement elmNew = xmlDoc.CreateElement("rotation"); elmNew.SetAttribute("id","1");     elmNew.SetAttribute("name","backkom");         XmlElement rotation_X = xmlDoc.CreateElement("x");              rotation_X.InnerText = "0";  rotation_X.SetAttribute("id","1");             XmlElement rotation_Y = xmlDoc.CreateElement("y");              rotation_Y.InnerText = "1";              XmlElement rotation_Z = xmlDoc.CreateElement("z");              rotation_Z.InnerText = "2";                 elmNew.AppendChild(rotation_X);             elmNew.AppendChild(rotation_Y);             elmNew.AppendChild(rotation_Z); root.AppendChild(elmNew);             xmlDoc.AppendChild(root);             xmlDoc.Save(filepath);  Debug.Log("AddXml OK!");}}//删除XML文件节点,属性public void DeleteXml(){string filepath = Application.dataPath + @"/XmlData.xml";if(File.Exists (filepath)){ XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(filepath); XmlNodeList nodeList=xmlDoc.SelectSingleNode("transforms").ChildNodes; foreach(XmlElement xe in nodeList) {if(xe.GetAttribute("id")=="1"){xe.RemoveAttribute("id");}foreach(XmlElement x1 in xe.ChildNodes){if(x1.Name == "z"){x1.RemoveAll();}} } xmlDoc.Save(filepath); Debug.Log("deleteXml OK!");}}//展示XML文件public void ShowXml(){string filepath = Application.dataPath + @"/XmlData.xml";if(File.Exists (filepath)){ XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(filepath); XmlNodeList nodeList=xmlDoc.SelectSingleNode("transforms").ChildNodes; foreach(XmlElement xe in nodeList) {Debug.Log("Attribute :" + xe.GetAttribute("name"));Debug.Log("NAME :" + xe.Name);foreach(XmlElement x1 in xe.ChildNodes){if(x1.Name == "y"){Debug.Log("VALUE :" + x1.InnerText);}} } Debug.Log(xmlDoc.OuterXml);}}}