zoukankan      html  css  js  c++  java
  • 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);
    			}
    		}
    
    	}
    
    
    }
    
    
  • 相关阅读:
    4.单例模式
    3.适配器模式
    2.策略模式
    1.工厂模式
    机器学习
    何为技术领导力
    图像像素的算术操作
    图像对象创建和赋值的区别
    图像色彩空间转换
    notepad更改文档编码格式
  • 原文地址:https://www.cnblogs.com/yufenghou/p/5203716.html
Copyright © 2011-2022 走看看