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
原创粉丝点击