{"id":5013,"date":"2021-08-06T19:48:51","date_gmt":"2021-08-06T11:48:51","guid":{"rendered":"https:\/\/yanjingang.com\/blog\/?p=5013"},"modified":"2021-08-14T14:39:50","modified_gmt":"2021-08-14T06:39:50","slug":"%e5%b0%8f%e7%8c%aa%e5%ad%a6u3d-%e6%89%8b%e6%8a%8a%e6%89%8b%e6%95%99%e4%bd%a0%e5%81%9a%e5%a1%94%e9%98%b2%e6%b8%b8%e6%88%8f","status":"publish","type":"post","link":"https:\/\/yanjingang.com\/blog\/?p=5013","title":{"rendered":"\u5c0f\u732a\u5b66U3D\u2014\u5854\u9632-\u5730\u56fe\u5e03\u7f6e(TowerDefense-Maze)"},"content":{"rendered":"<p>\u4e0a\u4e00\u6b21\uff0c\u6211\u4eec\u5b66\u4e60\u4e86U3D\u7684\u5165\u95e8\uff0c\u5e76\u5c1d\u8bd5\u4e86\u7b80\u5355\u7684\u5f39\u8df3\u3001\u53d1\u5c04\u3001\u78b0\u649e\u6548\u679c\u5236\u4f5c\u6280\u5de7\u3002\u8fd9\u6b21\uff0c\u6211\u4eec\u8bd5\u56fe\u6311\u6218\u4e0b\uff0c\u5c1d\u8bd5\u505a\u4e00\u4e2a\u7c97\u7cd9\u7248\u7684\u5854\u9632\u6e38\u620f\uff1a\u901a\u8fc7\u5728\u654c\u4eba\u5fc5\u7ecf\u4e4b\u8def\u4e0a\u5efa\u9020\u9632\u5fa1\u5854\uff0c\u4ee5\u5728\u654c\u4eba\u5230\u8fbe\u76ee\u7684\u5730\u4e4b\u524d\u6d88\u706d\u5b83\u4eec\u3002\u6211\u4eec\u5c06\u901a\u8fc7\u8fd9\u4e2a\u7b80\u5355\u6e38\u620f\u7684\u5236\u4f5c\u6765\u7ec3\u4e60\u5730\u56fe\u5e03\u7f6e\u3001\u654c\u4eba\u5bfb\u8def\u3001\u9501\u5b9a\u654c\u4eba\u5e76\u653b\u51fb\u3001\u5f39\u9053\u8f68\u8ff9\u3001\u7206\u70b8\u6548\u679c\u3001\u6e38\u620f\u573a\u666f\u7ba1\u7406\u3001\u8fc7\u573a\u52a8\u753b\u30013D\u6a21\u578b\u5bfc\u5165\u3001\u70ed\u52a0\u8f7d\u7b49\u6280\u672f\u70b9\u3002<\/p>\n<p>\u4ece\u96f6\u5f00\u59cb\u505a\u4e00\u4e2a\u6e38\u620f\u4e0d\u662f\u4e00\u4ef6\u5bb9\u6613\u7684\u4e8b\u60c5\uff0c\u6709\u5f88\u591a\u57fa\u7840\u77e5\u8bc6\u9700\u8981\u5728\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\u9010\u6e10\u8865\u4e0a\u3002\u540e\u8fb9\u7684\u5185\u5bb9\u4f1a\u6bd4\u8f83\u957f\uff0c\u4f46\u662f\u4e0d\u79ef\u7845\u6b65\u65e0\u4ee5\u81f3\u5343\u91cc\uff0c\u6211\u4eec\u5148\u4ece\u5730\u56fe\u5e03\u7f6e\u5f00\u59cb\u5b66\u4e60\uff0c\u5927\u5bb6\u8010\u5fc3\u8ddf\u7740\u4e00\u6b65\u4e00\u6b65\u5b66\u4e60\u548c\u52a8\u624b\u5c1d\u8bd5\u5373\u53ef\u3002<\/p>\n<h1>\u6e38\u620f\u9762\u677f\uff08The Board &#8211; Building a Maze\uff09<\/h1>\n<p>\u672c\u8282\u4e3b\u8981\u8bb2\u89e3\u6e38\u620f\u5730\u56fe\u7684\u521b\u5efa\u3001\u5bfb\u8def\u4ee5\u53ca\u76ee\u7684\u5730\u548c\u5899\u7684\u653e\u7f6e\u3002<\/p>\n<p><a href=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/tower-defense-board.jpg\"><img loading=\"lazy\" class=\"alignnone size-full wp-image-5016\" src=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/tower-defense-board.jpg\" alt=\"\" width=\"1024\" height=\"512\" srcset=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/tower-defense-board.jpg 1024w, https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/tower-defense-board-300x150.jpg 300w, https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/tower-defense-board-768x384.jpg 768w, https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/tower-defense-board-624x312.jpg 624w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/p>\n<h2>1.\u9762\u677f\uff1a\u521b\u5efa\u74e6\u7247\u6e38\u620f\u9762\u677f<\/h2>\n<h3>1.1.\u9762\u677f\uff08Board\uff09<\/h3>\n<p>a.\u521b\u5efa\u540d\u4e3aTowerDefense\u76843D\u9879\u76ee<\/p>\n<p>b.\u6dfb\u52a0\u540d\u4e3aGame Board\u7684\u7a7a\u5bf9\u8c61\uff0c\u5e76\u521b\u5efa\/\u7ed1\u5b9aGameBoard.cs\u811a\u672c<\/p>\n<p><a href=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/board-inspector.png\"><img loading=\"lazy\" class=\"alignnone size-full wp-image-5028\" src=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/board-inspector.png\" alt=\"\" width=\"640\" height=\"356\" srcset=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/board-inspector.png 640w, https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/board-inspector-300x167.png 300w, https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/board-inspector-624x347.png 624w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/a><\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">using System.Collections;\r\nusing System.Collections.Generic;\r\nusing UnityEngine;\r\n\r\npublic class GameBoard : MonoBehaviour\r\n{\r\n    [SerializeField]\r\n    Transform ground = default; \/\/\u6e38\u620f\u9762\u677f\u63a7\u5236\u5bf9\u8c61\r\n\r\n    Vector2Int size;  \/\/\u6e38\u620f\u9762\u677f\u5c3a\u5bf8\r\n\r\n\r\n    public void Initialize(Vector2Int size){\r\n      this.size = size;\r\n      ground.localScale = new Vector3(size.x, size.y, 1f);  \/\/\u76f8\u5bf9\u7236\u7ea7\u7f29\u653e\u6bd4\u4f8b\r\n    }\r\n\r\n}\r\n<\/code><\/pre>\n<p>c.\u521b\u5efa\u540d\u4e3aGround\u7684\u56db\u8fb9\u5f62\u5bf9\u8c61\uff0c\u65cb\u8f6cX\u8f7490\u5ea6(\u94fa\u6210\u5e73\u9762)\uff0c\u5e76\u6dfb\u52a0\u7c7b\u4f3c\u5730\u9762\u7684\u6750\u8d28\uff1b\u5c06Ground\u5bf9\u8c61\u7ed1\u5b9a\u5230Game Board\u5bf9\u8c61\u7684groud\u5c5e\u6027\u4e0a\u00a0<a href=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/ground-inspector.png\"><img loading=\"lazy\" class=\"alignnone size-full wp-image-5030\" src=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/ground-inspector.png\" alt=\"\" width=\"640\" height=\"452\" srcset=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/ground-inspector.png 640w, https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/ground-inspector-300x212.png 300w, https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/ground-inspector-624x441.png 624w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/a><\/p>\n<p>\u6b64\u65f6\u7684\u7ed3\u6784\u5982\u4e0b\uff1a<\/p>\n<p><a href=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/game-hierarchy.png\"><img loading=\"lazy\" class=\"alignnone size-full wp-image-5029\" src=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/game-hierarchy.png\" alt=\"\" width=\"450\" height=\"168\" srcset=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/game-hierarchy.png 450w, https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/game-hierarchy-300x112.png 300w\" sizes=\"(max-width: 450px) 100vw, 450px\" \/><\/a><\/p>\n<h3>1.2.\u6e38\u620f\uff08Game\uff09<\/h3>\n<p>a.\u521b\u5efa\u540d\u4e3aGame\u7684\u7a7a\u5bf9\u8c61\uff0c\u5e76\u521b\u5efa\/\u7ed1\u5b9aGame.cs\uff0c\u8bbe\u7f6eBoard\u5c5e\u6027\u4e3aGame Board<\/p>\n<p><a href=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/game.png\"><img loading=\"lazy\" class=\"alignnone size-full wp-image-5031\" src=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/game.png\" alt=\"\" width=\"640\" height=\"308\" srcset=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/game.png 640w, https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/game-300x144.png 300w, https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/game-624x300.png 624w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/a><\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">using System.Collections;\r\nusing System.Collections.Generic;\r\nusing UnityEngine;\r\n\r\n\/\/ \u6e38\u620f\u5bf9\u8c61\r\npublic class Game : MonoBehaviour\r\n{\r\n    [SerializeField]\r\n    Vector2Int boardSize = new Vector2Int(11, 11);\r\n\r\n    [SerializeField]\r\n    GameBoard board = default;\r\n\r\n    \/\/ \u5524\u9192\u4e8b\u4ef6\r\n    void Awake(){\r\n      board.Initialize(boardSize);  \/\/\u521d\u59cb\u531611x11\u7684\u6e38\u620f\u9762\u677f\r\n    }\r\n\r\n    \/\/ \u63a7\u5236\u6700\u5c0f\u503c\r\n    void OnValidate(){\r\n      if(boardSize.x &lt; 2){\r\n        boardSize.x = 2;\r\n      }\r\n      if(boardSize.y &lt; 2){\r\n        boardSize.y = 2;\r\n      }\r\n    }\r\n}\r\n<\/code><\/pre>\n<p>b.\u8c03\u6574\u76f8\u673a\u4e3aGameBoard\u6b63\u4e0a\u65b9\u4fef\u89c6<\/p>\n<p><a href=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/camera.png\"><img loading=\"lazy\" class=\"alignnone wp-image-5032 size-medium\" src=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/camera-300x159.png\" alt=\"\" width=\"300\" height=\"159\" srcset=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/camera-300x159.png 300w, https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/camera-624x331.png 624w, https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/camera.png 668w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<h3>1.3.\u74e6\u7247\uff08Tile\uff09<\/h3>\n<p>\u9762\u677f\u7531\u65b9\u5f62\u74e6\u7247\u7ec4\u6210\u3002\u654c\u4eba\u5c06\u80fd\u591f\u8de8\u8fb9\u7f18\u4ece\u4e00\u4e2a\u74e6\u7247\u79fb\u5230\u53e6\u4e00\u4e2a\u74e6\u7247\uff0c\u4f46\u662f\u4e0d\u80fd\u5bf9\u89d2\u7ebf\u79fb\u52a8\u3002\u8fd0\u52a8\u5c06\u59cb\u7ec8\u671d\u7740\u6700\u8fd1\u7684\u76ee\u7684\u5730\u8fdb\u884c\u3002\u6211\u4eec\u7528\u7bad\u5934\u53ef\u89c6\u5316\u6bcf\u4e2a\u74e6\u7247\u79fb\u52a8\u65b9\u5411\u3002<\/p>\n<p>a.\u4e0b\u8f7d\u4e0b\u65b9\u7684\u7bad\u5934\u7eb9\u7406\u6587\u4ef6\uff0c\u628a\u5b83\u653e\u5230\u9879\u76ee\u7684Assets\/Resources\u76ee\u5f55\u4e0b<\/p>\n<p><a href=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/arrow.png\"><img loading=\"lazy\" class=\"alignnone size-full wp-image-5033\" src=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/arrow.png\" alt=\"\" width=\"256\" height=\"256\" srcset=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/arrow.png 256w, https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/arrow-150x150.png 150w\" sizes=\"(max-width: 256px) 100vw, 256px\" \/><\/a><\/p>\n<p>b.\u65b0\u5efa\u540d\u4e3aTile Arrow\u7684\u6750\u8d28\uff0c\u9009\u53d6\u4e0a\u8fb9\u7684\u7bad\u5934\u7eb9\u7406<\/p>\n<p><a href=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/arrow-material.png\"><img loading=\"lazy\" class=\"alignnone size-full wp-image-5034\" src=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/arrow-material.png\" alt=\"\" width=\"640\" height=\"260\" srcset=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/arrow-material.png 640w, https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/arrow-material-300x122.png 300w, https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/arrow-material-624x254.png 624w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/a><\/p>\n<p>c.\u65b0\u5efa\u540d\u4e3aArrow\u7684\u56db\u8fb9\u5f62\u5bf9\u8c61\uff0c\u8c03\u6574\u4f4d\u7f6e\/\u65cb\u8f6c\u89d2\u5ea6\/\u7f29\u653e\u6bd4\u4f8b\uff0c\u5e76\u7ed1\u5b9aTile Arrow\u7eb9\u7406<\/p>\n<p><a href=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/arrow-inspector.png\"><img loading=\"lazy\" class=\"alignnone size-full wp-image-5036\" src=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/arrow-inspector.png\" alt=\"\" width=\"640\" height=\"416\" srcset=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/arrow-inspector.png 640w, https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/arrow-inspector-300x195.png 300w, https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/arrow-inspector-624x406.png 624w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/a><\/p>\n<p>c.\u65b0\u5efa\u540d\u4e3aGame Tile\u7a7a\u5bf9\u8c61\uff0c\u5e76\u521b\u5efa\/\u7ed1\u5b9aGameTile.cs\uff0c\u8bbe\u7f6earrow\u5c5e\u6027\u4e3aGame Arror<\/p>\n<p><a href=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/tile-inspector.png\"><img loading=\"lazy\" class=\"alignnone size-full wp-image-5037\" src=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/tile-inspector.png\" alt=\"\" width=\"640\" height=\"356\" srcset=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/tile-inspector.png 640w, https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/tile-inspector-300x167.png 300w, https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/tile-inspector-624x347.png 624w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/a><\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">using System.Collections;\r\nusing System.Collections.Generic;\r\nusing UnityEngine;\r\n\/\/ \u74e6\u7247\u5bf9\u8c61\r\npublic class GameTile : MonoBehaviour\r\n{\r\n    [SerializeField]\r\n    Transform arrow = default;\r\n}\r\n<\/code><\/pre>\n<p>d.\u8c03\u6574Arrow\u4e3aGameTile\u7684\u5b50\u5bf9\u8c61<\/p>\n<p><a href=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/prefab-hierarchy.png\"><img loading=\"lazy\" class=\"alignnone size-full wp-image-5039\" src=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/prefab-hierarchy.png\" alt=\"\" width=\"450\" height=\"120\" srcset=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/prefab-hierarchy.png 450w, https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/prefab-hierarchy-300x80.png 300w\" sizes=\"(max-width: 450px) 100vw, 450px\" \/><\/a><\/p>\n<h3>1.4.\u5e03\u7f6e\u74e6\u7247\uff08Laying Tiles\uff09<\/h3>\n<p>a.\u4fee\u6539GameBoard.cs\uff0c\u6dfb\u52a0tilePrefab\u5c5e\u6027\uff0c\u5e76\u8bbe\u7f6e\u4e3aGameTile<\/p>\n<p><a href=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/tile-prefab-reference.png\"><img loading=\"lazy\" class=\"alignnone size-full wp-image-5038\" src=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/tile-prefab-reference.png\" alt=\"\" width=\"640\" height=\"152\" srcset=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/tile-prefab-reference.png 640w, https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/tile-prefab-reference-300x71.png 300w, https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/tile-prefab-reference-624x148.png 624w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/a><\/p>\n<p>b.\u5728Initialize\u65b9\u6cd5\u4e2d\u521d\u59cb\u531611&#215;11\u74e6\u7247\u4f4d\u7f6e<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">using System.Collections;\r\nusing System.Collections.Generic;\r\nusing UnityEngine;\r\n\r\n\/\/ \u6e38\u620f\u9762\u677f\u5bf9\u8c61\r\npublic class GameBoard : MonoBehaviour\r\n{\r\n    [SerializeField]\r\n    Transform ground = default; \/\/\u6e38\u620f\u9762\u677f\u63a7\u5236\u5bf9\u8c61\r\n\r\n    Vector2Int size;  \/\/\u6e38\u620f\u9762\u677f\u5c3a\u5bf8\r\n\r\n    [SerializeField]\r\n    GameTile tilePrefab = default; \/\/\u6e38\u620f\u74e6\u7247\r\n\r\n    GameTile[] tiles; \/\/ \u74e6\u7247\u5217\u8868\r\n\r\n    \/\/ \u521d\u59cb\u5316\u65b9\u6cd5\r\n    public void Initialize(Vector2Int size){\r\n      this.size = size;\r\n      ground.localScale = new Vector3(size.x, size.y, 1f);  \/\/\u76f8\u5bf9\u7236\u7ea7\u7f29\u653e\u6bd4\u4f8b\r\n\r\n      tiles = new GameTile[size.x * size.y];\r\n\r\n      \/\/ \u521d\u59cb\u531611x11\u7684\u74e6\u7247\u4f4d\u7f6e\r\n      Vector2 offset = new Vector2(\r\n        (size.x - 1) * 0.5f, (size.y - 1) * 0.5f\r\n      );\r\n      for(int i=0, y=0; y&lt;size.y; y++){\r\n        for(int x=0; x&lt;size.x; x++, i++){\r\n          GameTile tile = Instantiate(tilePrefab);\r\n          tile.transform.SetParent(transform, false);\r\n          tile.transform.localPosition = new Vector3(\r\n            x - offset.x, 0f, y - offset.y\r\n          );\r\n          tiles[i] = tile;  \/\/\u4fdd\u5b58\u5230\u74e6\u7247\u5217\u8868\u7528\u4e8e\u540e\u7eed\u63a7\u5236\r\n        }\r\n      }\r\n    }\r\n\r\n}\r\n<\/code><\/pre>\n<p>c.\u8fd0\u884c\u6e38\u620f\uff0c\u53ef\u4ee5\u770b\u5230\u6446\u653e\u597d\u768411&#215;11\u4e2a\u74e6\u7247<\/p>\n<p><a href=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/tiles.png\"><img loading=\"lazy\" class=\"alignnone size-full wp-image-5041\" src=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/tiles.png\" alt=\"\" width=\"460\" height=\"460\" srcset=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/tiles.png 460w, https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/tiles-150x150.png 150w, https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/tiles-300x300.png 300w\" sizes=\"(max-width: 460px) 100vw, 460px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<h2>2.\u5bfb\u8def\uff1a\u5e7f\u5ea6\u4f18\u5148\u641c\u7d22\u6765\u5bfb\u627e\u8def\u5f84<\/h2>\n<p>\u6b64\u65f6\uff0c\u6bcf\u4e2a\u74e6\u7247\u90fd\u6709\u4e00\u4e2a\u7bad\u5934\uff0c\u4f46\u5b83\u4eec\u90fd\u6307\u5411\u6b63Z\u65b9\u5411\uff0c\u6211\u4eec\u5c06\u5176\u89e3\u91ca\u4e3a\u5317\u3002\u4e0b\u4e00\u6b65\u662f\u627e\u51fa\u6bcf\u4e2a\u74e6\u7247\u7684\u6b63\u786e\u65b9\u5411\u3002\u6211\u4eec\u901a\u8fc7\u627e\u5230\u654c\u4eba\u5c06\u8981\u5230\u8fbe\u76ee\u7684\u5730\u7684\u8def\u5f84\u6765\u505a\u5230\u8fd9\u4e00\u70b9\u3002<\/p>\n<h3>2.1.\u90bb\u5c45\uff08Tile Neighbors\uff09<\/h3>\n<p>\u8def\u5f84\u4ece\u4e00\u4e2a\u74e6\u7247\u5230\u53e6\u4e00\u4e2a\u74e6\u7247\uff0c\u6709\u4e1c\u5357\u897f\u5317\u56db\u4e2a\u65b9\u5411\u3002<\/p>\n<p>a.\u4e3a\u4e86\u4f7f\u641c\u7d22\u5bb9\u6613\uff0c\u5148\u4e3aGameTile\u74e6\u7247\u589e\u52a0\u8ddf\u8e2a\u5bf9\u5176\u56db\u4e2a\u90bb\u5c45\u7684\u5f15\u7528\u548c\u8bbe\u7f6e\u90bb\u5c45\u7684\u65b9\u6cd5<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">using System.Collections;\r\nusing System.Collections.Generic;\r\nusing UnityEngine;\r\n\r\n\/\/ \u74e6\u7247\u5bf9\u8c61\r\npublic class GameTile : MonoBehaviour\r\n{\r\n    [SerializeField]\r\n    Transform arrow = default;  \/\/ \u7bad\u5934\r\n\r\n    GameTile north, east, south, west;  \/\/ \u5f53\u524d\u74e6\u7247\u56db\u5468\u7684\u90bb\u5c45\u74e6\u7247\u5f15\u7528\r\n\r\n    \/\/ \u8bbe\u7f6e\u90bb\u5c45\u7684\u76f8\u4e92\u5f15\u7528\u4f9d\u8d56\u5173\u7cfb\r\n    public static void MakeEastWestNeighbors(GameTile east, GameTile west){ \/\/\u4e1c\u897f\u90bb\u5c45\r\n      Debug.Assert(west.east == null &amp;&amp; east.west == null, \"Redefined neighbors!\"); \/\/\u5f53\u51fa\u73b0\u5f15\u7528\u8986\u76d6\u65f6\uff0c\u6253\u5370debug\u4fe1\u606f\r\n      west.east = east;\r\n      east.west = west;\r\n    }\r\n    public static void MakeNorthSouthNeighbors(GameTile north, GameTile south){ \/\/\u5357\u5317\u90bb\u5c45\r\n      Debug.Assert(north.south == null &amp;&amp; south.north == null, \"Redefined neighbors!\"); \/\/\u5f53\u51fa\u73b0\u5f15\u7528\u8986\u76d6\u65f6\uff0c\u6253\u5370debug\u4fe1\u606f\r\n      south.north = north;\r\n      north.south = south;\r\n    }\r\n}\r\n<\/code><\/pre>\n<p>b.\u5728\u6e38\u620f\u9762\u677f\u521b\u5efa\u74e6\u7247\u65f6\uff0c\u5efa\u7acb\u74e6\u7247\u90bb\u5c45\u7684\u76f8\u4e92\u5f15\u7528\u5173\u7cfb<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">using System.Collections;\r\nusing System.Collections.Generic;\r\nusing UnityEngine;\r\n\r\n\/\/ \u6e38\u620f\u9762\u677f\u5bf9\u8c61\r\npublic class GameBoard : MonoBehaviour\r\n{\r\n    [SerializeField]\r\n    Transform ground = default; \/\/\u6e38\u620f\u9762\u677f\u63a7\u5236\u5bf9\u8c61\r\n\r\n    Vector2Int size;  \/\/\u6e38\u620f\u9762\u677f\u5c3a\u5bf8\r\n\r\n    [SerializeField]\r\n    GameTile tilePrefab = default; \/\/\u6e38\u620f\u74e6\u7247\r\n\r\n    GameTile[] tiles; \/\/ \u74e6\u7247\u5217\u8868\r\n\r\n    \/\/ \u521d\u59cb\u5316\u65b9\u6cd5\r\n    public void Initialize(Vector2Int size){\r\n      this.size = size;\r\n      ground.localScale = new Vector3(size.x, size.y, 1f);  \/\/\u76f8\u5bf9\u7236\u7ea7\u7f29\u653e\u6bd4\u4f8b\r\n\r\n      tiles = new GameTile[size.x * size.y];\r\n\r\n      \/\/ \u521d\u59cb\u531611x11\u7684\u74e6\u7247\u4f4d\u7f6e\r\n      Vector2 offset = new Vector2(\r\n        (size.x - 1) * 0.5f, (size.y - 1) * 0.5f\r\n      );\r\n      for(int i=0, y=0; y&lt;size.y; y++){\r\n        for(int x=0; x&lt;size.x; x++, i++){\r\n          GameTile tile = Instantiate(tilePrefab);\r\n          tile.transform.SetParent(transform, false);\r\n          tile.transform.localPosition = new Vector3(\r\n            x - offset.x, 0f, y - offset.y\r\n          );\r\n          \/\/ \u4fdd\u5b58\u5230\u74e6\u7247\u5217\u8868\u7528\u4e8e\u540e\u7eed\u63a7\u5236\r\n          tiles[i] = tile;\r\n          \/\/ \u5efa\u7acb\u74e6\u7247\u90bb\u5c45\u7684\u76f8\u4e92\u5f15\u7528\u5173\u7cfb\r\n          if(x &gt; 0){\r\n            GameTile.MakeEastWestNeighbors(tile, tiles[i - 1]); \/\/\u5173\u8054\u524d\u4e00\u4e2a\u7684\u5de6\u53f3\u5173\u7cfb\r\n          }\r\n          if(y &gt; 0){\r\n            GameTile.MakeNorthSouthNeighbors(tile, tiles[i - size.x]);  \/\/\u5173\u8054\u4e0a\u4e00\u884c\u540c\u4f4d\u7f6e\u7684\u5357\u5317\u5173\u7cfb\r\n          }\r\n        }\r\n      }\r\n    }\r\n\r\n}\r\n<\/code><\/pre>\n<h3>2.2.\u65b9\u5411\u548c\u8ddd\u79bb\uff08Direction and Distance\uff09<\/h3>\n<p>\u6211\u4eec\u4e0d\u4f1a\u8ba9\u6240\u6709\u7684\u654c\u4eba\u4e00\u76f4\u5bfb\u627e\u8def\u5f84\uff0c\u800c\u662f\u63d0\u524d\u5728\u6bcf\u4e2a\u74e6\u7247\u4e0a\u8bbe\u7f6e\u597d\u8def\u5f84\u4fe1\u606f\u3002\u654c\u4eba\u53ef\u4ee5\u67e5\u8be2\u4ed6\u4eec\u6240\u5728\u7684\u74e6\u7247\uff0c\u4ee5\u4e86\u89e3\u4e0b\u4e00\u6b65\u7684\u53bb\u5411\u3002\u6211\u4eec\u5728GameTile\u4e0a\u6dfb\u52a0\u5bf9\u8def\u5f84\u4e0a\u4e0b\u4e00\u4e2a\u74e6\u7247\u7684\u5f15\u7528\uff0c\u540c\u65f6\u6dfb\u52a0\u8ddd\u79bb\u76ee\u7684\u5730\u7684\u5269\u4f59\u8ddd\u79bb(\u74e6\u7247\u6570\u91cf)\uff0c\u5e76\u63d0\u4f9b\u5bf9\u5e94\u8bbe\u7f6e\u65b9\u6cd5\uff0c\u6211\u4eec\u4f1a\u5728\u627e\u5230\u6700\u77ed\u8def\u5f84\u65f6\u4f7f\u7528\u5b83\u3002<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">using System.Collections;\r\nusing System.Collections.Generic;\r\nusing UnityEngine;\r\n\r\n\/\/ \u74e6\u7247\u5bf9\u8c61\r\npublic class GameTile : MonoBehaviour\r\n{\r\n    ...\r\n    GameTile nextOnPath;  \/\/ \u654c\u4eba\u7684\u884c\u8d70\u8def\u5f84\u7684\u4e0b\u4e00\u74e6\u7247\r\n    int distance; \/\/ \u8ddd\u79bb\u76ee\u7684\u5730\u7684\u5269\u4f59\u74e6\u7247\u6570\r\n\r\n    ...\r\n\r\n    \/\/ \u6bcf\u6b21\u67e5\u627e\u8def\u5f84\u524d\u5148\u91cd\u7f6e\u654c\u4eba\u8def\u5f84\u4fe1\u606f\r\n    public void ClearPath(){\r\n      distance = int.MaxValue;  \/\/ \u9ed8\u8ba4\u8ddd\u79bb\u65e0\u9650\r\n      nextOnPath = null;        \/\/ \u6682\u65f6\u6ca1\u6709\u4e0b\u4e00\u74e6\u7247\r\n    }\r\n    \/\/ \u5f53\u524dtile\u662f\u5426\u6709\u8def\u5f84\u7684getter\r\n    public bool HasPath =&gt; distance != int.MaxValue;\r\n\r\n    \/\/ \u8bbe\u7f6e\u4e3a\u76ee\u7684\u5730\r\n    public void BecameDestination(){\r\n      distance = 0;             \/\/ \u8ddd\u79bb\u4e3a\u96f6\uff08\u76f8\u5bf9\u4e8e\u81ea\u5df1\uff09\uff0c\u8def\u5f84\u5728\u8fd9\u91cc\u7ed3\u675f\r\n      nextOnPath = null;        \/\/ \u4e0d\u4f1a\u518d\u6709\u4e0b\u4e00\u4e2a\u74e6\u7247\u4e86\r\n    }\r\n}\r\n<\/code><\/pre>\n<h3>2.3.\u8def\u5f84\u6210\u957f\uff08Growing the Path\uff09<\/h3>\n<p>\u5982\u679c\u6211\u4eec\u6709\u4e00\u4e2a\u5e26\u6709\u8def\u5f84\u7684\u74e6\u7247\uff0c\u5219\u53ef\u4ee5\u8ba9\u5b83\u6cbf\u7740\u8def\u5f84\u5411\u5b83\u7684\u67d0\u4e2a\u90bb\u5c45\u5ef6\u4f38\u3002\u6700\u521d\u552f\u4e00\u5e26\u6709\u8def\u5f84\u7684\u74e6\u7247\u662f\u76ee\u7684\u5730\uff0c\u6240\u4ee5\u6211\u4eec\u4ece\u8ddd\u79bb0\u5f00\u59cb\uff0c\u5e76\u4ece\u90a3\u91cc\u5f00\u59cb\u589e\u52a0\u8ddd\u79bb\uff0c\u671d\u7740\u654c\u4eba\u79fb\u52a8\u7684\u76f8\u53cd\u65b9\u5411\u524d\u8fdb\u3002\u6240\u4ee5\u76ee\u6807\u7684\u6240\u6709\u76f4\u63a5\u90bb\u5c45\u7684\u8ddd\u79bb\u90fd\u662f1\uff0c\u800c\u8fd9\u4e9b\u74e6\u7247\u7684\u6240\u6709\u5176\u4ed6\u90bb\u5c45\u7684\u8ddd\u79bb\u90fd\u662f2\uff0c\u4ee5\u6b64\u7c7b\u63a8\u3002<\/p>\n<p>\u6211\u4eec\u6dfb\u52a0GrowPathTo\u53ca\u6cbf\u7279\u5b9a\u65b9\u5411\u6269\u5c55\u8def\u5f84\u7684\u65b9\u6cd5<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">using System.Collections;\r\nusing System.Collections.Generic;\r\nusing UnityEngine;\r\n\r\n\/\/ \u74e6\u7247\u5bf9\u8c61\r\npublic class GameTile : MonoBehaviour\r\n{\r\n    ...\r\n\r\n    \/\/ \u53cd\u5411\u8bbe\u7f6e\u8def\u5f84\u5173\u7cfb(\u4ece\u76ee\u7684\u5730\u74e6\u7247\u5f00\u59cb)\r\n    GameTile GrowPathTo(GameTile neighbor){\r\n      if(!HasPath || neighbor == null || neighbor.HasPath){\r\n        return null;\r\n      }\r\n      neighbor.distance = distance + 1;\r\n      neighbor.nextOnPath = this;\r\n      return neighbor;\r\n    }\r\n    \/\/ \u6cbf\u7279\u5b9a\u65b9\u5411\u6269\u5c55\u8def\u5f84\r\n    public GameTile GrowPathNorth() =&gt; GrowPathTo(north);\r\n    public GameTile GrowPathEast() =&gt; GrowPathTo(east);\r\n    public GameTile GrowPathSouth() =&gt; GrowPathTo(south);\r\n    public GameTile GrowPathWest() =&gt; GrowPathTo(west);\r\n}\r\n<\/code><\/pre>\n<h3>2.4.\u5e7f\u5ea6\u4f18\u5148\u641c\u7d22\uff08Breadth-First Search\uff09<\/h3>\n<p>GameBoard\u6709\u8d23\u4efb\u786e\u4fdd\u5176\u6240\u6709\u74e6\u7247\u90fd\u5305\u542b\u6709\u6548\u7684\u8def\u5f84\u6570\u636e\u3002\u6211\u4eec\u5c06\u901a\u8fc7\u6267\u884c\u5e7f\u5ea6\u4f18\u5148\u641c\u7d22\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002\u6211\u4eec\u4ece\u76ee\u6807\u74e6\u7247\u5f00\u59cb\uff0c\u7136\u540e\u6269\u5c55\u5230\u5176\u90bb\u5c45\u7684\u8def\u5f84\uff0c\u7136\u540e\u518d\u5230\u8fd9\u4e9b\u74e6\u7247\u7684\u90bb\u5c45\uff0c\u4f9d\u6b64\u7c7b\u63a8\u3002\u6bcf\u4e00\u6b65\uff0c\u8ddd\u79bb\u589e\u52a0\u4e00\uff0c\u8def\u5f84\u6c38\u8fdc\u4e0d\u4f1a\u671d\u7740\u5df2\u6709\u8def\u5f84\u7684\u74e6\u7247\u589e\u957f\u3002\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u6240\u6709\u74e6\u7247\u6700\u7ec8\u90fd\u6cbf\u7740\u6700\u77ed\u7684\u8def\u5f84\u6307\u5411\u76ee\u6807\u3002\uff08\u4e3a\u4ec0\u4e48\u4e0d\u7528A*\u7b97\u6cd5\uff1aA *\u662f\u5e7f\u5ea6\u4f18\u5148\u641c\u7d22\u7684\u6f14\u53d8\u3002\u641c\u7d22\u5355\u4e2a\u6700\u77ed\u8def\u5f84\u65f6\uff0c\u6b64\u529f\u80fd\u5f88\u6709\u7528\u3002\u4f46\u662f\u6211\u4eec\u9700\u8981\u6240\u6709\u6700\u77ed\u8def\u5f84\uff0c\u56e0\u6b64A *\u6ca1\u6709\u4efb\u4f55\u597d\u5904\u3002\u6709\u5173\u5e7f\u5ea6\u4f18\u5148\u641c\u7d22\u548c\u5e94\u7528\u4e8e\u5e26\u6709\u52a8\u753b\u7684\u5341\u516d\u8fdb\u5236\u7f51\u683c\u7684A *\u7684\u793a\u4f8b\uff0c\u8bf7\u53c2\u89c1 <b>Hex Map<\/b>\u00a0\u7cfb\u5217\u3002\uff09<\/p>\n<p>\u6211\u4eec\u5229\u7528Queue\u961f\u5217\u4e34\u65f6\u5b58\u50a8\u5f85\u641c\u7d22\u8def\u5f84<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">using System.Collections;\r\nusing System.Collections.Generic;\r\nusing UnityEngine;\r\n\r\n\/\/ \u6e38\u620f\u9762\u677f\u5bf9\u8c61\r\npublic class GameBoard : MonoBehaviour\r\n{\r\n    ...\r\n\r\n    Queue&lt;GameTile&gt; searchFrontier = new Queue&lt;GameTile&gt;(); \/\/ \u8def\u5f84\u641c\u7d22\u8fb9\u754c\u4e34\u65f6\u961f\u5217\r\n\r\n\r\n    \/\/ \u521d\u59cb\u5316\u65b9\u6cd5\r\n    public void Initialize(Vector2Int size){\r\n      ...\r\n\r\n      \/\/ \u641c\u7d22\u8def\u5f84\r\n      FindPaths();\r\n    }\r\n\r\n    \/\/ \u641c\u7d22\u884c\u8fdb\u8def\u5f84\r\n    void FindPaths(){\r\n      \/\/ \u6e05\u9664\u6240\u6709\u74e6\u7247\u7684\u8def\u5f84\r\n      foreach(GameTile tile in tiles){\r\n        tile.ClearPath();\r\n      }\r\n      \/\/ \u5c06\u4e00\u4e2a\u74e6\u7247\u4f5c\u4e3a\u76ee\u6807\u5e76\u5c06\u5176\u6dfb\u52a0\u5230\u8fb9\u754c\uff0c\u8fd9\u91cc\u6211\u4eec\u5c31\u9009\u7b2c\u4e00\u4e2a\u74e6\u7247\r\n      tiles[0].BecameDestination(); \/\/ \u8bbe\u7f6e\u4e3a\u76ee\u7684\u5730\r\n      searchFrontier.Enqueue(tiles[0]); \/\/ \u5c06\u76ee\u7684\u5730\u6dfb\u52a0\u5230\u8fb9\u754c\u961f\u5217\r\n      \/\/ \u542f\u52a8\u8def\u5f84\u641c\u7d22\uff08\u6267\u884c\u5b8c\u6bd5\u540e\uff0c\u6240\u6709\u74e6\u7247\u90fd\u5c06\u88ab\u8bbe\u7f6e\u597ddistance\u548cnextOnPath\uff09\r\n      while(searchFrontier.Count &gt; 0){\r\n        GameTile tile = searchFrontier.Dequeue(); \/\/\u51fa\u961f\r\n        if(tile != null){ \/\/\u6709\u53ef\u80fd\u751f\u957f\u8def\u5f84\u5165\u961f\u7684\u6709null\r\n          \/\/ \u5c064\u4e2a\u65b9\u5411\u7684\u751f\u957f\u8def\u5f84\u4e0b\u4e00\u8282\u70b9\u5165\u961f\uff0c\u7528\u4e8e\u4e0b\u4e00\u5c42\u7684\u8def\u5f84\u751f\u957f\r\n          searchFrontier.Enqueue(tile.GrowPathNorth());\r\n          searchFrontier.Enqueue(tile.GrowPathEast());\r\n          searchFrontier.Enqueue(tile.GrowPathSouth());\r\n          searchFrontier.Enqueue(tile.GrowPathWest());\r\n        }\r\n      }\r\n    }\r\n\r\n}\r\n<\/code><\/pre>\n<h3>2.5.\u5c55\u793a\u8def\u5f84\uff08Showing the Paths\uff09<\/h3>\n<p>\u73b0\u5728\uff0c\u6211\u4eec\u5f97\u5230\u4e00\u4e2a\u5305\u542b\u6709\u6548\u8def\u5f84\u7684\u6e38\u620f\u9762\u677f\uff0c\u4f46\u76ee\u524d\u8fd8\u770b\u4e0d\u5230\u3002\u6211\u4eec\u9700\u8981\u8c03\u6574\u7bad\u5934\uff0c\u4f7f\u5176\u6cbf\u7740\u7a7f\u8fc7\u74e6\u7247\u7684\u8def\u5f84\u6307\u5411\uff0c\u901a\u8fc7\u65cb\u8f6c\u5b83\u4eec\u6765\u505a\u5230\u8fd9\u4e00\u70b9\u3002<\/p>\n<p>a.\u5c06\u9759\u6001Quaternion\u5b57\u6bb5\u6dfb\u52a0\u5230GameTile\uff0c\u6bcf\u4e2a\u65b9\u5411\u6dfb\u52a0\u4e00\u4e2a\uff0c\u5e76\u901a\u8fc7ShowPath\u65b9\u6cd5\u65cb\u8f6c\u74e6\u7247\u7bad\u5934\u65b9\u5411<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">using System.Collections;\r\nusing System.Collections.Generic;\r\nusing UnityEngine;\r\n\r\n\/\/ \u74e6\u7247\u5bf9\u8c61\r\npublic class GameTile : MonoBehaviour\r\n{\r\n    ...\r\n\r\n    \/\/ \u8def\u5f84\u7bad\u5934\u65b9\u5411\u89d2\r\n    static Quaternion northRotation = Quaternion.Euler(90f, 0f, 0f),\r\n                      eastRotation = Quaternion.Euler(90f, 90f, 0f),\r\n                      southRotation = Quaternion.Euler(90f, 180f, 0f),\r\n                      westRotation = Quaternion.Euler(90f, 270f, 0f);\r\n\r\n\r\n    ...\r\n\r\n    \/\/ \u5c55\u793a\u8def\u5f84(\u65cb\u8f6c\u6240\u6709\u74e6\u7247\u7684\u7bad\u5934\u65b9\u5411)\r\n    public void ShowPath(){\r\n      if(distance == 0){  \/\/ \u76ee\u7684\u5730\u74e6\u7247\uff0c\u4e0d\u663e\u793a\u7bad\u5934\r\n        arrow.gameObject.SetActive(false);\r\n        return;\r\n      }\r\n      \/\/ \u975e\u76ee\u7684\u5730\u74e6\u7247\uff0c\u663e\u793a\u5e76\u65cb\u8f6c\u7bad\u5934\u65b9\u5411\u4e3a\u8def\u5f84\u65b9\u5411\r\n      arrow.gameObject.SetActive(true);\r\n      arrow.localRotation =\r\n        nextOnPath == north ? northRotation :\r\n        nextOnPath == east ? eastRotation :\r\n        nextOnPath == south ? southRotation :\r\n        westRotation;\r\n    }\r\n}\r\n<\/code><\/pre>\n<p>b.\u5728GameBoard.FindPaths\u672b\u5c3e\u7684\u6240\u6709\u56fe\u5757\u4e0a\u8c03\u7528ShowPath\u65b9\u6cd5<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">using System.Collections;\r\nusing System.Collections.Generic;\r\nusing UnityEngine;\r\n\r\n\/\/ \u6e38\u620f\u9762\u677f\u5bf9\u8c61\r\npublic class GameBoard : MonoBehaviour\r\n{\r\n    ...\r\n\r\n    \/\/ \u641c\u7d22\u884c\u8fdb\u8def\u5f84\r\n    void FindPaths(){\r\n      ...\r\n\r\n      \/\/ \u6839\u636e\u8def\u5f84\u8c03\u6574\u74e6\u7247\u7bad\u5934\u65b9\u5411\r\n      foreach(GameTile tile in tiles){\r\n        tile.ShowPath();\r\n      }\r\n    }\r\n}\r\n<\/code><\/pre>\n<p>c.\u8fd0\u884c\u6548\u679c<\/p>\n<p><a href=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/found-paths.png\"><img loading=\"lazy\" class=\"alignnone size-full wp-image-5057\" src=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/found-paths.png\" alt=\"\" width=\"460\" height=\"460\" srcset=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/found-paths.png 460w, https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/found-paths-150x150.png 150w, https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/found-paths-300x300.png 300w\" sizes=\"(max-width: 460px) 100vw, 460px\" \/><\/a><\/p>\n<h3>2.6.\u4ea4\u66ff\u641c\u7d22\u4f18\u5148\u7ea7\uff08Alternating Search Priority\uff09<\/h3>\n<p>\u4e8b\u5b9e\u8bc1\u660e\uff0c\u5f53\u897f\u5357\u89d2\u7684\u74e6\u7247\u4e3a\u76ee\u7684\u5730\u65f6\uff0c\u6240\u6709\u8def\u5f84\u90fd\u4e00\u76f4\u5411\u897f\u76f4\u884c\uff0c\u76f4\u5230\u5230\u8fbe\u9762\u677f\u8fb9\u7f18\uff0c\u7136\u540e\u5411\u5357\u3002\u8fd9\u662f\u6ca1\u6709\u95ee\u9898\u7684\uff0c\u5728\u4e0d\u652f\u6301\u5bf9\u89d2\u7ebf\u79fb\u52a8\u7684\u60c5\u51b5\u4e0b\uff0c\u786e\u5b9e\u6ca1\u6709\u5230\u76ee\u7684\u5730\u7684\u66f4\u77ed\u8def\u5f84\u3002\u4f46\u662f\uff0c\u8fd8\u6709\u8bb8\u591a\u5176\u4ed6\u76f8\u540c\u7684\u6700\u77ed\u7684\u8def\u5f84\u3002<\/p>\n<p>a.\u4e3a\u4e86\u66f4\u597d\u5730\u4e86\u89e3\u4e3a\u4ec0\u4e48\u627e\u5230\u8fd9\u4e9b\u8def\u5f84\uff0c\u8bf7\u5c06\u76ee\u7684\u5730\u79fb\u52a8\u5230\u5730\u56fe\u7684\u4e2d\u5fc3\u3002\u5f53\u4f7f\u7528\u5947\u6570\u677f\u5c3a\u5bf8\u65f6\uff0c\u8fd9\u53ea\u662f\u6570\u7ec4\u4e2d\u95f4\u7684\u74e6\u7247\u3002<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">\r\n    \/\/ \u641c\u7d22\u884c\u8fdb\u8def\u5f84\r\n    void FindPaths(){\r\n      ...\r\n\r\n      \/\/ \u5c06\u4e00\u4e2a\u74e6\u7247\u4f5c\u4e3a\u76ee\u6807\u5e76\u5c06\u5176\u6dfb\u52a0\u5230\u8fb9\u754c\uff0c\u8fd9\u91cc\u6211\u4eec\u5c31\u9009\u7b2c\u4e00\u4e2a\u74e6\u7247\uff08\u5f53\u4f7f\u7528\u5947\u6570\u677f\u5c3a\u5bf8\u65f6\uff0ctiles.Length\/2\u662f\u4e2d\u95f4\u7684\u74e6\u7247\uff09\r\n      GameTile target = tiles[tiles.Length \/ 2];   \/\/tiles[0]; \r\n      target.BecameDestination(); \/\/ \u8bbe\u7f6e\u4e3a\u76ee\u7684\u5730\r\n      searchFrontier.Enqueue(target); \/\/ \u5c06\u76ee\u7684\u5730\u6dfb\u52a0\u5230\u8fb9\u754c\u961f\u5217\r\n\r\n      ...\r\n    }<\/code><\/pre>\n<p>\u8fd0\u884c\u6548\u679c\uff1a<\/p>\n<p><a href=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/center-destination.png\"><img loading=\"lazy\" class=\"alignnone size-full wp-image-5059\" src=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/center-destination.png\" alt=\"\" width=\"460\" height=\"460\" srcset=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/center-destination.png 460w, https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/center-destination-150x150.png 150w, https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/center-destination-300x300.png 300w\" sizes=\"(max-width: 460px) 100vw, 460px\" \/><\/a><\/p>\n<p>\u6ce8\uff1a\u5982\u679c\u4e2d\u5fc3\u70b9\u591a\u51fa\u4e00\u4e2a\u7bad\u5934\uff0c\u53ef\u80fd\u662f\u9ed8\u8ba4\u753b\u5e03\u4e2d\u7684Arrow\u5bfc\u81f4\u7684\uff0c\u53ef\u4ee5\u628a\u62d6\u62fd\u4e0a\u53bb\u7684Arrow\u5bf9\u8c61\u8bbe\u7f6e\u4e3a\u4e0d\u53ef\u89c1\u5373\u53ef\u3002<\/p>\n<p>\u4f60\u9700\u8981\u4e86\u89e3\u641c\u7d22\u662f\u5982\u4f55\u5de5\u4f5c\u7684\uff0c\u5f53\u6211\u4eec\u5728\u4e1c\u5317-\u897f\u5357\u5e8f\u4e0a\u52a0\u4e0a\u90bb\u5c45\u65f6\uff0c\u5317\u65b9\u662f\u6700\u4f18\u5148\u7684\u3002\u5f53\u6211\u4eec\u5411\u540e\u641c\u7d22\u65f6\uff0c\u8fd9\u610f\u5473\u7740\u5357\u65b9\u662f\u6700\u540e\u4e00\u4e2a\u7ecf\u8fc7\u7684\u65b9\u5411\u3002\u8fd9\u5c31\u662f\u4e3a\u4ec0\u4e48\u53ea\u6709\u51e0\u4e2a\u7bad\u5934\u6307\u5411\u5357\u65b9\uff0c\u800c\u6709\u90a3\u4e48\u591a\u7bad\u5934\u6307\u5411\u4e1c\u65b9\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u8c03\u6574\u65b9\u5411\u4f18\u5148\u7ea7\u6765\u66f4\u6539\u7ed3\u679c\u3002\u8ba9\u6211\u4eec\u4ea4\u6362\u4e1c\u65b9\u548c\u5357\u65b9\u3002\u90a3\u5e94\u8be5\u5bfc\u81f4\u5357\u5317\u548c\u4e1c\u897f\u5bf9\u79f0\u3002<\/p>\n<p><a href=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/nsew-priorities.png\"><img loading=\"lazy\" class=\"alignnone size-full wp-image-5062\" src=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/nsew-priorities.png\" alt=\"\" width=\"460\" height=\"460\" srcset=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/nsew-priorities.png 460w, https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/nsew-priorities-150x150.png 150w, https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/nsew-priorities-300x300.png 300w\" sizes=\"(max-width: 460px) 100vw, 460px\" \/><\/a><\/p>\n<p>\u8fd9\u6837\u770b\u8d77\u6765\u66f4\u597d\uff0c\u4f46\u5982\u679c\u8def\u5f84\u5728\u81ea\u7136\u7684\u5730\u65b9\u4ea4\u66ff\u65b9\u5411\u63a5\u8fd1\u5bf9\u89d2\u79fb\u52a8\uff0c\u90a3\u5c06\u662f\u6700\u597d\u7684\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u98a0\u5012\u76f8\u90bb\u5757\u7684\u641c\u7d22\u4f18\u5148\u7ea7\u6765\u505a\u5230\u8fd9\u4e00\u70b9\uff0c\u7c7b\u4f3c\u56fd\u9645\u8c61\u68cb\u68cb\u76d8\u6a21\u5f0f\u3002\u6211\u4eec\u9700\u8981\u505a\u4e9b\u5c0f\u7684\u4ee3\u7801\u8c03\u6574\u3002<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">\/\/ \u74e6\u7247\u5bf9\u8c61\r\npublic class GameTile : MonoBehaviour\r\n{\r\n    \/\/ \u74e6\u7247\u5728\u5730\u56fe\u4e2d\u7684\u7d22\u5f15\r\n    public int Idx{get;set;}\r\n}\r\n\r\n\/\/ \u6e38\u620f\u9762\u677f\u5bf9\u8c61\r\npublic class GameBoard : MonoBehaviour\r\n{\r\n    ...\r\n    \/\/ \u521d\u59cb\u5316\u65b9\u6cd5\r\n    public void Initialize(Vector2Int size){\r\n         ...\r\n          tile.Idx = i;    \/\/\u8bb0\u5f55\u74e6\u7247\u4f4d\u7f6e\u5e8f\u53f7\r\n          tiles[i] = tile;\r\n          ...\r\n    }\r\n\r\n    \/\/ \u641c\u7d22\u884c\u8fdb\u8def\u5f84\r\n    void FindPaths(){\r\n      ...\r\n        if(tile != null){ \/\/\u6709\u53ef\u80fd\u751f\u957f\u8def\u5f84\u5165\u961f\u7684\u6709null\r\n          \/\/ \u5c064\u4e2a\u65b9\u5411\u7684\u751f\u957f\u8def\u5f84\u4e0b\u4e00\u8282\u70b9\u5165\u961f\uff0c\u7528\u4e8e\u4e0b\u4e00\u5c42\u7684\u8def\u5f84\u751f\u957f\r\n          if((tile.Idx &amp; 1) == 0){  \/\/ \u5355\u4e2a\u201c\uff06\u201d\u53f7\u662f\u4e8c\u8fdb\u5236AND\u8fd0\u7b97\u7b26\uff0c\u6240\u6709\u5076\u6570\u7684\u4e8c\u8fdb\u5236(x0 &amp; 1)\u90fd=0\u3002\u5b83\u5bf9\u5176\u64cd\u4f5c\u6570\u7684\u6bcf\u4e00\u5bf9\u4f4d\u6267\u884c\u903b\u8f91\u4e0e\u8fd0\u7b97\u3002\u56e0\u6b64\uff0c\u4e00\u5bf9\u4e2d\u7684\u4e24\u4e2a\u4f4d\u90fd\u5fc5\u987b\u90fd\u4e3a1\uff0c\u4ee5\u4f7f\u7ed3\u679c\u4f4d\u4e3a1\u3002\u4f8b\u5982\uff0c10101010\u548c00001111\u4ea7\u751f00001010\u3002\u5728\u5185\u90e8\uff0c\u6570\u5b57\u662f\u4e8c\u8fdb\u5236\u7684\u3002 \u5b83\u4eec\u4ec5\u4f7f\u75280\u548c1\u3002 \u5728\u4e8c\u8fdb\u5236\u4e2d\uff0c\u5e8f\u52171\u30012\u30013\u30014\u5206\u522b\u5199\u4e3a1\u300110\u300111\u3001100\u3002\u5982\u4f60\u6240\u89c1\uff0c\u5076\u6570\u7684\u6700\u4f4e\u6709\u6548\u4f4d\u4e3a\u96f6\u3002\r\n            searchFrontier.Enqueue(tile.GrowPathNorth());\r\n            searchFrontier.Enqueue(tile.GrowPathSouth());\r\n            searchFrontier.Enqueue(tile.GrowPathEast());\r\n            searchFrontier.Enqueue(tile.GrowPathWest());\r\n          }else{  \/\/ \u5947\u6570\u74e6\u7247\u9006\u5411\u641c\u7d22\uff0c\u4ee5\u5f62\u6210\u5bf9\u89d2\u7ebf\u548c\u952f\u9f7f\u5f62\u7684\u8def\u7ebf\r\n            searchFrontier.Enqueue(tile.GrowPathWest());\r\n            searchFrontier.Enqueue(tile.GrowPathEast());\r\n            searchFrontier.Enqueue(tile.GrowPathSouth());\r\n            searchFrontier.Enqueue(tile.GrowPathNorth());\r\n          }\r\n      ...\r\n    }\r\n}\r\n<\/code><\/pre>\n<p><a href=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/alternating-search-order.png\"><img loading=\"lazy\" class=\"alignnone size-full wp-image-5063\" src=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/alternating-search-order.png\" alt=\"\" width=\"460\" height=\"460\" srcset=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/alternating-search-order.png 460w, https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/alternating-search-order-150x150.png 150w, https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/alternating-search-order-300x300.png 300w\" sizes=\"(max-width: 460px) 100vw, 460px\" \/><\/a><\/p>\n<h2>3.\u6539\u53d8\u74e6\u7247\uff1a\u652f\u6301\u7a7a\u3001\u76ee\u6807\u3001\u5899\u4f53\u7c7b\u578b\u7684\u74e6\u7247<\/h2>\n<p>\u6b64\u65f6\uff0c\u6240\u6709\u74e6\u7247\u90fd\u4e3a\u7a7a\u3002\u4e00\u4e2a\u7528\u4f5c\u76ee\u6807\u7684\u74e6\u7247\uff0c\u4f46\u9664\u4e86\u6ca1\u6709\u53ef\u89c1\u7bad\u5934\u5916\uff0c\u5b83\u770b\u8d77\u6765\u4e0e\u6240\u6709\u5176\u4ed6\u56fe\u5757\u76f8\u540c\u3002\u6211\u4eec\u5c06\u901a\u8fc7\u5728\u5176\u4e2d\u653e\u7f6e\u4e00\u4e9b\u4e1c\u897f\u6765\u6539\u53d8\u5b83\u4eec\u3002<\/p>\n<h3>3.1.\u74e6\u7247\u5185\u5bb9\uff08Tile Content\uff09<\/h3>\n<p>tile\u5bf9\u8c61\u672c\u8eab\u53ea\u662f\u4e00\u79cd\u8ddf\u8e2atile\u4fe1\u606f\u7684\u65b9\u6cd5\u3002\u6211\u4eec\u4e0d\u76f4\u63a5\u6539\u53d8\u8fd9\u4e9b\u5bf9\u8c61\u3002\u76f8\u53cd\uff0c\u6211\u4eec\u5c06\u5f15\u5165\u5355\u72ec\u7684\u5185\u5bb9\u5e76\u5c06\u5176\u653e\u5728\u6e38\u620f\u677f\u4e0a\uff0c\u7528\u4e8e\u533a\u5206\u7a7a\u74e6\u7247\u548c\u76ee\u6807\u74e6\u7247\u3002<\/p>\n<p>a.\u6dfb\u52a0\u4e00\u4e2aTile\u5185\u5bb9\u7ec4\u4ef6\u811a\u672c\uff1a<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">using System.Collections;\r\nusing System.Collections.Generic;\r\nusing UnityEngine;\r\n\r\n\/\/ \u533a\u5206\u7a7a\u74e6\u7247\u548c\u76ee\u6807\u74e6\u7247\r\npublic enum GameTileContentType{\r\n  Empty, Destination\r\n}\r\n\r\n\/\/ \u74e6\u7247\u5185\u5bb9\u81ea\u5b9a\u4e49\u7ec4\u4ef6\r\npublic class GameTileContent : MonoBehaviour\r\n{\r\n    [SerializeField]\r\n    GameTileContentType type = default; \/\/\u5141\u8bb8\u901a\u8fc7\u5176\u68c0\u67e5\u5668\u8bbe\u7f6e\u5185\u5bb9\u7c7b\u578b\r\n\r\n    public GameTileContentType Type =&gt; type;\r\n}\r\n<\/code><\/pre>\n<p>b.\u521b\u5efa\u9884\u5236\u4ef6<\/p>\n<p>\u4e3a\u8fd9\u4e24\u79cd\u5185\u5bb9\u7c7b\u578b\uff08\u7a7a\u74e6\u7247\/\u76ee\u6807\u74e6\u7247\uff09\u5206\u522b\u521b\u5efa\u9884\u5236\u4ef6\uff0c\u6bcf\u79cd\u9884\u5236\u4ef6\u90fd\u5e26\u6709\u4e00\u4e2aGameTileContent\u7ec4\u4ef6\uff0c\u5e76\u4e14\u5176\u7c7b\u578b\u8bbe\u7f6e\u6b63\u786e\u3002\u8ba9\u6211\u4eec\u4f7f\u7528\u4e00\u4e2a\u84dd\u8272\u7684\u6241\u5e73\u7acb\u65b9\u4f53\u6765\u53ef\u89c6\u5316\u76ee\u6807\u74e6\u7247\u3002\u56e0\u4e3a\u5b83\u51e0\u4e4e\u662f\u5e73\u5766\u7684\uff0c\u6240\u4ee5\u4e0d\u9700\u8981\u78b0\u649e\u3002\u5bf9\u4e8e\u7a7a\u5185\u5bb9\u9884\u5236\u4ef6\uff0c\u8bf7\u4f7f\u7528\u7a7a\u7684\u6e38\u620f\u5bf9\u8c61\u3002<\/p>\n<p><a href=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/destination-prefab.png\"><img loading=\"lazy\" class=\"alignnone size-full wp-image-5066\" src=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/destination-prefab.png\" alt=\"\" width=\"640\" height=\"524\" srcset=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/destination-prefab.png 640w, https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/destination-prefab-300x246.png 300w, https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/destination-prefab-624x511.png 624w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/a> <a href=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/empty-prefab.png\"><img loading=\"lazy\" class=\"alignnone size-large wp-image-5067\" src=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/empty-prefab.png\" alt=\"\" width=\"625\" height=\"344\" srcset=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/empty-prefab.png 640w, https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/empty-prefab-300x165.png 300w, https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/empty-prefab-624x343.png 624w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><\/a><\/p>\n<p>\u6211\u4eec\u5c06\u4e3a\u7a7a\u74e6\u7247\u63d0\u4f9b\u5185\u5bb9\u5bf9\u8c61\uff0c\u56e0\u4e3a\u6240\u6709\u74e6\u7247\u5c06\u59cb\u7ec8\u5177\u6709\u5185\u5bb9\uff0c\u8fd9\u610f\u5473\u7740\u6211\u4eec\u4e0d\u5fc5\u68c0\u67e5\u7a7a\u5185\u5bb9\u5f15\u7528\u3002<\/p>\n<h3>3.2.\u5185\u5bb9\u5de5\u5382\uff08Content Factory\uff09<\/h3>\n<p>\u74e6\u7247\u5185\u5bb9\u9884\u5236\u4ef6\u5c06\u7528\u4e8e\u5173\u8054\u5230\u74e6\u7247\u5c5e\u6027\u4e0a\uff0c\u5e76\u5728\u6e32\u67d3\u65f6\u53e0\u52a0\u5230\u74e6\u7247\u4e0a\u65b9\u5c55\u793a\u3002\u6211\u4eec\u901a\u8fc7\u5185\u5bb9\u5de5\u5382\u6765\u5b9e\u73b0\u3002<\/p>\n<p>a.\u521b\u5efa\u5185\u5bb9\u5de5\u5382\u811a\u672cGameTileContentFactory.cs\u548c\u5bf9\u5e94\u9884\u5236\u4ef6GameTileContentFactory\uff0c\u5e76\u5728\u9884\u5236\u4ef6\u4e0a\u7ed1\u5b9aDestination\u9884\u5236\u4ef6\u3001Dmpty\u9884\u5236\u4ef6<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">using System.Collections;\r\nusing System.Collections.Generic;\r\nusing UnityEngine;\r\nusing UnityEngine.SceneManagement;\r\n\r\n\/\/ \u74e6\u7247\u5185\u5bb9\u5de5\u5382\r\n[CreateAssetMenu] \/\/\u6dfb\u52a0\u5230unity\u83dc\u5355\r\npublic class GameTileContentFactory : ScriptableObject\r\n{\r\n    \/\/ \u9884\u7f6e\u914d\u7f6e\u5b57\u6bb5\r\n    [SerializeField]\r\n    GameTileContent destinationPrefab = default;\r\n    [SerializeField]\r\n    GameTileContent emptyPrefab = default;\r\n\r\n    \/\/ \u5de5\u5382\u7684\u5185\u5bb9\u573a\u666f\r\n    Scene contentScene;\r\n\r\n    \/\/ \u79fb\u52a8\u5bf9\u8c61\u5230\u5de5\u5382\u7684\u5185\u5bb9\u573a\u666f\r\n    void MoveToFactoryScene(GameObject o){\r\n      if(!contentScene.isLoaded){\r\n        if(Application.isEditor){ \/\/\u5728\u7f16\u8f91\u5668\u4e2d\u65f6\uff0c\u68c0\u67e5\u573a\u666f\u662f\u5426\u5b58\u5728\r\n          contentScene = SceneManager.GetSceneByName(name);\r\n          if(!contentScene.isLoaded){\r\n            contentScene = SceneManager.CreateScene(name);\r\n          }\r\n        }else{\r\n          contentScene = SceneManager.CreateScene(name);\r\n        }\r\n      }\r\n      SceneManager.MoveGameObjectToScene(o, contentScene);\r\n    }\r\n\r\n    \/\/ \u751f\u4ea7\u74e6\u7247\u5185\u5bb9\r\n    GameTileContent Get(GameTileContent prefab){\r\n      GameTileContent o = Instantiate(prefab);\r\n      o.OriginFactory = this;\r\n      MoveToFactoryScene(o.gameObject);\r\n      return o;\r\n    }\r\n\r\n    \/\/ \u751f\u6210\u4e0d\u540c\u7c7b\u578b\u74e6\u7247\r\n    public GameTileContent Get(GameTileContentType type){\r\n      switch(type){\r\n        case GameTileContentType.Destination:\r\n          return Get(destinationPrefab);\r\n        case GameTileContentType.Empty:\r\n          return Get(emptyPrefab);\r\n      }\r\n      Debug.Assert(false, \"Unsupported type: \" + type);\r\n      return null;\r\n    }\r\n\r\n    \/\/\u56de\u6536\r\n    public void Reclaim(GameTileContent content){\r\n      Debug.Assert(content.OriginFactory == this, \"Wrong factory reclaimed!\");\r\n      Destroy(content.gameObject);\r\n    }\r\n}\r\n<\/code><\/pre>\n<p><a href=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/content-factory.png\"><img loading=\"lazy\" class=\"alignnone size-full wp-image-5077\" src=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/content-factory.png\" alt=\"\" width=\"640\" height=\"212\" srcset=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/content-factory.png 640w, https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/content-factory-300x99.png 300w, https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/content-factory-624x207.png 624w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/a><\/p>\n<p>b.\u74e6\u7247\u5185\u5bb9\u7ec4\u5efa\u589e\u52a0\u5de5\u5382\u7684\u53cd\u5411\u8ddf\u8e2a<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">using System.Collections;\r\nusing System.Collections.Generic;\r\nusing UnityEngine;\r\n\r\n\/\/ \u533a\u5206\u7a7a\u74e6\u7247\u548c\u76ee\u6807\u74e6\u7247\r\npublic enum GameTileContentType{\r\n  Empty, Destination\r\n}\r\n\r\n\/\/ \u74e6\u7247\u5185\u5bb9\u81ea\u5b9a\u4e49\u7ec4\u4ef6\r\npublic class GameTileContent : MonoBehaviour\r\n{\r\n    \/\/ \u5185\u5bb9\u7c7b\u578b\r\n    [SerializeField]  \/\/\u901a\u8fc7\u5176\u68c0\u67e5\u5668\u8bbe\u7f6e\u5185\u5bb9\u7c7b\u578b\r\n    GameTileContentType type = default;\r\n\r\n    public GameTileContentType Type =&gt; type;\r\n\r\n    \/\/ \u8ddf\u8e2a\u539f\u59cb\u5de5\u5382\r\n    GameTileContentFactory originFactory;\r\n    public GameTileContentFactory OriginFactory{\r\n      get =&gt; originFactory;\r\n      set {\r\n        Debug.Assert(originFactory == null, \"Redefined origin factory!\");\r\n        originFactory = value;\r\n      }\r\n    }\r\n    \/\/ \u901a\u8fc7\u8ddf\u8e2a\u539f\u59cb\u5de5\u5382\u56de\u6536\r\n    public void Recycle(){\r\n      originFactory.Reclaim(this);\r\n    }\r\n}\r\n<\/code><\/pre>\n<h3>3.3.\u6539\u53d8\u5185\u5bb9\uff08Changing Content\uff09<\/h3>\n<p>\u74e6\u7247\u5bf9\u8c61\u589e\u52a0GameTileContent\u7684\u5173\u8054\u5c5e\u6027\uff0c\u4e3b\u8981\u7528\u4e8e\u628a\u5185\u5bb9\u79fb\u52a8\u5230\u74e6\u7247\u4f4d\u7f6e<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">\/\/ \u74e6\u7247\u5bf9\u8c61\r\npublic class GameTile : MonoBehaviour\r\n{\r\n    ...\r\n\r\n    \/\/ \u5173\u8054\u5230\u74e6\u7247\u7684\u5185\u5bb9\uff08\u8986\u76d6\u5230\u74e6\u7247\u4e0a\u65b9\uff09\r\n    GameTileContent content;\r\n    public GameTileContent Content{\r\n      get =&gt; content;\r\n      set{\r\n        Debug.Assert(value !=null, \"Null assingned to content!\");\r\n        if(content !=null){ \/\/\u56de\u6536\u5b83\u4ee5\u524d\u7684\u5185\u5bb9\r\n          content.Recycle();\r\n        }\r\n        content = value;  \/\/\u5b9a\u4f4d\u65b0\u5185\u5bb9\r\n        content.transform.localPosition = transform.localPosition;  \/\/\u628a\u5185\u5bb9\u79fb\u52a8\u5230\u74e6\u7247\u4f4d\u7f6e\r\n      }\r\n    }\r\n\r\n    ...\r\n}\r\n<\/code><\/pre>\n<h3>3.4.\u63a5\u89e6\u74e6\u7247\uff08Touching a Tile\uff09<\/h3>\n<p>a.\u6e38\u620f\u9762\u677f\u589e\u52a0\u74e6\u7247\u5185\u5bb9\u7684\u521d\u59cb\u5316\uff0c\u4ee5\u53ca\u76ee\u7684\u5730&lt;-&gt;\u975e\u76ee\u7684\u5730\u7684\u5bf9\u8c03\uff1b\u6539\u9020\u641c\u7d22\u652f\u6301\u591a\u76ee\u7684\u5730<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">using System.Collections;\r\nusing System.Collections.Generic;\r\nusing UnityEngine;\r\n\r\n\/\/ \u6e38\u620f\u9762\u677f\u5bf9\u8c61\r\npublic class GameBoard : MonoBehaviour\r\n{\r\n    ...\r\n\r\n    GameTileContentFactory contentFactory;   \/\/\u5185\u5bb9\u5de5\u5382\r\n\r\n    \/\/ \u521d\u59cb\u5316\u65b9\u6cd5\r\n    public void Initialize(Vector2Int size, GameTileContentFactory contentFactory){\r\n      this.size = size;\r\n      this.contentFactory = contentFactory;\r\n      ...\r\n          tile.Content = contentFactory.Get(GameTileContentType.Empty);\r\n      ...\r\n\r\n      \/\/ \u641c\u7d22\u8def\u5f84\r\n      ToggleDestination(tiles[tiles.Length \/ 2]); \/\/\u5f53\u4f7f\u7528\u5947\u6570\u677f\u5c3a\u5bf8\u65f6\uff0ctiles.Length\/2\u662f\u4e2d\u95f4\u7684\u74e6\u7247\r\n    }\r\n\r\n    \/\/ \u5bf9\u8c03\u74e6\u7247\u7684\u76ee\u7684\u5730&lt;-&gt;\u975e\u76ee\u7684\u5730\u5c5e\u6027\r\n    public void ToggleDestination(GameTile tile){\r\n      if(tile == null){\r\n        return;\r\n      }\r\n      if(tile.Content.Type == GameTileContentType.Destination){\r\n        tile.Content = contentFactory.Get(GameTileContentType.Empty);\r\n        if(!FindPaths()){\r\n          tile.Content = contentFactory.Get(GameTileContentType.Destination);\r\n          FindPaths();\r\n        }\r\n      }else{\r\n        tile.Content = contentFactory.Get(GameTileContentType.Destination);\r\n        FindPaths();\r\n      }\r\n    }\r\n\r\n\r\n    \/\/ \u641c\u7d22\u884c\u8fdb\u8def\u5f84\r\n    bool FindPaths(){\r\n      \/\/ \u6e05\u9664\u6240\u6709\u74e6\u7247\u7684\u8def\u5f84\r\n      foreach(GameTile tile in tiles){\r\n        if(tile.Content.Type == GameTileContentType.Destination){ \/\/\u76ee\u7684\u5730\r\n          Debug.Log(\"BecameDestination Idx: \" + tile.Idx);\r\n          tile.BecameDestination(); \/\/ \u8bbe\u7f6e\u4e3a\u76ee\u7684\u5730\r\n          searchFrontier.Enqueue(tile); \/\/ \u5c06\u76ee\u7684\u5730\u6dfb\u52a0\u5230\u8fb9\u754c\u961f\u5217\r\n        }else{\r\n          tile.ClearPath();\r\n        }\r\n      }\r\n      if(searchFrontier.Count == 0){\r\n        return false;\r\n      }\r\n\r\n      ...\r\n    }\r\n\r\n    ...\r\n}\r\n<\/code><\/pre>\n<p>b.Game\u6dfb\u52a0\u9f20\u6807\u70b9\u51fb\u5207\u6362\u76ee\u7684\u5730\u5e76\u91cd\u65b0\u641c\u7d20\u8def\u5f84\u4e8b\u4ef6\uff1b\u5728\u5c5e\u6027\u4e2d\u7ed1\u5b9aTileContentFactory\u9884\u5236<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">using System.Collections;\r\nusing System.Collections.Generic;\r\nusing UnityEngine;\r\n\r\n\/\/ \u6e38\u620f\u5bf9\u8c61\r\npublic class Game : MonoBehaviour\r\n{\r\n    ...\r\n\r\n    [SerializeField]\r\n    GameTileContentFactory tileContentFactory = default;\r\n\r\n    Ray TouchRay =&gt; Camera.main.ScreenPointToRay(Input.mousePosition);  \/\/\u4e3b\u76f8\u673a\u5355\u51fb\u8f6c\u6362\u4e3a\u5c04\u7ebf\r\n\r\n    \/\/ \u5524\u9192\u4e8b\u4ef6\r\n    void Awake(){\r\n      board.Initialize(boardSize, tileContentFactory);  \/\/\u521d\u59cb\u531611x11\u7684\u6e38\u620f\u9762\u677f\r\n    }\r\n    ...\r\n    void Update(){\r\n      if(Input.GetMouseButtonDown(0)){  \/\/\u9f20\u6807\u4e8b\u4ef6\r\n        board.ToggleDestination(board.GetTile(TouchRay));\r\n      }\r\n    }\r\n}\r\n<\/code><\/pre>\n<p><a href=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/game-with-factory.png\"><img loading=\"lazy\" class=\"alignnone size-full wp-image-5079\" src=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/game-with-factory.png\" alt=\"\" width=\"640\" height=\"220\" srcset=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/game-with-factory.png 640w, https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/game-with-factory-300x103.png 300w, https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/game-with-factory-624x215.png 624w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/a><\/p>\n<p>c.\u8fd0\u884c\u5e76\u70b9\u51fb\u751f\u6210\u591a\u4e2a\u76ee\u7684\u5730\uff0c\u53ef\u4ee5\u770b\u5230\u4f1a\u81ea\u52a8\u5411\u591a\u4e2a\u76ee\u7684\u5730\u89c4\u5212\u8def\u5f84<\/p>\n<p><a href=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/destinations.png\"><img loading=\"lazy\" class=\"alignnone size-full wp-image-5080\" src=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/destinations.png\" alt=\"\" width=\"460\" height=\"460\" srcset=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/destinations.png 460w, https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/destinations-150x150.png 150w, https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/destinations-300x300.png 300w\" sizes=\"(max-width: 460px) 100vw, 460px\" \/><\/a><\/p>\n<h2>4.\u5899\uff08Walls\uff09\uff1a\u5728\u8fd0\u884c\u65f6\u7f16\u8f91\u74e6\u7247\u5185\u5bb9<\/h2>\n<p>\u5854\u9632\u6e38\u620f\u7684\u76ee\u7684\u662f\u786e\u4fdd\u654c\u4eba\u4e0d\u4f1a\u5230\u8fbe\u76ee\u7684\u5730\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u4e24\u79cd\u65b9\u5f0f\u5b8c\u6210\u3002\u9996\u5148\uff0c\u901a\u8fc7\u6740\u6b7b\u5b83\u4eec\uff0c\u5176\u6b21\u662f\u901a\u8fc7\u653e\u6162\u5b83\u4eec\u7684\u901f\u5ea6\u6765\u4f7f\u4f60\u6709\u66f4\u591a\u65f6\u95f4\u6740\u6b7b\u5b83\u4eec\u3002\u5728\u4e00\u5757\u74e6\u7247\u6e38\u620f\u9762\u677f\u4e0a\uff0c\u7ed9\u81ea\u5df1\u66f4\u591a\u65f6\u95f4\u7684\u4e3b\u8981\u65b9\u6cd5\u662f\u589e\u52a0\u654c\u4eba\u5fc5\u987b\u79fb\u52a8\u7684\u8ddd\u79bb\u3002\u8fd9\u662f\u901a\u8fc7\u5728\u677f\u4e0a\u653e\u7f6e\u969c\u788d\u7269\u6765\u5b8c\u6210\u7684\uff0c\u969c\u788d\u7269\u901a\u5e38\u662f\u53ef\u4ee5\u6740\u6b7b\u654c\u4eba\u7684\u5854\uff0c\u4f46\u662f\u5728\u672c\u6559\u7a0b\u4e2d\uff0c\u6211\u4eec\u5c06\u9650\u5236\u4e3a\u5899\u4f53\u3002<\/p>\n<h3>4.1.\u5185\u5bb9\uff08Content\uff09<\/h3>\n<p>\u5899\u662f\u53e6\u4e00\u79cd\u5185\u5bb9\u3002<\/p>\n<p>a.\u5c06Wall\u6dfb\u52a0\u5230GameTileContentType\u4e2d<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">\/\/ \u74e6\u7247\u5185\u5bb9\u7c7b\u578b\r\npublic enum GameTileContentType{\r\n  Empty,  \/\/\u8def\r\n  Destination,  \/\/\u76ee\u7684\u5730\r\n  Wall  \/\/\u5899\r\n}<\/code><\/pre>\n<p>b.\u521b\u5efa\u5899\u9884\u5236\u4ef6<\/p>\n<p>\u8fd9\u6b21\uff0c\u521b\u5efa\u4e00\u4e2a\u74e6\u7247\u5185\u5bb9\u6e38\u620f\u5bf9\u8c61\uff0c\u5e76\u4e3a\u5176\u6307\u5b9a\u4e00\u4e2a\u7acb\u65b9\u4f53\u5b50\u9879\uff0c\u4f7f\u5176\u5b9a\u4f4d\u5728\u68cb\u76d8\u9876\u90e8\u5e76\u586b\u5145\u6574\u4e2a\u74e6\u7247\u3002\u5c06\u5b83\u653e\u9ad8\u534a\u4e2a\u5355\u4f4d\u5e76\u4fdd\u6301\u5176\u78b0\u649e\u4f53\uff0c\u56e0\u4e3a\u5899\u58c1\u53ef\u4ee5\u4ece\u89c6\u89c9\u4e0a\u906e\u6321\u4f4f\u540e\u9762\u7684\u74e6\u7247\u3002\u56e0\u6b64\uff0c\u5f53\u64ad\u653e\u5668\u63a5\u89e6\u5899\u58c1\u65f6\uff0c\u5c06\u5f71\u54cd\u76f8\u5e94\u7684\u74e6\u7247\u3002<\/p>\n<p><a href=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/wall-root.png\"><img loading=\"lazy\" class=\"alignnone size-full wp-image-5084\" src=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/wall-root.png\" alt=\"\" width=\"640\" height=\"352\" srcset=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/wall-root.png 640w, https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/wall-root-300x165.png 300w, https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/wall-root-624x343.png 624w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/a> <a href=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/wall-prefab-hierarchy.png\"><img loading=\"lazy\" class=\"alignnone size-large wp-image-5085\" src=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/wall-prefab-hierarchy.png\" alt=\"\" width=\"450\" height=\"120\" srcset=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/wall-prefab-hierarchy.png 450w, https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/wall-prefab-hierarchy-300x80.png 300w\" sizes=\"(max-width: 450px) 100vw, 450px\" \/><\/a> <a href=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/wall-cube.png\"><img loading=\"lazy\" class=\"alignnone size-large wp-image-5086\" src=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/wall-cube.png\" alt=\"\" width=\"625\" height=\"441\" srcset=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/wall-cube.png 640w, https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/wall-cube-300x212.png 300w, https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/wall-cube-624x441.png 624w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><\/a><\/p>\n<p>c.\u4f7f\u7528\u4ee3\u7801\u548c\u68c0\u67e5\u5668\u5c06\u5899\u58c1\u9884\u5236\u4ef6\u6dfb\u52a0\u5230\u5de5\u5382<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">\/\/ \u74e6\u7247\u5185\u5bb9\u5de5\u5382\r\npublic class GameTileContentFactory : ScriptableObject\r\n{\r\n    \/\/ \u9884\u7f6e\u914d\u7f6e\u5b57\u6bb5\r\n    ...\r\n    [SerializeField]\r\n    GameTileContent wallPrefab = default;  \/\/ \u5899\u9884\u5236\u4ef6\r\n\r\n    ...\r\n\r\n    \/\/ \u751f\u6210\u4e0d\u540c\u7c7b\u578b\u74e6\u7247\r\n    public GameTileContent Get(GameTileContentType type){\r\n      switch(type){\r\n        case GameTileContentType.Destination:\r\n          return Get(destinationPrefab);\r\n        case GameTileContentType.Empty:\r\n          return Get(emptyPrefab);\r\n        case GameTileContentType.Wall:\r\n          return Get(wallPrefab);\r\n      }\r\n      Debug.Assert(false, \"Unsupported type: \" + type);\r\n      return null;\r\n    }\r\n\r\n    ...\r\n}\r\n<\/code><\/pre>\n<p><a href=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/factory.png\"><img loading=\"lazy\" class=\"alignnone size-full wp-image-5087\" src=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/factory.png\" alt=\"\" width=\"640\" height=\"160\" srcset=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/factory.png 640w, https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/factory-300x75.png 300w, https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/factory-624x156.png 624w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/a><\/p>\n<h3>4.2.\u5f00\u5173\u5899\u58c1\uff08Toggling Walls\uff09<\/h3>\n<p>a.\u5c31\u50cf\u4e3a\u76ee\u7684\u5730\u4e00\u6837\uff0c\u5411GameBoard\u6dfb\u52a0\u5899\u7684\u5207\u6362\u65b9\u6cd5<\/p>\n<p>\u6211\u4eec\u4ec5\u652f\u6301\u5728\u7a7a\u74e6\u7247\u548c\u5899\u7816\u4e4b\u95f4\u5207\u6362\uff0c\u800c\u4e0d\u5141\u8bb8\u5899\u76f4\u63a5\u66ff\u6362\u76ee\u7684\u5730\u3002\u56e0\u6b64\uff0c\u4ec5\u5f53\u74e6\u7247\u4e3a\u7a7a\u65f6\u624d\u8f6c\u6362\u4e3a\u5899\u3002\u53e6\u5916\uff0c\u5899\u58c1\u4f1a\u5e94\u8be5\u963b\u788d\u5bfb\u8def\u3002\u4f46\u662f\u6bcf\u4e2a\u74e6\u7247\u90fd\u9700\u8981\u6709\u4e00\u6761\u901a\u5f80\u76ee\u7684\u5730\u7684\u8def\u5f84\uff0c\u5426\u5219\u654c\u4eba\u53ef\u80fd\u4f1a\u5361\u4f4f\u3002\u518d\u6b21\uff0c\u6211\u4eec\u5c06\u4f7f\u7528FindPaths\u5bf9\u6b64\u8fdb\u884c\u68c0\u67e5\uff0c\u5982\u679c\u521b\u5efa\u4e86\u65e0\u6548\u7684\u9762\u677f\u72b6\u6001\uff0c\u5219\u64a4\u6d88\u66f4\u6539\u3002<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">\/\/ \u6e38\u620f\u9762\u677f\u5bf9\u8c61\r\npublic class GameBoard : MonoBehaviour\r\n{\r\n    ...\r\n\r\n    \/\/ \u5bf9\u8c03\u74e6\u7247\u7684\u5899&lt;-&gt;\u975e\u5899\u5c5e\u6027\r\n    public void ToggleWall(GameTile tile){\r\n      if(tile == null){\r\n        return;\r\n      }\r\n      if(tile.Content.Type == GameTileContentType.Wall){\r\n        tile.Content = contentFactory.Get(GameTileContentType.Empty);\r\n        FindPaths();\r\n      }else if(tile.Content.Type == GameTileContentType.Empty){ \/\/\u4ec5\u652f\u6301\u5728\u7a7a\u74e6\u7247\u548c\u5899\u7816\u4e4b\u95f4\u5207\u6362\r\n        tile.Content = contentFactory.Get(GameTileContentType.Wall);\r\n        FindPaths();\r\n      }\r\n    }\r\n\r\n    ...\r\n}\r\n<\/code><code class=\"\"><\/code><\/pre>\n<p>b.\u4fee\u6539Game\u7684\u70b9\u51fb\u4e8b\u4ef6\u89e6\u53d1<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">\/\/ \u6e38\u620f\u5bf9\u8c61\r\npublic class Game : MonoBehaviour\r\n{\r\n    ...\r\n\r\n    void Update(){\r\n      if(Input.GetMouseButtonDown(0)){  \/\/\u9f20\u6807\u5de6\u952e\u4e8b\u4ef6\r\n        board.ToggleWall(board.GetTile(TouchRay));  \/\/\u5207\u6362\u7a7a\u74e6\u7247\u548c\u5899\u7816\r\n      }else if(Input.GetMouseButtonDown(1)){  \/\/\u9f20\u6807\u53f3\u952e\u4e8b\u4ef6\r\n        board.ToggleDestination(board.GetTile(TouchRay));  \/\/\u5207\u6362\u7a7a\u74e6\u7247\u548c\u76ee\u7684\u5730\r\n      }\r\n    }\r\n}\r\n<\/code><\/pre>\n<p>c.\u8fd0\u884c\u6d4b\u8bd5\u6dfb\u52a0\u5899<\/p>\n<p><a href=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/walls.png\"><img loading=\"lazy\" class=\"alignnone size-full wp-image-5088\" src=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/walls.png\" alt=\"\" width=\"460\" height=\"460\" srcset=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/walls.png 460w, https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/walls-150x150.png 150w, https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/walls-300x300.png 300w\" sizes=\"(max-width: 460px) 100vw, 460px\" \/><\/a><\/p>\n<h3>4.3.\u963b\u65ad\u5bfb\u8def\uff08Blocking Findpathing\uff09<\/h3>\n<p>\u8981\u8ba9\u5899\u58c1\u963b\u788d\u5bfb\u8def\uff0c\u6211\u4eec\u6240\u8981\u505a\u7684\u5c31\u662f\u4e0d\u8981\u5411searchFrontier\u6dfb\u52a0\u5e26\u6709Wall\u7684\u74e6\u7247\u3002\u53ef\u4ee5\u901a\u8fc7GameTile.GrowPathTo\u4e0d\u8fd4\u56de\u5e26\u6709\u5899\u7684\u74e6\u7247\u6765\u5b9e\u73b0\u3002\u4f46\u662f\u8def\u5f84\u4ecd\u7136\u5e94\u8be5\u957f\u5230\u5899\u91cc\u9762\u53bb\uff0c\u4f46\u4e0d\u4f1a\u7ee7\u7eed\u5ef6\u4f38\u3002<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">\/\/ \u74e6\u7247\u5bf9\u8c61\r\npublic class GameTile : MonoBehaviour\r\n{\r\n    ...\r\n\r\n    \/\/ \u53cd\u5411\u8bbe\u7f6e\u8def\u5f84\u5173\u7cfb(\u4ece\u76ee\u7684\u5730\u74e6\u7247\u5f00\u59cb)\r\n    GameTile GrowPathTo(GameTile neighbor){\r\n      ...\r\n      return neighbor.Content.Type != GameTileContentType.Wall ? neighbor : null; \/\/\u751f\u957f\u8def\u5f84\u540e\uff0c\u5982\u679c\u90bb\u5c45\u662f\u5899\uff0c\u5219\u8ba4\u4e3a\u8def\u5f84\u963b\u65ad\uff0c\u4e0d\u518d\u8fd4\u56de\u6b64\u74e6\u7247\u5230\u8def\u5f84\u641c\u7d22\u961f\u5217\r\n    }\r\n    ...\r\n}\r\n<\/code><\/pre>\n<p><a href=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/walls-valid-paths.png\"><img loading=\"lazy\" class=\"alignnone size-full wp-image-5089\" src=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/walls-valid-paths.png\" alt=\"\" width=\"460\" height=\"460\" srcset=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/walls-valid-paths.png 460w, https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/walls-valid-paths-150x150.png 150w, https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/walls-valid-paths-300x300.png 300w\" sizes=\"(max-width: 460px) 100vw, 460px\" \/><\/a><\/p>\n<h3>4.4.\u9690\u85cf\u8def\u5f84\uff08Hiding the Paths\uff09<\/h3>\n<p>\u8def\u5f84\u53ef\u89c6\u5316\u4f7f\u6211\u4eec\u80fd\u591f\u67e5\u770b\u5bfb\u8def\u7684\u5de5\u4f5c\u539f\u7406\u5e76\u9a8c\u8bc1\u5176\u786e\u5b9e\u6b63\u786e\uff0c\u4f46\u5e76\u4e0d\u6253\u7b97\u5c06\u5176\u663e\u793a\u7ed9\u73a9\u5bb6\uff0c\u81f3\u5c11\u5e76\u975e\u603b\u662f\u5982\u6b64\u3002\u56e0\u6b64\uff0c\u8ba9\u6211\u4eec\u53ef\u4ee5\u9690\u85cf\u7bad\u5934\u3002<\/p>\n<p>a.\u5728GameTile\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u516c\u5171\u7684HidePath\u65b9\u6cd5<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">\/\/ \u74e6\u7247\u5bf9\u8c61\r\npublic class GameTile : MonoBehaviour\r\n{\r\n    ...\r\n    \/\/ \u9690\u85cf\u8def\u5f84\u7bad\u5934\r\n    public void HidePath(){\r\n      arrow.gameObject.SetActive(false);\r\n    }\r\n}\r\n<\/code><\/pre>\n<p>b.\u4e3aGameBoard\u6dfb\u52a0\u4e00\u4e2a\u8def\u5f84\u7bad\u5934\u5c55\u793a\u5f00\u5173\u5b57\u6bb5\uff08\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u8bbe\u7f6e\u4e3afalse\uff09\uff0c\u7528\u4e8e\u63a7\u5236\u6240\u6709\u7bad\u5934\u7684\u5c55\u793a\u548c\u9690\u85cf<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">\/\/ \u6e38\u620f\u9762\u677f\u5bf9\u8c61\r\npublic class GameBoard : MonoBehaviour\r\n{\r\n    ...\r\n\r\n    bool isShowPaths = true; \/\/ \u8def\u5f84\u7bad\u5934\u5c55\u793a\u5f00\u5173\r\n    public bool IsShowPaths{\r\n      get =&gt; isShowPaths;\r\n      set {\r\n        isShowPaths = value;\r\n        if(isShowPaths){\r\n          foreach(GameTile tile in tiles){\r\n            tile.ShowPath();\r\n          }\r\n        }else{\r\n          foreach(GameTile tile in tiles){\r\n            tile.HidePath();\r\n          }\r\n        }\r\n      }\r\n    }\r\n\r\n    ...\r\n    \/\/ \u641c\u7d22\u884c\u8fdb\u8def\u5f84\r\n    bool FindPaths(){\r\n      ...\r\n      \/\/ \u6839\u636e\u8def\u5f84\u8c03\u6574\u74e6\u7247\u7bad\u5934\u65b9\u5411\r\n      if(IsShowPaths){\r\n        foreach(GameTile tile in tiles){\r\n          tile.ShowPath();\r\n        }\r\n      }\r\n      ...\r\n    }\r\n    ...\r\n}\r\n<\/code><\/pre>\n<p>c.\u4e3aGame\u6dfb\u52a0\u952e\u76d8\u4e8b\u4ef6\uff0c\u63a7\u5236\u8def\u5f84\u7684\u663e\u793a\u548c\u9690\u85cf<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">\/\/ \u6e38\u620f\u5bf9\u8c61\r\npublic class Game : MonoBehaviour\r\n{\r\n    ...\r\n    void Update(){\r\n       if(Input.GetKeyDown(KeyCode.V)){  \/\/\u952e\u76d8V\u952e\r\n        board.IsShowPaths = !board.IsShowPaths; \/\/ \u5207\u6362\u8def\u5f84\u7bad\u5934\u663e\u793a\u72b6\u6001\r\n      }\r\n    }\r\n}\r\n<\/code><\/pre>\n<p>d.\u8fd0\u884c\u5e76\u901a\u8fc7\u952e\u76d8V\u952e\u9690\u85cf\u8def\u5f84<\/p>\n<p><a href=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/without-arrows.png\"><img loading=\"lazy\" class=\"alignnone size-full wp-image-5090\" src=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/without-arrows.png\" alt=\"\" width=\"460\" height=\"460\" srcset=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/without-arrows.png 460w, https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/without-arrows-150x150.png 150w, https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/without-arrows-300x300.png 300w\" sizes=\"(max-width: 460px) 100vw, 460px\" \/><\/a><\/p>\n<h3>4.5.\u5c55\u793a\u7f51\u683c\uff08Showing the Grid\uff09<\/h3>\n<p>\u9690\u85cf\u7bad\u5934\u65f6\uff0c\u5f88\u96be\u770b\u5230\u6bcf\u4e2a\u74e6\u7247\u7684\u4f4d\u7f6e\u3002\u8ba9\u6211\u4eec\u6dfb\u52a0\u7f51\u683c\u7ebf\u4f7f\u4e4b\u66f4\u5bb9\u6613\u3002\u8fd9\u662f\u5e26\u6709\u8fb9\u6846\u7684\u7f51\u683c\u7eb9\u7406\uff0c\u53ef\u7528\u4e8e\u52fe\u52d2\u51fa\u5355\u4e2a\u74e6\u7247\u3002<\/p>\n<p>a.\u4e0b\u8f7d\u7f51\u683c\u7eb9\u7406\u5e76\u653e\u5230\u9879\u76ee\u7684Assets\/Resources\u76ee\u5f55<\/p>\n<p><a href=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/grid.png\"><img loading=\"lazy\" class=\"alignnone size-full wp-image-5092\" src=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/grid.png\" alt=\"\" width=\"256\" height=\"256\" srcset=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/grid.png 256w, https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/grid-150x150.png 150w\" sizes=\"(max-width: 256px) 100vw, 256px\" \/><\/a><\/p>\n<p>b.\u4e3aGameBoard\u6dfb\u52a0\u7f51\u683c\u7eb9\u7406\u5c5e\u6027\uff0c\u5e76\u8bbe\u5b9a\u4e3a\u4e0a\u65b9\u7684grid\u7eb9\u7406\u8d44\u6e90<\/p>\n<p>\u6211\u4eec\u4e0d\u4f1a\u5c06\u6b64\u7eb9\u7406\u5355\u72ec\u6dfb\u52a0\u5230\u6bcf\u4e2a\u56fe\u5757\uff0c\u800c\u662f\u5c06\u5176\u5e94\u7528\u4e8e\u5730\u9762\u3002\u4f46\u662f\u6211\u4eec\u5c06\u4f7f\u7f51\u683c\u6210\u4e3a\u53ef\u9009\u7684\uff0c\u5c31\u50cf\u8def\u5f84\u53ef\u89c6\u5316\u4e00\u6837\u3002\u56e0\u6b64\uff0c\u5c06\u4e00\u4e2aTexture2D\u914d\u7f6e\u5b57\u6bb5\u6dfb\u52a0\u5230GameBoard\u5e76\u5c06\u5176\u8bbe\u7f6e\u4e3a\u7f51\u683c\u7eb9\u7406\u3002<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">    [SerializeField]\r\n    Texture2D gridTexture = default; \/\/\u7f51\u683c\u7eb9\u7406<\/code><\/pre>\n<p><a href=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/board-with-grid-texture.png\"><img loading=\"lazy\" class=\"alignnone size-full wp-image-5093\" src=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/board-with-grid-texture.png\" alt=\"\" width=\"640\" height=\"188\" srcset=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/board-with-grid-texture.png 640w, https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/board-with-grid-texture-300x88.png 300w, https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/board-with-grid-texture-624x183.png 624w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/a><\/p>\n<p>c.\u4e3aGameBoard\u6dfb\u52a0\u7f51\u683c\u5c55\u793a\u5f00\u5173\uff0c\u5e76\u5728\u5f00\u5173\u6253\u5f00\u65f6\u8bbe\u7f6e\u4e3a\u7f51\u683c\u7eb9\u7406<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">   [SerializeField]\r\n    Texture2D gridTexture = default; \/\/\u7f51\u683c\u7eb9\u7406\u5c5e\u6027\r\n\r\n    bool isShowGrid; \/\/ \u7f51\u683c\u5c55\u793a\u5f00\u5173\r\n    public bool IsShowGrid{\r\n      get =&gt; isShowGrid;\r\n      set {\r\n        isShowGrid = value;\r\n        Material m = ground.GetComponent&lt;MeshRenderer&gt;().material;  \/\/\u6750\u8d28\u5bf9\u8c61\r\n        if(isShowGrid){\r\n          m.mainTexture = gridTexture;  \/\/\u8bbe\u7f6e\u7f51\u683c\u7eb9\u7406\r\n          m.SetTextureScale(\"_MainTex\", size);  \/\/\u7f29\u653e\u6750\u8d28\u7684\u4e3b\u8981\u7eb9\u7406\uff0c\u4f7f\u5176\u4e0e\u9762\u76d8\u5c3a\u5bf8\u5927\u5c0f\u5339\u914d\uff0c\u5f62\u6210\u74e6\u7247\u7f51\u683c\r\n        }else{\r\n          m.mainTexture = null;\r\n        }\r\n      }\r\n    }<\/code><\/pre>\n<p>d.\u4e3aGame\u6dfb\u52a0\u5feb\u6377\u952e\uff0c\u7528\u4e8e\u63a7\u5236\u7f51\u683c\u7684\u5c55\u793a\u548c\u9690\u85cf<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">\/\/ \u6e38\u620f\u5bf9\u8c61\r\npublic class Game : MonoBehaviour\r\n{\r\n    ...\r\n    \/\/ \u5524\u9192\u4e8b\u4ef6\r\n    void Awake(){\r\n      ...\r\n      board.IsShowPaths = true; \/\/\u5c55\u793a\u8def\u5f84\r\n      board.IsShowGrid = true;  \/\/\u5c55\u793a\u7f51\u683c\r\n    }\r\n    ...\r\n    void Update(){\r\n       ...\r\n       if(Input.GetKeyDown(KeyCode.G)){  \/\/\u952e\u76d8G\u952e\r\n        board.IsShowGrid = !board.IsShowGrid; \/\/ \u5207\u6362\u7f51\u683c\u663e\u793a\u72b6\u6001\r\n      }\r\n    }\r\n}\r\n<\/code><\/pre>\n<p>e.\u8fd0\u884c\u5e76\u901a\u8fc7G\/V\u5feb\u6377\u952e\u63a7\u5236\u7f51\u683c\u548c\u7bad\u5934\u7684\u5c55\u793a<\/p>\n<p><a href=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/scaled-grid.png\"><img loading=\"lazy\" class=\"alignnone size-full wp-image-5094\" src=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/scaled-grid.png\" alt=\"\" width=\"460\" height=\"460\" srcset=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/scaled-grid.png 460w, https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/scaled-grid-150x150.png 150w, https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/scaled-grid-300x300.png 300w\" sizes=\"(max-width: 460px) 100vw, 460px\" \/><\/a> <a href=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/scaled-grid-paths.png\"><img loading=\"lazy\" class=\"alignnone size-full wp-image-5095\" src=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/scaled-grid-paths.png\" alt=\"\" width=\"460\" height=\"460\" srcset=\"https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/scaled-grid-paths.png 460w, https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/scaled-grid-paths-150x150.png 150w, https:\/\/yanjingang.com\/blog\/wp-content\/uploads\/2021\/08\/scaled-grid-paths-300x300.png 300w\" sizes=\"(max-width: 460px) 100vw, 460px\" \/><\/a><\/p>\n<p>\u622a\u6b62\u5230\u73b0\u5728\uff0c\u6211\u4eec\u505a\u597d\u4e86\u6e38\u620f\u5730\u56fe\u3001\u76ee\u7684\u5730\u3001\u5899\u3001\u8def\u5f84\u89c4\u5212\u7684\u51c6\u5907\u5de5\u4f5c\u3002\u4e0b\u4e00\u8282\uff0c\u6211\u4eec\u5c06\u5b66\u4e60\u5982\u4f55\u5728\u6e38\u620f\u9762\u677f\u4e2d\u6dfb\u52a0\u654c\u4eba\uff0c\u5e76\u8ba9\u654c\u4eba\u6cbf\u7740\u89c4\u5212\u8def\u5f84\u524d\u5f80\u76ee\u7684\u5730\u3002\u597d\u671f\u5f85\u54c7\uff5e\uff01<\/p>\n<p>&nbsp;<\/p>\n<p>yan 21.8.8<\/p>\n<p>\u53c2\u8003\uff1a<\/p>\n<p><a href=\"https:\/\/catlikecoding.com\/unity\/tutorials\/object-management\/\" target=\"_blank\" rel=\"noopener noreferrer\">Object Management<\/a><\/p>\n<p><a href=\"https:\/\/zhuanlan.zhihu.com\/p\/208321484\" target=\"_blank\" rel=\"noopener noreferrer\">Unity\u57fa\u7840\u6559\u7a0b\u7cfb\u5217\u2014\u2014Unity\u5bf9\u8c61\u7ba1\u740612\u7bc7<\/a><\/p>\n<p><a href=\"https:\/\/catlikecoding.com\/unity\/tutorials\/tower-defense\/the-board\/\" target=\"_blank\" rel=\"noopener noreferrer\">Unity Tower Defense &#8211; The Board<\/a><\/p>\n<p><a href=\"https:\/\/zhuanlan.zhihu.com\/p\/310164913\" target=\"_blank\" rel=\"noopener noreferrer\">Unity Demo\u6559\u7a0b\u7cfb\u5217\u2014\u2014Unity\u5854\u9632\u6e38\u620f<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e0a\u4e00\u6b21\uff0c\u6211\u4eec\u5b66\u4e60\u4e86U3D\u7684\u5165\u95e8\uff0c\u5e76\u5c1d\u8bd5\u4e86\u7b80\u5355\u7684\u5f39\u8df3\u3001\u53d1\u5c04\u3001\u78b0\u649e\u6548\u679c\u5236\u4f5c\u6280\u5de7\u3002\u8fd9\u6b21\uff0c\u6211\u4eec\u8bd5\u56fe\u6311\u6218\u4e0b\uff0c\u5c1d\u8bd5\u505a\u4e00\u4e2a\u7c97 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1140],"tags":[884,1141,1145,1144,1146],"_links":{"self":[{"href":"https:\/\/yanjingang.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5013"}],"collection":[{"href":"https:\/\/yanjingang.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/yanjingang.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/yanjingang.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/yanjingang.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5013"}],"version-history":[{"count":0,"href":"https:\/\/yanjingang.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5013\/revisions"}],"wp:attachment":[{"href":"https:\/\/yanjingang.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5013"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yanjingang.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5013"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yanjingang.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5013"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}