{"id":2285,"date":"2021-08-25T16:56:38","date_gmt":"2021-08-25T08:56:38","guid":{"rendered":"http:\/\/www.u3d8.com\/?p=2285"},"modified":"2021-08-25T17:01:35","modified_gmt":"2021-08-25T09:01:35","slug":"unity%e4%b9%8btilemap%e5%af%bc%e5%87%ba%e5%9c%b0%e5%9b%be%e6%95%b0%e6%8d%ae","status":"publish","type":"post","link":"http:\/\/www.u3d8.com\/?p=2285","title":{"rendered":"Unity\u4e4bTilemap\u5bfc\u51fa\u5730\u56fe\u6570\u636e"},"content":{"rendered":"\n<p>\u672c\u6587\u4ecb\u7ecd\u7684\u662fUnity\u5b98\u65b9\u76842d\u5730\u7f16\u5de5\u5177\u5982\u4f55\u5bfc\u51fa\u5730\u56fe\u6570\u636e<\/p>\n\n\n\n<p>\u4e0b\u9762\u5f00\u59cb\u6211\u4eec\u7684\u6559\u7a0b<\/p>\n\n\n\n<p style=\"font-size:26px\"><strong>\u4e00\u3001\u6dfb\u52a0\u4e24\u4e2a\u811a\u672c<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>using System.Collections;\r\nusing System.Collections.Generic;\r\nusing UnityEngine;\r\nusing UnityEngine.Tilemaps;\r\n\r\n&#91;RequireComponent(typeof(Tilemap))]\r\npublic class TilemapBehaviour : MonoBehaviour\r\n{\r\n    private Tilemap tileMap;\r\n    public Tilemap Tilemap\r\n    {\r\n        get\r\n        {\r\n            if (tileMap == null)\r\n                tileMap = GetComponent&lt;Tilemap>();\r\n            return tileMap;\r\n        }\r\n    }\r\n\r\n    public BoundsInt area;\r\n}\r\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>using System.Collections;\r\nusing System.Collections.Generic;\r\nusing UnityEngine;\r\nusing UnityEditor;\r\nusing UnityEngine.Tilemaps;\r\nusing System.IO;\r\n\r\n&#91;CustomEditor(typeof(TilemapBehaviour))]\r\n&#91;CanEditMultipleObjects]\r\npublic class TilemapInspector : Editor\r\n{\r\n    private TilemapBehaviour tilemapBehaviour;\r\n\r\n    public override void OnInspectorGUI()\r\n    {\r\n        tilemapBehaviour = (TilemapBehaviour)target;\r\n        base.OnInspectorGUI();\r\n        if (GUILayout.Button(\"\u5bfc\u51fa\u5730\u56fe\"))\r\n        {\r\n            ExportMap();\r\n        }\r\n\r\n        if (GUILayout.Button(\"\u6e05\u9664\u5730\u56fe\"))\r\n        {\r\n            if (EditorUtility.DisplayDialog(\"\u63d0\u793a\", \"\u786e\u5b9a\u8981\u6e05\u9664\u5730\u56fe\u5417\uff1f\", \"\u786e\u5b9a\", \"\u53d6\u6d88\"))\r\n                ClearMap();\r\n        }\r\n    }\r\n\r\n\r\n    public void ExportMap()\r\n    {\r\n        TileBase&#91;] tileArray = tilemapBehaviour.Tilemap.GetTilesBlock(tilemapBehaviour.area);\r\n\r\n        Debug.Log(string.Format(\"Tilemap\uff1a{0} \u51c6\u5907\u5bfc\u51fa\u5730\u56fe\u6570\u636e\", tilemapBehaviour.Tilemap.name));\r\n\r\n        int tilecount = 0;\r\n\r\n        Dictionary&lt;string, string> data = new Dictionary&lt;string, string>();\r\n\r\n        for (int i = tilemapBehaviour.area.xMin; i &lt; tilemapBehaviour.area.xMax; i++)\r\n        {\r\n            for (int j = tilemapBehaviour.area.yMin; j &lt; tilemapBehaviour.area.yMax; j++)\r\n            {\r\n                \r\n                Vector3Int tempVec = new Vector3Int(i, j, 0);\r\n                if (tilemapBehaviour.Tilemap.GetTile(tempVec) == null)\r\n                    continue;\r\n\r\n                Debug.Log(string.Format(\"\u4f4d\u7f6e\uff1a{0} Tile\uff1a{1}\", tempVec.ToString(), tilemapBehaviour.Tilemap.GetTile(tempVec).ToString()));\r\n\r\n                data.Add(i + \"_\" + j, tilemapBehaviour.Tilemap.GetTile(tempVec).name);\r\n\r\n                tilecount++;\r\n            }\r\n        }\r\n\r\n        string jsonData = LitJson.JsonMapper.ToJson(data);\r\n\r\n        ExportMap(jsonData, tilemapBehaviour.Tilemap.name);\r\n\r\n        Debug.Log(string.Format(\"Tilemap\uff1a{0} \u603b\u5171Tile\u6570\u91cf\uff1a{1}\", tilemapBehaviour.Tilemap.name, tilecount.ToString()));\r\n\r\n    }\r\n\r\n\r\n    public void ExportMap(string data, string mapName)\r\n    {\r\n        string fullPath = EditorUtility.SaveFilePanel(\"\u4fdd\u5b58\u5730\u56fe\u6587\u4ef6\", Application.dataPath, mapName, \"json\");\r\n        if (string.IsNullOrEmpty(fullPath))\r\n            return;\r\n\r\n        TextWriter tw = new StreamWriter(fullPath, false);\r\n        tw.Write(data);\r\n        tw.Close();\r\n        Debug.Log(\"\u5bfc\u51fa\u5730\u56fe\u5b8c\u6210 path:\" + fullPath);\r\n        AssetDatabase.SaveAssets();\r\n        AssetDatabase.Refresh();\r\n    }\r\n\r\n    public void ClearMap()\r\n    {\r\n        tilemapBehaviour.Tilemap.ClearAllTiles();\r\n    }\r\n}\r\n<\/code><\/pre>\n\n\n\n<p style=\"font-size:26px\"><strong>\u4e8c\u3001\u5728Tilemap\u7269\u4f53\u4e0a\u6302\u8f7d\u201cTilemapBehaviour.cs\u201d<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"http:\/\/www.u3d8.com\/wp-content\/uploads\/2021\/08\/QQ\u622a\u56fe20210825165126.jpg\" data-lightbox=\"image_lg\"><img title=\"Unity\u4e4bTilemap\u5bfc\u51fa\u5730\u56fe\u6570\u636e - \u7b2c1\u5f20  | u3d8\u6280\u672f\u5206\u4eab\" alt=\"Unity\u4e4bTilemap\u5bfc\u51fa\u5730\u56fe\u6570\u636e - \u7b2c1\u5f20  | u3d8\u6280\u672f\u5206\u4eab\"  loading=\"lazy\" width=\"427\" height=\"660\"  data-src=\"http:\/\/www.u3d8.com\/wp-content\/uploads\/2021\/08\/QQ\u622a\u56fe20210825165126.jpg\" alt=\"\" class=\"wp-image-2286\" srcset=\"http:\/\/www.u3d8.com\/wp-content\/uploads\/2021\/08\/QQ\u622a\u56fe20210825165126.jpg 427w, http:\/\/www.u3d8.com\/wp-content\/uploads\/2021\/08\/QQ\u622a\u56fe20210825165126-194x300.jpg 194w\" sizes=\"(max-width: 427px) 100vw, 427px\" \/><\/a><\/figure>\n\n\n\n<p style=\"font-size:26px\"><strong>\u4e09\u3001\u624b\u52a8\u586b\u5165Area\u503c<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><a href=\"http:\/\/www.u3d8.com\/wp-content\/uploads\/2021\/08\/tilemap_area.gif\" data-lightbox=\"image_lg\"><img title=\"Unity\u4e4bTilemap\u5bfc\u51fa\u5730\u56fe\u6570\u636e - \u7b2c2\u5f20  | u3d8\u6280\u672f\u5206\u4eab\" alt=\"Unity\u4e4bTilemap\u5bfc\u51fa\u5730\u56fe\u6570\u636e - \u7b2c2\u5f20  | u3d8\u6280\u672f\u5206\u4eab\"  loading=\"lazy\"  data-src=\"http:\/\/www.u3d8.com\/wp-content\/uploads\/2021\/08\/tilemap_area-1024x531.gif\" alt=\"\" class=\"wp-image-2287\" width=\"674\" height=\"349\" srcset=\"http:\/\/www.u3d8.com\/wp-content\/uploads\/2021\/08\/tilemap_area-1024x531.gif 1024w, http:\/\/www.u3d8.com\/wp-content\/uploads\/2021\/08\/tilemap_area-300x156.gif 300w, http:\/\/www.u3d8.com\/wp-content\/uploads\/2021\/08\/tilemap_area-768x399.gif 768w, http:\/\/www.u3d8.com\/wp-content\/uploads\/2021\/08\/tilemap_area-1536x797.gif 1536w\" sizes=\"(max-width: 674px) 100vw, 674px\" \/><\/a><\/figure>\n\n\n\n<p style=\"font-size:26px\"><strong>\u56db\u3001\u70b9\u51fb\u5bfc\u51fa\u5373\u53ef<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"http:\/\/www.u3d8.com\/wp-content\/uploads\/2021\/08\/QQ\u622a\u56fe20210825165404.jpg\" data-lightbox=\"image_lg\"><img title=\"Unity\u4e4bTilemap\u5bfc\u51fa\u5730\u56fe\u6570\u636e - \u7b2c3\u5f20  | u3d8\u6280\u672f\u5206\u4eab\" alt=\"Unity\u4e4bTilemap\u5bfc\u51fa\u5730\u56fe\u6570\u636e - \u7b2c3\u5f20  | u3d8\u6280\u672f\u5206\u4eab\"  loading=\"lazy\" width=\"414\" height=\"172\"  data-src=\"http:\/\/www.u3d8.com\/wp-content\/uploads\/2021\/08\/QQ\u622a\u56fe20210825165404.jpg\" alt=\"\" class=\"wp-image-2288\" srcset=\"http:\/\/www.u3d8.com\/wp-content\/uploads\/2021\/08\/QQ\u622a\u56fe20210825165404.jpg 414w, http:\/\/www.u3d8.com\/wp-content\/uploads\/2021\/08\/QQ\u622a\u56fe20210825165404-300x125.jpg 300w\" sizes=\"(max-width: 414px) 100vw, 414px\" \/><\/a><\/figure>\n\n\n\n<p>\u5bfc\u51fa\u7684\u5730\u56fe\u6570\u636e\u6837\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\"3_12\":\"gray\",\"4_9\":\"gray\",\"6_11\":\"gray\",\"8_15\":\"gray 1\",\"8_16\":\"gray 1\",\"11_16\":\"gray\"}<\/code><\/pre>\n\n\n\n<p>\u8fd9\u91cc\u53ea\u793a\u8303\u4e0b\u7b80\u5355\u7684\u5bfc\u51fa\u6570\u636e\uff0c\u5177\u4f53\u6570\u636e\u683c\u5f0f\u53ef\u4ee5\u81ea\u5df1\u6839\u636e\u9879\u76ee\u9700\u6c42\u5b9a\u4e49<\/p>\n\n\n\n<p>GitHub\u4e0b\u8f7d\u5730\u5740\uff1a<\/p>\n\n\n\n<p><a href=\"https:\/\/github.com\/654306663\/TilemapExporter\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/github.com\/654306663\/TilemapExporter<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u4ecb\u7ecd\u7684\u662fUnity\u5b98\u65b9\u76842d\u5730\u7f16\u5de5\u5177\u5982\u4f55\u5bfc\u51fa\u5730\u56fe\u6570\u636e \u4e0b\u9762\u5f00\u59cb\u6211\u4eec\u7684\u6559\u7a0b \u4e00 &hellip; <a href=\"http:\/\/www.u3d8.com\/?p=2285\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[297,23],"tags":[],"_links":{"self":[{"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/posts\/2285"}],"collection":[{"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2285"}],"version-history":[{"count":4,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/posts\/2285\/revisions"}],"predecessor-version":[{"id":2292,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/posts\/2285\/revisions\/2292"}],"wp:attachment":[{"href":"http:\/\/www.u3d8.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2285"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2285"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2285"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}