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

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

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
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yy763496668

您的鼓励是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值