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

在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);
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值