• 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);
    			}
    		}
    
    	}
    
    
    }
    
    
  • 相关阅读:
    数据库字段包含反斜杠的查询处理
    sql中的日期时间处理
    查询时间的测试
    group by 和 聚合函数的使用
    比较版本号
    sql IIF函数的应用
    win10系统杀毒功能
    php性能的问题
    linux 下ab压力测试
    datatables的学习总结
  • 原文地址:https://www.cnblogs.com/yufenghou/p/5203716.html
Copyright © 2020-2023  润新知