日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Unity?使用tiledmap解析地圖的詳細過程_C#教程

作者:Cuijiahao ? 更新時間: 2022-06-12 編程語言

1、先使用tiledmap編輯地圖,圖層用來刷圖塊,對象用來定義單個格子的數據

2、為每個圖塊調屬性

?

3、圖塊需要單獨配置屬性的就必須創建對象,并設置值

右鍵設置屬性?

4、導出json文件

?

?

5、代碼如下,詳細看相應注釋

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文本轉化為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軸第幾個格子內
            int key_y = obj.Value["y"] / tileheight;//格子y軸 除以 格子高 得出這個對象在y軸第幾個格子內
            objectsDic[string.Format("{0}-{1}", key_y, key_x)] = obj.Value["properties"];//將對象里的值保存到對應格子內
        }
 
        //圖層
        string layers_data = data["layers"][0]["data"].ToString();//獲取圖層內的二維數組
        JSONArray arr_layers_data = JSONNode.Parse(layers_data).AsArray;
        JSONNode tileproperties = data["tilesets"][0]["tileproperties"];//獲取對應圖層二維數組內的格子對象
        //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;//獲取二維數組里的值
                if (gid == -1)//如果此格子沒有刷,則值為-1
                    continue;
                JSONNode Dic_Grid = tileproperties[gid.ToString()];//轉換對應的格子對象
                
                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()
    {
        
    }
}

原文鏈接:https://blog.csdn.net/cuijiahao/article/details/124044533

欄目分類
最近更新