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);
    			}
    		}
    
    	}
    
    
    }
    
    
  • 相关阅读:
    AttributeError: module ‘select’ has no attribute 'epoll’
    mac 下使用Charles抓包华为手机app
    mac 安装注册Charles
    LINK : fatal error LNK1181: cannot open input file 'glew32.lib' error: command 'C:\Program Files (
    glew的安装
    pytest . class
    无法导入cv2模块(Python 3.6)
    mac 使用tesseract识别图片中的中文
    xsxsxs
    struct 声明类
  • 原文地址:https://www.cnblogs.com/yufenghou/p/5203716.html
Copyright © 2011-2022 走看看