Unity C#生成写入xml文件

之前整理了几个xml读取方法。现在整理下xml的写入方法。

1

文件格式如下

<Map Name="测试地图1" Id="60056" Version="1.0.0" BackImg="1" PathImg="1">
  <PathLayer>
    <Path V3="-434|249|0" Id="0" />
    <Path V3="-421|-63|0" Id="1" />
    <Path V3="-148|-38|0" Id="2" />
    <Path V3="-141|59|0" Id="3" />
    <Path V3="137|53|0" Id="4" />
    <Path V3="158|-49|0" Id="5" />
    <Path V3="428|-66|0" Id="6" />
    <Path V3="414|253|0" Id="7" />
  </PathLayer>
  <ObstacleLayer>
    <Obstacles Id="1" GetId="5" Probability="5" />
    <Obstacles Id="2" GetId="65" Probability="6" />
    <Obstacles Id="3" GetId="7" Probability="7" />
  </ObstacleLayer>
  <MonsterLayer>
    <Monster MonsterId="4|5|8" Nums="345" Speed="600" Time="5" />
    <Monster MonsterId="4|5|8" Nums="33" Speed="555" Time="4" />
    <Monster MonsterId="4|5|8" Nums="434" Speed="555" Time="567" />
    <Monster MonsterId="4|5|8" Nums="3453" Speed="322" Time="444" />
    <Monster MonsterId="23232" Nums="111" Speed="222" Time="444" />
  </MonsterLayer>
</Map>

写入方法代码:

这里包含的写节点。写每条数据里的参数等。很简单的。

    /// <summary>
    /// 保存地图
    /// </summary>
    public void SevaMap(string mapId, string name)
    {
        GameObject mapobj = GameObject.Find("Map");
        Map map = mapobj.transform.parent.gameObject.GetComponent<Map>();
        FileInfo fi = new FileInfo(Application.dataPath + "OutputXml/" + mapId + ".xml");
        if (fi.Exists)
        {
            fi.Delete();
        }
        //ReadXml.MapInfo mapInfo = ReadXml.readXml.MapInfoDic[MapId];
        XmlDocument doc = new XmlDocument();
        XmlElement root = doc.CreateElement("Map");
        root.SetAttribute("Name", name);
        root.SetAttribute("Id", mapId.ToString());
        root.SetAttribute("Version", "1.0.0");
        root.SetAttribute("BackImg", map.mBackTexture.mainTexture.name);
        root.SetAttribute("PathImg", map.mPathTexture.mainTexture.name);
        doc.AppendChild(root);

        List<GameObject> pathList = map.GetPathList();
        XmlElement Path = doc.CreateElement("PathLayer");
        root.AppendChild(Path);
        for (int i = 0; i < pathList.Count; i++)
        {
            XmlElement Path_ = doc.CreateElement("Path");
            Path_.SetAttribute("V3", string.Concat(pathList[i].transform.localPosition.x.ToString(), "|", pathList[i].transform.localPosition.y.ToString(), "|", 0));
            Path_.SetAttribute("Id", i.ToString());
            Path.AppendChild(Path_);
        }

        XmlElement Obstacle = doc.CreateElement("ObstacleLayer");
        root.AppendChild(Obstacle);
        List<GameObject> obstaclesList = map.GetObstaclesList();
        for (int i = 0; i < obstaclesList.Count; i++)
        {
            XmlElement Obstacle_ = doc.CreateElement("Obstacles");
            string[] strArr = obstaclesList[i].name.Split('_');
            Debug.LogError(strArr.Length + "--" + obstaclesList[i].name);
            Obstacle_.SetAttribute("Id", strArr[1]);
            Obstacle_.SetAttribute("GetId", strArr[2]);
            Obstacle_.SetAttribute("Probability", strArr[3]);
            Obstacle.AppendChild(Obstacle_);
        }

        XmlElement Weapons = doc.CreateElement("WeaponsLayer");
        root.AppendChild(Obstacle);
        List<GameObject> weaponsList = map.GetWeaponList();
        for (int i = 0; i < weaponsList.Count; i++)
        {
            XmlElement Weapons_ = doc.CreateElement("Weapons");
            string[] strArr = weaponsList[i].name.Split('_');
            Weapons_.SetAttribute("Id", strArr[1]);
            Weapons_.SetAttribute("Level", strArr[2]);
            Weapons.AppendChild(Weapons_);
        }
        XmlElement Monster = doc.CreateElement("MonsterLayer");
        root.AppendChild(Monster);
        for (int i = 0; i < wave.Count; i++)
        {
            XmlElement Monster_ = doc.CreateElement("Monster");
            Monster_.SetAttribute("MonsterId", wave[i].monster);
            Monster_.SetAttribute("Nums", wave[i].nums);
            Monster_.SetAttribute("Speed", wave[i].speed);
            Monster_.SetAttribute("Time", wave[i].time);
            Monster.AppendChild(Monster_);
        }

        doc.Save(Application.dataPath + "/OutputXml/" + mapId + ".xml");
        TipsMgr.GetInstance().AddTipe("保存地图成功!");
    }

这些文件在之前发的那个地图编辑器中用到。有兴趣可以去看一下。

传送门:地图编辑器

xml读取 传送:xml读取

本文链接:

https://bobsong.cn/500.html
1 + 5 =
快来做第一个评论的人吧~