其他分享
首页 > 其他分享> > XML设置写入和读取地图

XML设置写入和读取地图

作者:互联网

using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;

public class Menu : MonoBehaviour
{
    private static int j = 1;
    private static int k = 1;

    /// <summary> 添加菜单栏(写入、保存) </summary>
    [MenuItem("GetTransform/获得所有角色的位置设置XML &8")]
    public static void SetSelectedObjPos()
    {
        //获得选中的Transfrom组件
        Transform[] objs = Selection.transforms;
        SaveName save = new SaveName();
        for (int i = 0; i < objs.Length; i++)
        {
            SaveByXml(objs[i]);
            //RecursiveSave(objs[i], save);
            Debug.Log("保存完成");
        }
    }

    /// <summary> 添加菜单栏(读取) </summary>
    [MenuItem("GetTransform/从XML取得角色位置 &9")]
    public static void GetSelectedObjPos()
    {
        //获得选中的Transfrom组件
        Transform[] objs = Selection.transforms;
        //地址
        string filePath = Application.dataPath + "/byXML.txt";
        //查看是否有文件
        //TODO:还需检查是否名字和层级相同
        //TODO:还需要检查视图是否一样
        if (File.Exists(filePath))
        {
            //加载 XML 文档
            XmlDocument xmlDoc = new XmlDocument();
            //指定URL加载文档
            xmlDoc.Load(filePath);
            //选择XmlNode匹配(获得第一个节点)
            XmlNode root = xmlDoc.SelectSingleNode("saveTransform ");
            //获得root节点的所有子节点(所有第一层GameObject)
            XmlNodeList nodeList = root.ChildNodes;
            for (int i = 0; i < objs.Length; i++)
            {
                for (int j = 0; j < nodeList.Count; j++)
                {
                    //将第一层的数据带入
                    //LoadByXml(nodeList[j], objs[i].GetChild(j));
                    LoadByXml(nodeList[j], objs[i]);
                }
                Debug.Log("加载游戏");
            }
        }
    }

    /// <summary> 创建Xml </summary>
    public static void SaveByXml(Transform parent)
    {
        //创建 XML 文件的储存路径
        string filePath = Application.dataPath + "/byXML.txt";
        //如果XML文件已经存在,就删除
        if (File.Exists(filePath))
        {
            File.Delete(filePath);
        }
        //创建 XML 文档
        XmlDocument xmlDoc = new XmlDocument();
        //创建跟节点,即最上层的节点
        XmlElement root = xmlDoc.CreateElement("saveTransform");
        //设置该节点的一个属性
        root.SetAttribute("场景名称", EditorSceneManager.GetActiveScene().name);
        XmlElement parentObject = xmlDoc.CreateElement("GameObject层级0");
        XmlElement transformXml = xmlDoc.CreateElement("Transform");
        //将Xml加入
        TransformXml(parent, transformXml, xmlDoc);
        parentObject.AppendChild(transformXml);
        RecursiveAllObjs(parent, parentObject, xmlDoc);
        root.AppendChild(parentObject);
        xmlDoc.AppendChild(root);
        xmlDoc.Save(filePath);
    }

    /// <summary> 获得节点 </summary>
    private static void RecursiveAllObjs(Transform parent, XmlElement parentObject, XmlDocument xmlDoc)
    {
        for (int i = 0; i < parent.childCount; i++) //childCount的数量包括不显示的物体
        {
            //获得子物体的 Transform 位置
            var temp = parent.GetChild(i);
            //创建节点游戏物体
            XmlElement gameObjectXml = xmlDoc.CreateElement("GameObject" + "层级" + j);
            gameObjectXml.SetAttribute("名称", temp.name);
            XmlElement transformXml = xmlDoc.CreateElement("Transform");
            TransformXml(temp, transformXml, xmlDoc);
            gameObjectXml.AppendChild(transformXml);
            //将transformXml设置父节点为 root
            parentObject.AppendChild(gameObjectXml);
            //再次查询子类中的Transform。
            if (temp.childCount > 0)
            {
                j++;
                //迭代传入参数
                RecursiveAllObjs(temp, gameObjectXml, xmlDoc);
                j = 1;
            }
        }
    }

