unity 解析tmx 2

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


public class xml : MonoBehaviour {
    public Transform steel;

    void Start ()
    {
        List<Vector3> list=translateTileMapToList ();

        addListToWin (list);
    }

    List<Vector3> translateTileMapToList()
    {
        List<Vector3> list = new List<Vector3> ();


        XmlDocument xmlDoc = new XmlDocument ();
        xmlDoc.Load ("Assets/2.xml");
        XmlNode mapNode = xmlDoc.SelectSingleNode ("map");

        // map 
        foreach (XmlNode layerNode in mapNode.ChildNodes) 
        {


            //tileset layer

            if (layerNode.Name == "layer") 
            {
                XmlElement layerElement = (XmlElement)layerNode;
                string widthString = layerElement.GetAttribute ("width");
                int width = System.Int32.Parse (widthString);
                string heightString = layerElement.GetAttribute ("height");
                int height = System.Int32.Parse (heightString);
                //string height =(int) layerElement.GetAttribute ("height").ToString();

                //data
                foreach (XmlNode dataNode in ((XmlElement)layerNode).ChildNodes) 
                {
                    //tile
                    int number=0;
                    foreach (XmlNode tileNode in ((XmlElement)dataNode).ChildNodes)
                    {
                        XmlElement tileElement = (XmlElement)tileNode;
                        int x = number % width;
                        int y = height - number / height;

                        string zString = tileElement.GetAttribute ("gid");
                        int z = System.Int32.Parse (zString);

                        list.Add (new Vector3 (x, y, z));


                        number++;

                    }
                }
            }
        }
        return list;
    }


    void addListToWin (List<Vector3> list)
    {

        for (int i = 0; i < list.Count; i++)
        {
            if (list [i].z == 11) {
                Instantiate (steel, new Vector3 (list[i].x * 32f / 100f, list[i].y * 32f / 100f, 0), Quaternion.identity);
            }
        }

    }


}

转载于:https://www.cnblogs.com/yufenghou/p/5203716.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值