基于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);}}}
阅读全文
0 0
- 基于Unity3D的Xml文件的存储的实现
- 基于Unity3D的JSON文件的存储的实现
- 实现基于文件存储的Session类
- 基于Unity3D的对象池的实现
- 基于Unity3D的虚拟摇杆的实现
- 基于Unity3D的相册系统的实现
- 基于Unity3D的循环路径的实现
- 基于Unity3D的滑动面板的实现
- unity3d 简单的xml文件读写操作
- Unity3D 读取XML文件里面的属性
- Unity3d 基于NGUI的虚拟摇杆实现
- Unity3D基于UGUI的虚拟摇杆实现
- [原创]实现基于文件存储的Session类
- [原创]实现基于文件存储的Session类
- [原创]实现基于文件存储的Session类
- MongoDB基于GridFS实现文件的分布式存储
- MongoDB基于GridFS实现文件的分布式存储
- 以XML文件存储配置数据的技术实现,c++
- windows下安装Python虚拟环境virtualenvwrapper-win
- JavaScript forEach()遍历函数使用及介绍
- OPNET中sink module
- 文件的存取
- MySQL插入或者读取中文数据的时候出现乱码总结
- 基于Unity3D的Xml文件的存储的实现
- 支持向量机的R实现
- Android shape的应用
- 使用调试生成配置
- http://www.mamicode.com/info-detail-1208112.html
- Image Printing Program Based on Halftoning (半色调打印技术)
- Python爬虫之二:自制简易词典
- Windows下深度学习平台Keras环境搭建【CPU版】
- bootsect启动代码分析