Unity?使用tiledmap解析地圖的詳細過程
1、先使用tiledmap編輯地圖,圖層用來刷圖塊,對象用來定義單個格子的數(shù)據(jù)

2、為每個圖塊調(diào)屬性

3、圖塊需要單獨配置屬性的就必須創(chuàng)建對象,并設(shè)置值

右鍵設(shè)置屬性

4、導(dǎo)出json文件


5、代碼如下,詳細看相應(yīng)注釋
using SimpleJSON;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class tmx : MonoBehaviour
{
void Start()
{
TextAsset text = Resources.Load<TextAsset>(string.Format("{0}/{1}", Const.CONFIG_TMX_PATH, "map1"));//加載tmx文件
JSONNode data = JSONNode.Parse(text.text).AsObject;//將tmx文本轉(zhuǎn)化為json對象
//1、對象層操作
Dictionary<string, JSONNode> objectsDic = new Dictionary<string, JSONNode>();
string layers_objects = data["layers"][1]["objects"].ToString();
JSONArray arr_layers_objects = JSONNode.Parse(layers_objects).AsArray;
int tilewidth = int.Parse(data["tilewidth"]);//獲取格子寬
int tileheight = int.Parse(data["tileheight"]);//獲取格子高
foreach (var obj in arr_layers_objects)//遍歷所有對象層上的對象
{
int key_x = obj.Value["x"] / tilewidth;//格子x軸 除以 格子寬得出這個對象在x軸第幾個格子內(nèi)
int key_y = obj.Value["y"] / tileheight;//格子y軸 除以 格子高 得出這個對象在y軸第幾個格子內(nèi)
objectsDic[string.Format("{0}-{1}", key_y, key_x)] = obj.Value["properties"];//將對象里的值保存到對應(yīng)格子內(nèi)
}
//圖層
string layers_data = data["layers"][0]["data"].ToString();//獲取圖層內(nèi)的二維數(shù)組
JSONArray arr_layers_data = JSONNode.Parse(layers_data).AsArray;
JSONNode tileproperties = data["tilesets"][0]["tileproperties"];//獲取對應(yīng)圖層二維數(shù)組內(nèi)的格子對象
//int tilesets = int.Parse(tileproperties["1"]["ID"]);
int col = int.Parse(data["width"]);//獲取橫向有多少個格子
int row = int.Parse(data["height"]);//獲取縱向有多少個格子
float sprite_size = 0.66f;//每個方格64像素+空余2像素
Vector3 vec = new Vector3(-int.Parse((col / 2).ToString()) * sprite_size, int.Parse((row / 2).ToString()) * sprite_size, 0);
for (int i = 0; i < row; i++)//從左向右
{
for (int j = 0; j < col; j++)//從上到下
{
int gid = arr_layers_data[j + i * col] - 1;//獲取二維數(shù)組里的值
if (gid == -1)//如果此格子沒有刷,則值為-1
continue;
JSONNode Dic_Grid = tileproperties[gid.ToString()];//轉(zhuǎn)換對應(yīng)的格子對象
var go = Instantiate(Resources.Load(string.Format("{0}/{1}", Const.PREFAB_PATH, "Grid"))) as GameObject;
go.name = string.Format("{0}_{1}_{2}", i, j, int.Parse(Dic_Grid["ID"]));//獲取格子對象的ID
go.transform.SetParent(transform);
if (objectsDic.ContainsKey(string.Format("{0}-{1}", i, j)))
{
var __objectsDic = objectsDic[string.Format("{0}-{1}", i, j)];
if (__objectsDic["ROW"] != null && __objectsDic["COL"] != null)
{
int __col = int.Parse(__objectsDic["COL"]);
int __row = int.Parse(__objectsDic["ROW"]);
var start = vec + new Vector3(sprite_size * j, -sprite_size * i, 0);
var end = vec + new Vector3(sprite_size * (j + __col - 1), -sprite_size * (i + __row - 1), 0);
var pos = (start + end) / 2f;
go.transform.localPosition = pos;
go.GetComponent<SpriteRenderer>().size = new Vector2(go.GetComponent<SpriteRenderer>().size.x * __col + 0.02f * __col - 0.02f, 0.675f * __row + 0.02f * __row - 0.02f);
}
else if (__objectsDic["ROW"] != null)
{
var start = vec.y - sprite_size * i;
var end = vec.y - sprite_size * (i + int.Parse(__objectsDic["ROW"]) - 1);
var y = (start + end) / 2f;
go.transform.localPosition = new Vector3(sprite_size * j + vec.x, y, 0);
}
else if (__objectsDic["COL"] != null)
{
var start = vec.x + sprite_size * j;
var end = vec.x + sprite_size * (j + int.Parse(__objectsDic["COL"]) - 1);
var x = (start + end) / 2f;
go.transform.localPosition = new Vector3(x, -sprite_size * i + vec.y, 0);
}
else
{
go.transform.localPosition = vec + new Vector3(sprite_size * j, -sprite_size * i, 0);
}
}
//if (Dic_Grid["ROW"] != null && Dic_Grid["COL"] != null)
//{
// var start = vec + new Vector3(sprite_size * j, -sprite_size * i, 0);
// var end = vec + new Vector3(sprite_size * (j + int.Parse(Dic_Grid["COL"]) - 1), -sprite_size * (i + int.Parse(Dic_Grid["ROW"]) - 1), 0);
// var pos = (start + end) / 2f;
// go.transform.localPosition = pos;
//}
//else if (Dic_Grid["ROW"] != null)
//{
// var start = vec.y - sprite_size * i;
// var end = vec.y - sprite_size * (i + int.Parse(Dic_Grid["ROW"]) - 1);
// var y = (start + end) / 2f;
// go.transform.localPosition = new Vector3(sprite_size * j + vec.x, y, 0);
//}
//else if (Dic_Grid["COL"] != null)
//{
// var start = vec.x + sprite_size * j;
// var end = vec.x + sprite_size * (j + int.Parse(Dic_Grid["COL"]) - 1);
// var x = (start + end) / 2f;
// go.transform.localPosition = new Vector3(x, -sprite_size * i + vec.y, 0);
//}
else
{
go.transform.localPosition = vec + new Vector3(sprite_size * j, -sprite_size * i, 0);
}
}
}
//Debug.Log(int.Parse((5 / 2).ToString()));
}
void Update()
{
}
}到此這篇關(guān)于Unity 使用tiledmap解析地圖的文章就介紹到這了,更多相關(guān)Unity tiledmap解析地圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#發(fā)送數(shù)據(jù)到剪貼板及從剪貼板中取數(shù)據(jù)的方法
這篇文章主要介紹了C#發(fā)送數(shù)據(jù)到剪貼板及從剪貼板中取數(shù)據(jù)的方法,涉及C#針對剪貼板數(shù)據(jù)的讀寫操作技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-07-07
C#中FormClosing與FormClosed的區(qū)別詳細解析
本文是對C#中FormClosing與FormClosed的區(qū)別進行了詳細的分析介紹,需要的朋友可以過來參考下,希望對大家有所幫助2013-10-10
unity實現(xiàn)虛擬搖桿控制Virtual Joystick
這篇文章主要為大家詳細介紹了unity實現(xiàn)虛擬搖桿控制Virtual Joystick,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-04-04
C#結(jié)合數(shù)據(jù)庫的數(shù)據(jù)采集器示例
這篇文章主要介紹了C#結(jié)合數(shù)據(jù)庫的數(shù)據(jù)采集器,功能比較實用,需要的朋友可以參考下2014-07-07