    /// <summary> 将Transform组件内容加入子类XML </summary> 
    private static void TransformXml(Transform parent, XmlElement transformXml, XmlDocument xmlDoc)
    {
        //创建节点position
        XmlElement localPosition = xmlDoc.CreateElement("LocalPosition");
        XmlElement localPositionX = xmlDoc.CreateElement("LocalPositionX");
        XmlElement localPositionY = xmlDoc.CreateElement("LocalPositionY");
        XmlElement localPositionZ = xmlDoc.CreateElement("LocalPositionZ");
        localPositionX.InnerText = parent.localPosition.x.ToString();
        localPositionY.InnerText = parent.localPosition.y.ToString();
        localPositionZ.InnerText = parent.localPosition.z.ToString();
        localPosition.AppendChild(localPositionX);
        localPosition.AppendChild(localPositionY);
        localPosition.AppendChild(localPositionZ);

        //创建节点rotation
        XmlElement localEulerAngles = xmlDoc.CreateElement("LocalEulerAngles");
        XmlElement localEulerAnglesX = xmlDoc.CreateElement("LocalEulerAnglesX");
        XmlElement localEulerAnglesY = xmlDoc.CreateElement("LocalEulerAnglesY");
        XmlElement localEulerAnglesZ = xmlDoc.CreateElement("LocalEulerAnglesZ");
        localEulerAnglesX.InnerText = parent.localEulerAngles.x.ToString();
        localEulerAnglesY.InnerText = parent.localEulerAngles.y.ToString();
        localEulerAnglesZ.InnerText = parent.localEulerAngles.z.ToString();
        localEulerAngles.AppendChild(localEulerAnglesX);
        localEulerAngles.AppendChild(localEulerAnglesY);
        localEulerAngles.AppendChild(localEulerAnglesZ);

        //创建节点scale
        XmlElement localScale = xmlDoc.CreateElement("LocalScale");
        XmlElement localScaleX = xmlDoc.CreateElement("LocalScale");
        XmlElement localScaleY = xmlDoc.CreateElement("LocalScale");
        XmlElement localScaleZ = xmlDoc.CreateElement("LocalScale");
        localScaleX.InnerText = parent.localScale.x.ToString();
        localScaleY.InnerText = parent.localScale.y.ToString();
        localScaleZ.InnerText = parent.localScale.z.ToString();
        localScale.AppendChild(localScaleX);
        localScale.AppendChild(localScaleY);
        localScale.AppendChild(localScaleZ);

        //设置父节点transformXml
        transformXml.AppendChild(localPosition);
        transformXml.AppendChild(localEulerAngles);
        transformXml.AppendChild(localScale);
    }

    /// <summary> 加载节点 </summary>
    public static void LoadByXml(XmlNode noteData, Transform parent)
    {
        int j = 1;
        XmlNodeList notes = noteData.ChildNodes;
        //获得子物体的零号位都是Transform 位置
        XmlNodeList transformData = notes[0].ChildNodes;
        #region 初始化物体的Transfrom
        //获得Transform的零号位都是Position
        XmlNodeList LocalPositionData = transformData[0].ChildNodes;
        parent.localPosition = new Vector3(float.Parse(LocalPositionData[0].InnerText), float.Parse(LocalPositionData[1].InnerText), float.Parse(LocalPositionData[2].InnerText));
        //获得Transform的一号位都是Rotation
        XmlNodeList LocalEulerAnglesData = transformData[1].ChildNodes;
        parent.localEulerAngles = new Vector3(float.Parse(LocalEulerAnglesData[0].InnerText), float.Parse(LocalEulerAnglesData[1].InnerText), float.Parse(LocalEulerAnglesData[2].InnerText));
        //获得Transform的二号位都是Rotation
        XmlNodeList LocalScaleData = transformData[2].ChildNodes;
        parent.localScale = new Vector3(float.Parse(LocalScaleData[0].InnerText), float.Parse(LocalScaleData[1].InnerText), float.Parse(LocalScaleData[2].InnerText));
        #endregion
        //判断物体是否还有子物体
        if (notes.Count > 1)
        {
            //排除第一个Transform节点后开始循环每一个
            for (int i = 1; i < notes.Count; i++) 
            {
                j++;
                var temp = parent.GetChild(i-1);
                //获得的是GameObject
                LoadByXml(notes[i], temp);
            }
        }
    }
}

标签:XML,AppendChild,读取,parent,写入,XmlElement,InnerText,CreateElement,xmlDoc
来源: https://www.cnblogs.com/zheyw/p/15214143.html