Uinty利用XML来本地保存场景中的数据
来源:互联网 发布:淘宝卖假货举报中心 编辑:程序博客网 时间:2024/06/05 15:08
上一篇写了关于创建、读取、修改、添加的操作,现在来一下实战
using UnityEngine;using System.Collections;//不要忘记添加命名空间using System.Xml;using System.IO;public class XMLDemo2 : MonoBehaviour { public GameObject qiu; public GameObject zhengFang; public GameObject yuanzhu; GameObject dd; string path; // Use this for initialization void Start () { dd = GameObject.Find("tt"); path = Application.dataPath + "/yangyong.xml"; } // Update is called once per frame void Update () { if (Input.GetMouseButtonDown(0)) { //保存目标物体到XML文件中 Save(); clearScene(); } if (Input.GetMouseButtonDown(1)) { huifu(); } } void Save(){ XmlDocument xd = new XmlDocument(); XmlNode root = xd.CreateElement("root"); xd.AppendChild(root); foreach (Transform t in dd.transform) { if (t.gameObject.tag == "qiu") { XmlElement yuan01 = xd.CreateElement("qiu"); yuan01.SetAttribute("posX", t.transform.position.x.ToString()); yuan01.SetAttribute("posY", t.transform.position.y.ToString()); yuan01.SetAttribute("posZ", t.transform.position.z.ToString()); yuan01.SetAttribute("rotX", t.transform.localEulerAngles.x.ToString()); yuan01.SetAttribute("rotY", t.transform.localEulerAngles.y.ToString()); yuan01.SetAttribute("rotZ", t.transform.localEulerAngles.z.ToString()); yuan01.SetAttribute("scaX", t.transform.localScale.x.ToString()); yuan01.SetAttribute("scaY", t.transform.localScale.y.ToString()); yuan01.SetAttribute("scaZ", t.transform.localScale.z.ToString()); root.AppendChild(yuan01); } if (t.gameObject.tag == "zhengfang") { XmlElement yuan01 = xd.CreateElement("zhengfang"); yuan01.SetAttribute("posX", t.transform.position.x.ToString()); yuan01.SetAttribute("posY", t.transform.position.y.ToString()); yuan01.SetAttribute("posZ", t.transform.position.z.ToString()); yuan01.SetAttribute("rotX", t.transform.localEulerAngles.x.ToString()); yuan01.SetAttribute("rotY", t.transform.localEulerAngles.y.ToString()); yuan01.SetAttribute("rotZ", t.transform.localEulerAngles.z.ToString()); yuan01.SetAttribute("scaX", t.transform.localScale.x.ToString()); yuan01.SetAttribute("scaY", t.transform.localScale.y.ToString()); yuan01.SetAttribute("scaZ", t.transform.localScale.z.ToString()); root.AppendChild(yuan01); } if (t.gameObject.tag == "yuanzhu") { XmlElement yuan01 = xd.CreateElement("yuanzhu"); yuan01.SetAttribute("posX", t.transform.position.x.ToString()); yuan01.SetAttribute("posY", t.transform.position.y.ToString()); yuan01.SetAttribute("posZ", t.transform.position.z.ToString()); yuan01.SetAttribute("rotX", t.transform.localEulerAngles.x.ToString()); yuan01.SetAttribute("rotY", t.transform.localEulerAngles.y.ToString()); yuan01.SetAttribute("rotZ", t.transform.localEulerAngles.z.ToString()); yuan01.SetAttribute("scaX", t.transform.localScale.x.ToString()); yuan01.SetAttribute("scaY", t.transform.localScale.y.ToString()); yuan01.SetAttribute("scaZ", t.transform.localScale.z.ToString()); root.AppendChild(yuan01); } } if (!File.Exists(path)) { xd.Save(path); } } void clearScene() { dd.SetActive(false); } void huifu() { XmlDocument aa = new XmlDocument(); aa.Load(path); XmlNode root = aa.DocumentElement; XmlNodeList list = root.ChildNodes; foreach (XmlElement node in list) { if (node.Name == "zhengfang") { GameObject object1 = Instantiate(zhengFang) as GameObject; float posX = float.Parse(node.GetAttribute("posX")); float posY = float.Parse(node.GetAttribute("posY")); float posZ = float.Parse(node.GetAttribute("posZ")); float rotX = float.Parse(node.GetAttribute("rotX")); float rotY = float.Parse(node.GetAttribute("rotY")); float rotZ = float.Parse(node.GetAttribute("rotZ")); float scaX = float.Parse(node.GetAttribute("scaX")); float scaY = float.Parse(node.GetAttribute("scaY")); float scaZ = float.Parse(node.GetAttribute("scaZ")); object1.transform.position = new Vector3(posX,posY,posZ); object1.transform.eulerAngles = new Vector3(rotX, rotY, rotZ); object1.transform.localScale = new Vector3(scaX, scaY, scaZ); } if (node.Name == "yuanzhu") { GameObject object1 = Instantiate(zhengFang) as GameObject; float posX = float.Parse(node.GetAttribute("posX")); float posY = float.Parse(node.GetAttribute("posY")); float posZ = float.Parse(node.GetAttribute("posZ")); float rotX = float.Parse(node.GetAttribute("rotX")); float rotY = float.Parse(node.GetAttribute("rotY")); float rotZ = float.Parse(node.GetAttribute("rotZ")); float scaX = float.Parse(node.GetAttribute("scaX")); float scaY = float.Parse(node.GetAttribute("scaY")); float scaZ = float.Parse(node.GetAttribute("scaZ")); object1.transform.position = new Vector3(posX, posY, posZ); object1.transform.eulerAngles = new Vector3(rotX, rotY, rotZ); object1.transform.localScale = new Vector3(scaX, scaY, scaZ); } if (node.Name == "qiu") { GameObject object1 = Instantiate(zhengFang) as GameObject; float posX = float.Parse(node.GetAttribute("posX")); float posY = float.Parse(node.GetAttribute("posY")); float posZ = float.Parse(node.GetAttribute("posZ")); float rotX = float.Parse(node.GetAttribute("rotX")); float rotY = float.Parse(node.GetAttribute("rotY")); float rotZ = float.Parse(node.GetAttribute("rotZ")); float scaX = float.Parse(node.GetAttribute("scaX")); float scaY = float.Parse(node.GetAttribute("scaY")); float scaZ = float.Parse(node.GetAttribute("scaZ")); object1.transform.position = new Vector3(posX, posY, posZ); object1.transform.eulerAngles = new Vector3(rotX, rotY, rotZ); object1.transform.localScale = new Vector3(scaX, scaY, scaZ); } } }}
最后xml文件的数据
QQ群:470841727
0 0
- Uinty利用XML来本地保存场景中的数据
- Unity利用Xml和Json保存场景
- 如何利用asp保存远程网页中的图片到本地来!
- 如何利用asp保存远程网页中的图片到本地来!
- 利用dom4j创建xml文件并保存到本地实例
- 使用文件来保存程序中的数据
- 15、利用JQuery来接收XML数据
- Unity中使用序列化来保存本地游戏数据
- 横竖屏切换 利用onSaveInstanceState来保存数据
- iphone 利用本地数据来代替远程UIWebView请求
- iOS-利用本地数据来代替远程UIWebView请求
- iOS-利用本地数据来代替远程UIWebView请求
- iphone中利用本地数据来代替远程UIWebView请求
- iOS-利用本地数据来代替远程UIWebView请求
- uinty
- Uinty学习之搭建游戏场景
- Android中利用xml文件保存数据信息
- 本地保存数据Preferences
- Android Studio中如何使用Git和Github来管理项目
- ListView滚动方向和滚动位置的探索
- 【Linux入门学习之】vi/vim编辑器必知必会
- ListView解决嵌套冲突3种办法
- ExecutorService——shutdown方法和awaitTermination方法
- Uinty利用XML来本地保存场景中的数据
- 只知编程
- AndroidStudio的一些实用技巧
- 【SHELL】一个非常有趣的问题
- MySql表结构修改详解
- 安卓 支付 问题
- 史上最详细的vsftpd配置文件讲解
- docker学习-进入的container及创建本地镜像
- 使用pods的时候再次遇到了一些困难