关闭

Uinty利用XML来本地保存场景中的数据

标签: xml数据
388人阅读 评论(0) 收藏 举报
分类:

上一篇写了关于创建、读取、修改、添加的操作,现在来一下实战

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

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:81573次
    • 积分:2495
    • 等级:
    • 排名:第14899名
    • 原创:173篇
    • 转载:11篇
    • 译文:0篇
    • 评论:16条
    博客专栏
    文章分类