├── .gitignore ├── Assets ├── TileEditor.meta └── TileEditor │ ├── Demo.meta │ ├── Demo │ ├── Materials.meta │ ├── Materials │ │ ├── Brick.mat │ │ ├── Brick.mat.meta │ │ ├── BrickTop.mat │ │ ├── BrickTop.mat.meta │ │ ├── Grass.mat │ │ ├── Grass.mat.meta │ │ ├── Line.mat │ │ ├── Line.mat.meta │ │ ├── Player.mat │ │ ├── Player.mat.meta │ │ ├── Stone.mat │ │ ├── Stone.mat.meta │ │ ├── Wood.mat │ │ ├── Wood.mat.meta │ │ ├── WoodTop.mat │ │ └── WoodTop.mat.meta │ ├── Scenes.meta │ ├── Scenes │ │ ├── TileEditorDemo.unity │ │ └── TileEditorDemo.unity.meta │ ├── Scripts.meta │ ├── Scripts │ │ ├── Player.cs │ │ └── Player.cs.meta │ ├── Textures.meta │ ├── Textures │ │ ├── Brick.png │ │ ├── Brick.png.meta │ │ ├── BrickTop.png │ │ ├── BrickTop.png.meta │ │ ├── Grass.png │ │ ├── Grass.png.meta │ │ ├── Stone.png │ │ ├── Stone.png.meta │ │ ├── Wood.png │ │ ├── Wood.png.meta │ │ ├── WoodTop.png │ │ └── WoodTop.png.meta │ ├── Tiles.meta │ └── Tiles │ │ ├── BrickWall.prefab │ │ ├── BrickWall.prefab.meta │ │ ├── Crate.prefab │ │ ├── Crate.prefab.meta │ │ ├── GrassFloor.prefab │ │ ├── GrassFloor.prefab.meta │ │ ├── StoneFloor.prefab │ │ ├── StoneFloor.prefab.meta │ │ ├── TileSet.asset │ │ ├── TileSet.asset.meta │ │ ├── WoodWall.prefab │ │ └── WoodWall.prefab.meta │ ├── Scripts.meta │ └── Scripts │ ├── Editor.meta │ ├── Editor │ ├── TileMapEditor.cs │ └── TileMapEditor.cs.meta │ ├── PathTile.cs │ ├── PathTile.cs.meta │ ├── TileMap.cs │ ├── TileMap.cs.meta │ ├── TileSet.cs │ └── TileSet.cs.meta ├── LICENSE ├── ProjectSettings ├── AudioManager.asset ├── DynamicsManager.asset ├── EditorBuildSettings.asset ├── EditorSettings.asset ├── GraphicsSettings.asset ├── InputManager.asset ├── NavMeshLayers.asset ├── NetworkManager.asset ├── Physics2DSettings.asset ├── ProjectSettings.asset ├── QualitySettings.asset ├── TagManager.asset └── TimeManager.asset ├── README.md └── TileEditor.userprefs /.gitignore: -------------------------------------------------------------------------------- 1 | [Ll]ibrary/ 2 | [Tt]emp/ 3 | [Oo]bj/ 4 | 5 | # Autogenerated VS/MD solution and project files 6 | *.csproj 7 | *.unityproj 8 | *.sln 9 | -------------------------------------------------------------------------------- /Assets/TileEditor.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 7cfd468fea89b2040b6c5cd849836c8b 3 | folderAsset: yes 4 | DefaultImporter: 5 | userData: 6 | -------------------------------------------------------------------------------- /Assets/TileEditor/Demo.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 0c9b4d1e38416f64e8e21cb127fc4750 3 | folderAsset: yes 4 | DefaultImporter: 5 | userData: 6 | -------------------------------------------------------------------------------- /Assets/TileEditor/Demo/Materials.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: a194f39b70c2f95488dbd6ebb2b1a129 3 | folderAsset: yes 4 | DefaultImporter: 5 | userData: 6 | -------------------------------------------------------------------------------- /Assets/TileEditor/Demo/Materials/Brick.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UnityPatterns/TileEditor/936e48f0fdeab3cc1046ef6afad49f2905a7df01/Assets/TileEditor/Demo/Materials/Brick.mat -------------------------------------------------------------------------------- /Assets/TileEditor/Demo/Materials/Brick.mat.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 165559cad1f8339499642be0d8728f5b 3 | NativeFormatImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Assets/TileEditor/Demo/Materials/BrickTop.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UnityPatterns/TileEditor/936e48f0fdeab3cc1046ef6afad49f2905a7df01/Assets/TileEditor/Demo/Materials/BrickTop.mat -------------------------------------------------------------------------------- /Assets/TileEditor/Demo/Materials/BrickTop.mat.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 2cef99d4478002e4b8cc9e36f52eb312 3 | NativeFormatImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Assets/TileEditor/Demo/Materials/Grass.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UnityPatterns/TileEditor/936e48f0fdeab3cc1046ef6afad49f2905a7df01/Assets/TileEditor/Demo/Materials/Grass.mat -------------------------------------------------------------------------------- /Assets/TileEditor/Demo/Materials/Grass.mat.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 2d7714c9862470e498d60ef5db2067f7 3 | NativeFormatImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Assets/TileEditor/Demo/Materials/Line.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UnityPatterns/TileEditor/936e48f0fdeab3cc1046ef6afad49f2905a7df01/Assets/TileEditor/Demo/Materials/Line.mat -------------------------------------------------------------------------------- /Assets/TileEditor/Demo/Materials/Line.mat.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 26ff9c69aba5efb44bdb6824f53bb5a2 3 | NativeFormatImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Assets/TileEditor/Demo/Materials/Player.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UnityPatterns/TileEditor/936e48f0fdeab3cc1046ef6afad49f2905a7df01/Assets/TileEditor/Demo/Materials/Player.mat -------------------------------------------------------------------------------- /Assets/TileEditor/Demo/Materials/Player.mat.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: d900a1594b4088544b44c35c53d2f343 3 | NativeFormatImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Assets/TileEditor/Demo/Materials/Stone.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UnityPatterns/TileEditor/936e48f0fdeab3cc1046ef6afad49f2905a7df01/Assets/TileEditor/Demo/Materials/Stone.mat -------------------------------------------------------------------------------- /Assets/TileEditor/Demo/Materials/Stone.mat.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: edb92f2cd9afa9248984ced7f80c40c7 3 | NativeFormatImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Assets/TileEditor/Demo/Materials/Wood.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UnityPatterns/TileEditor/936e48f0fdeab3cc1046ef6afad49f2905a7df01/Assets/TileEditor/Demo/Materials/Wood.mat -------------------------------------------------------------------------------- /Assets/TileEditor/Demo/Materials/Wood.mat.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 1cebc53202dba4f4991e227600fc3de6 3 | NativeFormatImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Assets/TileEditor/Demo/Materials/WoodTop.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UnityPatterns/TileEditor/936e48f0fdeab3cc1046ef6afad49f2905a7df01/Assets/TileEditor/Demo/Materials/WoodTop.mat -------------------------------------------------------------------------------- /Assets/TileEditor/Demo/Materials/WoodTop.mat.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 8c8248b18af8b594c97b3d789da57e1b 3 | NativeFormatImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Assets/TileEditor/Demo/Scenes.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: c93eed3f742915144b78d3e85675ff22 3 | folderAsset: yes 4 | DefaultImporter: 5 | userData: 6 | -------------------------------------------------------------------------------- /Assets/TileEditor/Demo/Scenes/TileEditorDemo.unity: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UnityPatterns/TileEditor/936e48f0fdeab3cc1046ef6afad49f2905a7df01/Assets/TileEditor/Demo/Scenes/TileEditorDemo.unity -------------------------------------------------------------------------------- /Assets/TileEditor/Demo/Scenes/TileEditorDemo.unity.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 8dbfc55449ddc4242b5a235f3b4bfff4 3 | DefaultImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Assets/TileEditor/Demo/Scripts.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 8de87a07b24160e4fbb4ad83eaec880e 3 | folderAsset: yes 4 | DefaultImporter: 5 | userData: 6 | -------------------------------------------------------------------------------- /Assets/TileEditor/Demo/Scripts/Player.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | 5 | public class Player : MonoBehaviour 6 | { 7 | public float walkSpeed; 8 | 9 | TileMap tileMap; 10 | List path = new List(); 11 | LineRenderer lineRenderer; 12 | 13 | void Start() 14 | { 15 | lineRenderer = GetComponent(); 16 | tileMap = FindObjectOfType(typeof(TileMap)) as TileMap; 17 | enabled = tileMap != null; 18 | 19 | 20 | } 21 | 22 | void Update() 23 | { 24 | if (Input.GetMouseButtonDown(0)) 25 | { 26 | var plane = new Plane(Vector3.up, Vector3.zero); 27 | var ray = Camera.main.ScreenPointToRay(Input.mousePosition); 28 | float hit; 29 | if (plane.Raycast(ray, out hit)) 30 | { 31 | var target = ray.GetPoint(hit); 32 | if (tileMap.FindPath(transform.position, target, path)) 33 | { 34 | lineRenderer.SetVertexCount(path.Count); 35 | for (int i = 0; i < path.Count; i++) 36 | lineRenderer.SetPosition(i, path[i].transform.position); 37 | 38 | StopAllCoroutines(); 39 | StartCoroutine(WalkPath()); 40 | } 41 | } 42 | } 43 | } 44 | 45 | IEnumerator WalkPath() 46 | { 47 | var index = 0; 48 | while (index < path.Count) 49 | { 50 | yield return StartCoroutine(WalkTo(path[index].transform.position)); 51 | index++; 52 | } 53 | } 54 | 55 | IEnumerator WalkTo(Vector3 position) 56 | { 57 | while (Vector3.Distance(transform.position, position) > 0.01f) 58 | { 59 | transform.position = Vector3.MoveTowards(transform.position, position, walkSpeed * Time.deltaTime); 60 | yield return 0; 61 | } 62 | transform.position = position; 63 | } 64 | 65 | /*void OnDrawGizmos() 66 | { 67 | Gizmos.color = Color.blue; 68 | for (int i = 0; i < path.Count; i++) 69 | { 70 | Gizmos.DrawSphere(path[i].transform.position, 0.05f); 71 | if (i > 0) 72 | Gizmos.DrawLine(path[i - 1].transform.position, path[i].transform.position); 73 | } 74 | }*/ 75 | } 76 | -------------------------------------------------------------------------------- /Assets/TileEditor/Demo/Scripts/Player.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 1df5fbaccb0bbf34484c7d008e385090 3 | MonoImporter: 4 | serializedVersion: 2 5 | defaultReferences: [] 6 | executionOrder: 0 7 | icon: {instanceID: 0} 8 | userData: 9 | -------------------------------------------------------------------------------- /Assets/TileEditor/Demo/Textures.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 8a84932a67ee22d45be385344d62e16b 3 | folderAsset: yes 4 | DefaultImporter: 5 | userData: 6 | -------------------------------------------------------------------------------- /Assets/TileEditor/Demo/Textures/Brick.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UnityPatterns/TileEditor/936e48f0fdeab3cc1046ef6afad49f2905a7df01/Assets/TileEditor/Demo/Textures/Brick.png -------------------------------------------------------------------------------- /Assets/TileEditor/Demo/Textures/Brick.png.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: e015315d1f7c1044196201cfe9a6d687 3 | TextureImporter: 4 | serializedVersion: 2 5 | mipmaps: 6 | mipMapMode: 0 7 | enableMipMap: 1 8 | linearTexture: 0 9 | correctGamma: 0 10 | fadeOut: 0 11 | borderMipMap: 0 12 | mipMapFadeDistanceStart: 1 13 | mipMapFadeDistanceEnd: 3 14 | bumpmap: 15 | convertToNormalMap: 0 16 | externalNormalMap: 0 17 | heightScale: .25 18 | normalMapFilter: 0 19 | isReadable: 0 20 | grayScaleToAlpha: 0 21 | generateCubemap: 0 22 | seamlessCubemap: 0 23 | textureFormat: -3 24 | maxTextureSize: 256 25 | textureSettings: 26 | filterMode: -1 27 | aniso: 2 28 | mipBias: -1 29 | wrapMode: -1 30 | nPOTScale: 1 31 | lightmap: 0 32 | compressionQuality: 50 33 | spriteMode: 0 34 | spriteExtrude: 1 35 | alignment: 0 36 | spritePivot: {x: .5, y: .5} 37 | spritePixelsToUnits: 100 38 | alphaIsTransparency: 0 39 | textureType: 0 40 | buildTargetSettings: [] 41 | spriteSheet: 42 | sprites: [] 43 | spriteAtlasHint: 44 | userData: 45 | -------------------------------------------------------------------------------- /Assets/TileEditor/Demo/Textures/BrickTop.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UnityPatterns/TileEditor/936e48f0fdeab3cc1046ef6afad49f2905a7df01/Assets/TileEditor/Demo/Textures/BrickTop.png -------------------------------------------------------------------------------- /Assets/TileEditor/Demo/Textures/BrickTop.png.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 5f847c0792968854f951b3c0454da7a4 3 | TextureImporter: 4 | serializedVersion: 2 5 | mipmaps: 6 | mipMapMode: 0 7 | enableMipMap: 1 8 | linearTexture: 0 9 | correctGamma: 0 10 | fadeOut: 0 11 | borderMipMap: 0 12 | mipMapFadeDistanceStart: 1 13 | mipMapFadeDistanceEnd: 3 14 | bumpmap: 15 | convertToNormalMap: 0 16 | externalNormalMap: 0 17 | heightScale: .25 18 | normalMapFilter: 0 19 | isReadable: 0 20 | grayScaleToAlpha: 0 21 | generateCubemap: 0 22 | seamlessCubemap: 0 23 | textureFormat: -1 24 | maxTextureSize: 1024 25 | textureSettings: 26 | filterMode: -1 27 | aniso: -1 28 | mipBias: -1 29 | wrapMode: -1 30 | nPOTScale: 1 31 | lightmap: 0 32 | compressionQuality: 50 33 | spriteMode: 0 34 | spriteExtrude: 1 35 | alignment: 0 36 | spritePivot: {x: .5, y: .5} 37 | spritePixelsToUnits: 100 38 | alphaIsTransparency: 0 39 | textureType: -1 40 | buildTargetSettings: [] 41 | spriteSheet: 42 | sprites: [] 43 | spriteAtlasHint: 44 | userData: 45 | -------------------------------------------------------------------------------- /Assets/TileEditor/Demo/Textures/Grass.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UnityPatterns/TileEditor/936e48f0fdeab3cc1046ef6afad49f2905a7df01/Assets/TileEditor/Demo/Textures/Grass.png -------------------------------------------------------------------------------- /Assets/TileEditor/Demo/Textures/Grass.png.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 7fe6af932d23beb47b91230f1813a3ff 3 | TextureImporter: 4 | serializedVersion: 2 5 | mipmaps: 6 | mipMapMode: 0 7 | enableMipMap: 1 8 | linearTexture: 0 9 | correctGamma: 0 10 | fadeOut: 0 11 | borderMipMap: 0 12 | mipMapFadeDistanceStart: 1 13 | mipMapFadeDistanceEnd: 3 14 | bumpmap: 15 | convertToNormalMap: 0 16 | externalNormalMap: 0 17 | heightScale: .25 18 | normalMapFilter: 0 19 | isReadable: 0 20 | grayScaleToAlpha: 0 21 | generateCubemap: 0 22 | seamlessCubemap: 0 23 | textureFormat: -3 24 | maxTextureSize: 256 25 | textureSettings: 26 | filterMode: -1 27 | aniso: 2 28 | mipBias: -1 29 | wrapMode: -1 30 | nPOTScale: 1 31 | lightmap: 0 32 | compressionQuality: 50 33 | spriteMode: 0 34 | spriteExtrude: 1 35 | alignment: 0 36 | spritePivot: {x: .5, y: .5} 37 | spritePixelsToUnits: 100 38 | alphaIsTransparency: 0 39 | textureType: 0 40 | buildTargetSettings: [] 41 | spriteSheet: 42 | sprites: [] 43 | spriteAtlasHint: 44 | userData: 45 | -------------------------------------------------------------------------------- /Assets/TileEditor/Demo/Textures/Stone.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UnityPatterns/TileEditor/936e48f0fdeab3cc1046ef6afad49f2905a7df01/Assets/TileEditor/Demo/Textures/Stone.png -------------------------------------------------------------------------------- /Assets/TileEditor/Demo/Textures/Stone.png.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 983a039bdedd733439cf86ea5cc8691a 3 | TextureImporter: 4 | serializedVersion: 2 5 | mipmaps: 6 | mipMapMode: 0 7 | enableMipMap: 1 8 | linearTexture: 0 9 | correctGamma: 0 10 | fadeOut: 0 11 | borderMipMap: 0 12 | mipMapFadeDistanceStart: 1 13 | mipMapFadeDistanceEnd: 3 14 | bumpmap: 15 | convertToNormalMap: 0 16 | externalNormalMap: 0 17 | heightScale: .25 18 | normalMapFilter: 0 19 | isReadable: 0 20 | grayScaleToAlpha: 0 21 | generateCubemap: 0 22 | seamlessCubemap: 0 23 | textureFormat: -3 24 | maxTextureSize: 256 25 | textureSettings: 26 | filterMode: -1 27 | aniso: 2 28 | mipBias: -1 29 | wrapMode: -1 30 | nPOTScale: 1 31 | lightmap: 0 32 | compressionQuality: 50 33 | spriteMode: 0 34 | spriteExtrude: 1 35 | alignment: 0 36 | spritePivot: {x: .5, y: .5} 37 | spritePixelsToUnits: 100 38 | alphaIsTransparency: 0 39 | textureType: 0 40 | buildTargetSettings: [] 41 | spriteSheet: 42 | sprites: [] 43 | spriteAtlasHint: 44 | userData: 45 | -------------------------------------------------------------------------------- /Assets/TileEditor/Demo/Textures/Wood.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UnityPatterns/TileEditor/936e48f0fdeab3cc1046ef6afad49f2905a7df01/Assets/TileEditor/Demo/Textures/Wood.png -------------------------------------------------------------------------------- /Assets/TileEditor/Demo/Textures/Wood.png.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: ae5bf44cc7cd1d547a2aaf73929ae80a 3 | TextureImporter: 4 | serializedVersion: 2 5 | mipmaps: 6 | mipMapMode: 0 7 | enableMipMap: 1 8 | linearTexture: 0 9 | correctGamma: 0 10 | fadeOut: 0 11 | borderMipMap: 0 12 | mipMapFadeDistanceStart: 1 13 | mipMapFadeDistanceEnd: 3 14 | bumpmap: 15 | convertToNormalMap: 0 16 | externalNormalMap: 0 17 | heightScale: .25 18 | normalMapFilter: 0 19 | isReadable: 0 20 | grayScaleToAlpha: 0 21 | generateCubemap: 0 22 | seamlessCubemap: 0 23 | textureFormat: -3 24 | maxTextureSize: 256 25 | textureSettings: 26 | filterMode: -1 27 | aniso: 2 28 | mipBias: -1 29 | wrapMode: -1 30 | nPOTScale: 1 31 | lightmap: 0 32 | compressionQuality: 50 33 | spriteMode: 0 34 | spriteExtrude: 1 35 | alignment: 0 36 | spritePivot: {x: .5, y: .5} 37 | spritePixelsToUnits: 100 38 | alphaIsTransparency: 0 39 | textureType: 0 40 | buildTargetSettings: [] 41 | spriteSheet: 42 | sprites: [] 43 | spriteAtlasHint: 44 | userData: 45 | -------------------------------------------------------------------------------- /Assets/TileEditor/Demo/Textures/WoodTop.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UnityPatterns/TileEditor/936e48f0fdeab3cc1046ef6afad49f2905a7df01/Assets/TileEditor/Demo/Textures/WoodTop.png -------------------------------------------------------------------------------- /Assets/TileEditor/Demo/Textures/WoodTop.png.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: b4605a7de2204e14dbc7a4f86e7a9e7b 3 | TextureImporter: 4 | serializedVersion: 2 5 | mipmaps: 6 | mipMapMode: 0 7 | enableMipMap: 1 8 | linearTexture: 0 9 | correctGamma: 0 10 | fadeOut: 0 11 | borderMipMap: 0 12 | mipMapFadeDistanceStart: 1 13 | mipMapFadeDistanceEnd: 3 14 | bumpmap: 15 | convertToNormalMap: 0 16 | externalNormalMap: 0 17 | heightScale: .25 18 | normalMapFilter: 0 19 | isReadable: 0 20 | grayScaleToAlpha: 0 21 | generateCubemap: 0 22 | seamlessCubemap: 0 23 | textureFormat: -3 24 | maxTextureSize: 256 25 | textureSettings: 26 | filterMode: -1 27 | aniso: 2 28 | mipBias: -1 29 | wrapMode: -1 30 | nPOTScale: 1 31 | lightmap: 0 32 | compressionQuality: 50 33 | spriteMode: 0 34 | spriteExtrude: 1 35 | alignment: 0 36 | spritePivot: {x: .5, y: .5} 37 | spritePixelsToUnits: 100 38 | alphaIsTransparency: 0 39 | textureType: 0 40 | buildTargetSettings: [] 41 | spriteSheet: 42 | sprites: [] 43 | spriteAtlasHint: 44 | userData: 45 | -------------------------------------------------------------------------------- /Assets/TileEditor/Demo/Tiles.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 003d1cb12e700ad49898b6023d53d7c2 3 | folderAsset: yes 4 | DefaultImporter: 5 | userData: 6 | -------------------------------------------------------------------------------- /Assets/TileEditor/Demo/Tiles/BrickWall.prefab: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UnityPatterns/TileEditor/936e48f0fdeab3cc1046ef6afad49f2905a7df01/Assets/TileEditor/Demo/Tiles/BrickWall.prefab -------------------------------------------------------------------------------- /Assets/TileEditor/Demo/Tiles/BrickWall.prefab.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 76c1caf0e5f6f394b8c14fcfc45a7df1 3 | NativeFormatImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Assets/TileEditor/Demo/Tiles/Crate.prefab: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UnityPatterns/TileEditor/936e48f0fdeab3cc1046ef6afad49f2905a7df01/Assets/TileEditor/Demo/Tiles/Crate.prefab -------------------------------------------------------------------------------- /Assets/TileEditor/Demo/Tiles/Crate.prefab.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: eaeab4a22882b2645b5bf273ef4e8b0c 3 | NativeFormatImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Assets/TileEditor/Demo/Tiles/GrassFloor.prefab: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UnityPatterns/TileEditor/936e48f0fdeab3cc1046ef6afad49f2905a7df01/Assets/TileEditor/Demo/Tiles/GrassFloor.prefab -------------------------------------------------------------------------------- /Assets/TileEditor/Demo/Tiles/GrassFloor.prefab.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: e27d39705f5b8fe4c857be1f80f6aa2e 3 | NativeFormatImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Assets/TileEditor/Demo/Tiles/StoneFloor.prefab: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UnityPatterns/TileEditor/936e48f0fdeab3cc1046ef6afad49f2905a7df01/Assets/TileEditor/Demo/Tiles/StoneFloor.prefab -------------------------------------------------------------------------------- /Assets/TileEditor/Demo/Tiles/StoneFloor.prefab.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: cad3f9c2ca58ee148a8fdf327a7fead8 3 | NativeFormatImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Assets/TileEditor/Demo/Tiles/TileSet.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UnityPatterns/TileEditor/936e48f0fdeab3cc1046ef6afad49f2905a7df01/Assets/TileEditor/Demo/Tiles/TileSet.asset -------------------------------------------------------------------------------- /Assets/TileEditor/Demo/Tiles/TileSet.asset.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 9e553eaec9861f94ca4abbee233d10b3 3 | NativeFormatImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Assets/TileEditor/Demo/Tiles/WoodWall.prefab: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UnityPatterns/TileEditor/936e48f0fdeab3cc1046ef6afad49f2905a7df01/Assets/TileEditor/Demo/Tiles/WoodWall.prefab -------------------------------------------------------------------------------- /Assets/TileEditor/Demo/Tiles/WoodWall.prefab.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 37ae2c2122b7794428a3c09de2c4164c 3 | NativeFormatImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Assets/TileEditor/Scripts.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 9c65086d9ac6798469cbcb61b2a0efcb 3 | folderAsset: yes 4 | DefaultImporter: 5 | userData: 6 | -------------------------------------------------------------------------------- /Assets/TileEditor/Scripts/Editor.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 9b6ed0c21162b484297afc00998ad4e0 3 | folderAsset: yes 4 | DefaultImporter: 5 | userData: 6 | -------------------------------------------------------------------------------- /Assets/TileEditor/Scripts/Editor/TileMapEditor.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using UnityEditor; 3 | using System.Collections; 4 | using System.Collections.Generic; 5 | using System.Reflection; 6 | using System.IO; 7 | 8 | [CustomEditor(typeof(TileMap))] 9 | public class TileMapEditor : Editor 10 | { 11 | enum State { Hover, BoxSelect } 12 | static Vector3[] rect = new Vector3[4]; 13 | 14 | TileMap tileMap; 15 | FieldInfo undoCallback; 16 | bool editing; 17 | Matrix4x4 worldToLocal; 18 | 19 | State state; 20 | int cursorX; 21 | int cursorZ; 22 | int cursorClickX; 23 | int cursorClickZ; 24 | bool deleting; 25 | int direction; 26 | 27 | bool updateConnections = true; 28 | bool wireframeHidden; 29 | 30 | #region Inspector GUI 31 | 32 | public override void OnInspectorGUI() 33 | { 34 | //Get tilemap 35 | if (tileMap == null) 36 | tileMap = (TileMap)target; 37 | 38 | //Crazy hack to register undo 39 | if (undoCallback == null) 40 | { 41 | undoCallback = typeof(EditorApplication).GetField("undoRedoPerformed", BindingFlags.NonPublic | BindingFlags.Static); 42 | if (undoCallback != null) 43 | undoCallback.SetValue(null, new EditorApplication.CallbackFunction(OnUndoRedo)); 44 | } 45 | 46 | //Toggle editing mode 47 | if (editing) 48 | { 49 | if (GUILayout.Button("Stop Editing")) 50 | editing = false; 51 | else 52 | { 53 | EditorGUILayout.BeginHorizontal(); 54 | if (GUILayout.Button("Update All")) 55 | UpdateAll(); 56 | if (GUILayout.Button("Clear")) 57 | Clear(); 58 | EditorGUILayout.EndHorizontal(); 59 | } 60 | } 61 | else if (GUILayout.Button("Edit TileMap")) 62 | editing = true; 63 | 64 | //Tile Size 65 | EditorGUI.BeginChangeCheck(); 66 | var newTileSize = EditorGUILayout.FloatField("Tile Size", tileMap.tileSize); 67 | if (EditorGUI.EndChangeCheck()) 68 | { 69 | RecordDeepUndo(); 70 | tileMap.tileSize = newTileSize; 71 | UpdatePositions(); 72 | } 73 | 74 | //Tile Prefab 75 | EditorGUI.BeginChangeCheck(); 76 | var newTilePrefab = (Transform)EditorGUILayout.ObjectField("Tile Prefab", tileMap.tilePrefab, typeof(Transform), false); 77 | if (EditorGUI.EndChangeCheck()) 78 | { 79 | RecordUndo(); 80 | tileMap.tilePrefab = newTilePrefab; 81 | } 82 | 83 | //Tile Map 84 | EditorGUI.BeginChangeCheck(); 85 | var newTileSet = (TileSet)EditorGUILayout.ObjectField("Tile Set", tileMap.tileSet, typeof(TileSet), false); 86 | if (EditorGUI.EndChangeCheck()) 87 | { 88 | RecordUndo(); 89 | tileMap.tileSet = newTileSet; 90 | } 91 | 92 | //Tile Prefab selector 93 | if (tileMap.tileSet != null) 94 | { 95 | EditorGUI.BeginChangeCheck(); 96 | var names = new string[tileMap.tileSet.prefabs.Length + 1]; 97 | var values = new int[names.Length + 1]; 98 | names[0] = tileMap.tilePrefab != null ? tileMap.tilePrefab.name : ""; 99 | values[0] = 0; 100 | for (int i = 1; i < names.Length; i++) 101 | { 102 | names[i] = tileMap.tileSet.prefabs[i - 1] != null ? tileMap.tileSet.prefabs[i - 1].name : ""; 103 | //if (i < 10) 104 | // names[i] = i + ". " + names[i]; 105 | values[i] = i; 106 | } 107 | var index = EditorGUILayout.IntPopup("Select Tile", 0, names, values); 108 | if (EditorGUI.EndChangeCheck() && index > 0) 109 | { 110 | RecordUndo(); 111 | tileMap.tilePrefab = tileMap.tileSet.prefabs[index - 1]; 112 | } 113 | } 114 | 115 | //Selecting direction 116 | EditorGUILayout.BeginHorizontal(GUILayout.Width(60)); 117 | EditorGUILayout.PrefixLabel("Direction"); 118 | EditorGUILayout.BeginVertical(GUILayout.Width(20)); 119 | GUILayout.Space(20); 120 | if (direction == 3) 121 | GUILayout.Box("<", GUILayout.Width(20)); 122 | else if (GUILayout.Button("<")) 123 | direction = 3; 124 | GUILayout.Space(20); 125 | EditorGUILayout.EndVertical(); 126 | EditorGUILayout.BeginVertical(GUILayout.Width(20)); 127 | if (direction == 0) 128 | GUILayout.Box("^", GUILayout.Width(20)); 129 | else if (GUILayout.Button("^")) 130 | direction = 0; 131 | if (direction == -1) 132 | GUILayout.Box("?", GUILayout.Width(20)); 133 | else if (GUILayout.Button("?")) 134 | direction = -1; 135 | if (direction == 2) 136 | GUILayout.Box("v", GUILayout.Width(20)); 137 | else if (GUILayout.Button("v")) 138 | direction = 2; 139 | EditorGUILayout.EndVertical(); 140 | EditorGUILayout.BeginVertical(GUILayout.Width(20)); 141 | GUILayout.Space(20); 142 | if (direction == 1) 143 | GUILayout.Box(">", GUILayout.Width(20)); 144 | else if (GUILayout.Button(">")) 145 | direction = 1; 146 | GUILayout.Space(20); 147 | EditorGUILayout.EndVertical(); 148 | EditorGUILayout.EndHorizontal(); 149 | 150 | //Connect diagonals 151 | EditorGUI.BeginChangeCheck(); 152 | var newConnectDiagonals = EditorGUILayout.Toggle("Connect Diagonals", tileMap.connectDiagonals); 153 | if (EditorGUI.EndChangeCheck()) 154 | { 155 | RecordUndo(); 156 | tileMap.connectDiagonals = newConnectDiagonals; 157 | updateConnections = true; 158 | SceneView.RepaintAll(); 159 | } 160 | 161 | //Connect diagonals 162 | if (tileMap.connectDiagonals) 163 | { 164 | EditorGUI.BeginChangeCheck(); 165 | var newCutCorners = EditorGUILayout.Toggle("Cut Corners", tileMap.cutCorners); 166 | if (EditorGUI.EndChangeCheck()) 167 | { 168 | RecordUndo(); 169 | tileMap.cutCorners = newCutCorners; 170 | updateConnections = true; 171 | SceneView.RepaintAll(); 172 | } 173 | } 174 | 175 | //Draw path tiles 176 | EditorGUI.BeginChangeCheck(); 177 | drawPathMap = EditorGUILayout.Toggle("Draw Path Map", drawPathMap); 178 | if (EditorGUI.EndChangeCheck()) 179 | SceneView.RepaintAll(); 180 | } 181 | 182 | #endregion 183 | 184 | #region Scene GUI 185 | 186 | void OnSceneGUI() 187 | { 188 | //Get tilemap 189 | if (tileMap == null) 190 | tileMap = (TileMap)target; 191 | 192 | //Update paths 193 | if (updateConnections) 194 | { 195 | updateConnections = false; 196 | tileMap.UpdateConnections(); 197 | } 198 | 199 | //Toggle editing 200 | if (e.type == EventType.KeyDown && e.keyCode == KeyCode.Tab) 201 | { 202 | editing = !editing; 203 | EditorUtility.SetDirty(target); 204 | } 205 | 206 | //Toggle selected tile 207 | /*if (tileMap.tileSet != null) 208 | { 209 | if (e.type == EventType.KeyDown) 210 | { 211 | var code = (int)e.keyCode - (int)KeyCode.Alpha1; 212 | if (code >= 0 && code < tileMap.tileSet.prefabs.Length) 213 | { 214 | RecordUndo(); 215 | tileMap.tilePrefab = tileMap.tileSet.prefabs[code]; 216 | e.Use(); 217 | return; 218 | } 219 | } 220 | }*/ 221 | 222 | //Draw path nodes 223 | if (drawPathMap) 224 | { 225 | Handles.color = new Color(0, 0, 1, 0.5f); 226 | foreach (var instance in tileMap.instances) 227 | { 228 | var tile = instance.GetComponent(); 229 | if (tile != null) 230 | { 231 | Handles.DotCap(0, tile.transform.localPosition, Quaternion.identity, tileMap.tileSize / 17); 232 | foreach (var other in tile.connections) 233 | if (other != null && tile.GetInstanceID() > other.GetInstanceID()) 234 | Handles.DrawLine(tile.transform.localPosition, other.transform.localPosition); 235 | } 236 | } 237 | } 238 | 239 | if (editing) 240 | { 241 | //Hide mesh 242 | HideWireframe(true); 243 | 244 | //Quit on tool change 245 | if (e.type == EventType.KeyDown) 246 | { 247 | switch (e.keyCode) 248 | { 249 | case KeyCode.Q: 250 | case KeyCode.W: 251 | case KeyCode.E: 252 | case KeyCode.R: 253 | return; 254 | } 255 | } 256 | 257 | //Quit if panning or no camera exists 258 | if (Tools.current == Tool.View || (e.isMouse && e.button > 1) || Camera.current == null || e.type == EventType.ScrollWheel) 259 | return; 260 | 261 | //Quit if laying out 262 | if (e.type == EventType.Layout) 263 | { 264 | HandleUtility.AddDefaultControl(GUIUtility.GetControlID(FocusType.Passive)); 265 | return; 266 | } 267 | 268 | //Update matrices 269 | Handles.matrix = tileMap.transform.localToWorldMatrix; 270 | worldToLocal = tileMap.transform.worldToLocalMatrix; 271 | 272 | //Draw axes 273 | Handles.color = Color.red; 274 | Handles.DrawLine(new Vector3(-tileMap.tileSize, 0, 0), new Vector3(tileMap.tileSize, 0, 0)); 275 | Handles.DrawLine(new Vector3(0, 0, -tileMap.tileSize), new Vector3(0, 0, tileMap.tileSize)); 276 | 277 | //Update mouse position 278 | var plane = new Plane(tileMap.transform.up, tileMap.transform.position); 279 | var ray = Camera.current.ScreenPointToRay(new Vector3(e.mousePosition.x, Camera.current.pixelHeight - e.mousePosition.y)); 280 | float hit; 281 | if (!plane.Raycast(ray, out hit)) 282 | return; 283 | var mousePosition = worldToLocal.MultiplyPoint(ray.GetPoint(hit)); 284 | cursorX = Mathf.RoundToInt(mousePosition.x / tileMap.tileSize); 285 | cursorZ = Mathf.RoundToInt(mousePosition.z / tileMap.tileSize); 286 | 287 | //Update the state and repaint 288 | state = UpdateState(); 289 | HandleUtility.Repaint(); 290 | e.Use(); 291 | } 292 | else 293 | HideWireframe(false); 294 | } 295 | 296 | void HideWireframe(bool hide) 297 | { 298 | if (wireframeHidden != hide) 299 | { 300 | wireframeHidden = hide; 301 | foreach (var renderer in tileMap.transform.GetComponentsInChildren()) 302 | EditorUtility.SetSelectedWireframeHidden(renderer, hide); 303 | } 304 | } 305 | 306 | #endregion 307 | 308 | #region Update state 309 | 310 | State UpdateState() 311 | { 312 | switch (state) 313 | { 314 | //Hovering 315 | case State.Hover: 316 | DrawGrid(); 317 | DrawRect(cursorX, cursorZ, 1, 1, Color.white, new Color(1, 1, 1, 0f)); 318 | if (e.type == EventType.MouseDown && e.button < 2) 319 | { 320 | cursorClickX = cursorX; 321 | cursorClickZ = cursorZ; 322 | deleting = e.button > 0; 323 | return State.BoxSelect; 324 | } 325 | break; 326 | 327 | //Placing 328 | case State.BoxSelect: 329 | 330 | //Get the drag selection 331 | var x = Mathf.Min(cursorX, cursorClickX); 332 | var z = Mathf.Min(cursorZ, cursorClickZ); 333 | var sizeX = Mathf.Abs(cursorX - cursorClickX) + 1; 334 | var sizeZ = Mathf.Abs(cursorZ - cursorClickZ) + 1; 335 | 336 | //Draw the drag selection 337 | DrawRect(x, z, sizeX, sizeZ, Color.white, deleting ? new Color(1, 0, 0, 0.2f) : new Color(0, 1, 0, 0.2f)); 338 | 339 | //Finish the drag 340 | if (e.type == EventType.MouseUp && e.button < 2) 341 | { 342 | if (deleting) 343 | { 344 | if (e.button > 0) 345 | SetRect(x, z, sizeX, sizeZ, null, direction); 346 | } 347 | else if (e.button == 0) 348 | SetRect(x, z, sizeX, sizeZ, tileMap.tilePrefab, direction); 349 | 350 | return State.Hover; 351 | } 352 | break; 353 | } 354 | return state; 355 | } 356 | 357 | void DrawGrid() 358 | { 359 | var gridSize = 5; 360 | var maxDist = Mathf.Sqrt(Mathf.Pow(gridSize - 1, 2) * 2) * 0.75f; 361 | for (int x = -gridSize; x <= gridSize; x++) 362 | { 363 | for (int z = -gridSize; z <= gridSize; z++) 364 | { 365 | Handles.color = new Color(1, 1, 1, 1 - Mathf.Sqrt(x * x + z * z) / maxDist); 366 | var p = new Vector3((cursorX + x) * tileMap.tileSize, 0, (cursorZ + z) * tileMap.tileSize); 367 | Handles.DotCap(0, p, Quaternion.identity, HandleUtility.GetHandleSize(p) * 0.02f); 368 | } 369 | } 370 | } 371 | 372 | void DrawRect(int x, int z, int sizeX, int sizeZ, Color outline, Color fill) 373 | { 374 | Handles.color = Color.white; 375 | var min = new Vector3(x * tileMap.tileSize - tileMap.tileSize / 2, 0, z * tileMap.tileSize - tileMap.tileSize / 2); 376 | var max = min + new Vector3(sizeX * tileMap.tileSize, 0, sizeZ * tileMap.tileSize); 377 | rect[0].Set(min.x, 0, min.z); 378 | rect[1].Set(max.x, 0, min.z); 379 | rect[2].Set(max.x, 0, max.z); 380 | rect[3].Set(min.x, 0, max.z); 381 | Handles.DrawSolidRectangleWithOutline(rect, fill, outline); 382 | } 383 | 384 | #endregion 385 | 386 | #region Modifying TileMap 387 | 388 | bool UpdateTile(int index) 389 | { 390 | //Destroy existing tile 391 | if (tileMap.instances[index] != null) 392 | { 393 | #if UNITY_4_3 394 | Undo.DestroyObjectImmediate(tileMap.instances[index].gameObject); 395 | #else 396 | DestroyImmediate(tileMap.instances[index].gameObject); 397 | #endif 398 | } 399 | 400 | //Check if prefab is null 401 | if (tileMap.prefabs[index] != null) 402 | { 403 | //Place the tile 404 | var instance = (Transform)PrefabUtility.InstantiatePrefab(tileMap.prefabs[index]); 405 | instance.parent = tileMap.transform; 406 | instance.localPosition = tileMap.GetPosition(index); 407 | instance.localRotation = Quaternion.Euler(0, tileMap.directions[index] * 90, 0); 408 | tileMap.instances[index] = instance; 409 | wireframeHidden = false; 410 | return true; 411 | } 412 | else 413 | { 414 | //Remove the tile 415 | tileMap.hashes.RemoveAt(index); 416 | tileMap.prefabs.RemoveAt(index); 417 | tileMap.directions.RemoveAt(index); 418 | tileMap.instances.RemoveAt(index); 419 | return false; 420 | } 421 | } 422 | 423 | void UpdatePositions() 424 | { 425 | for (int i = 0; i < tileMap.hashes.Count; i++) 426 | if (tileMap.instances[i] != null) 427 | tileMap.instances[i].localPosition = tileMap.GetPosition(i); 428 | } 429 | 430 | void UpdateAll() 431 | { 432 | int x, z; 433 | for (int i = 0; i < tileMap.hashes.Count; i++) 434 | { 435 | tileMap.GetPosition(i, out x, out z); 436 | SetTile(x, z, tileMap.prefabs[i], tileMap.directions[i]); 437 | } 438 | } 439 | 440 | void Clear() 441 | { 442 | RecordDeepUndo(); 443 | int x, z; 444 | while (tileMap.hashes.Count > 0) 445 | { 446 | tileMap.GetPosition(0, out x, out z); 447 | SetTile(x, z, null, 0); 448 | } 449 | } 450 | 451 | bool SetTile(int x, int z, Transform prefab, int direction) 452 | { 453 | var hash = tileMap.GetHash(x, z); 454 | var index = tileMap.hashes.IndexOf(hash); 455 | if (index >= 0) 456 | { 457 | //Replace existing tile 458 | tileMap.prefabs[index] = prefab; 459 | if (direction < 0) 460 | tileMap.directions[index] = Random.Range(0, 4); 461 | else 462 | tileMap.directions[index] = direction; 463 | return UpdateTile(index); 464 | } 465 | else if (prefab != null) 466 | { 467 | //Create new tile 468 | index = tileMap.prefabs.Count; 469 | tileMap.hashes.Add(hash); 470 | tileMap.prefabs.Add(prefab); 471 | if (direction < 0) 472 | tileMap.directions.Add(Random.Range(0, 4)); 473 | else 474 | tileMap.directions.Add(direction); 475 | tileMap.instances.Add(null); 476 | return UpdateTile(index); 477 | } 478 | else 479 | return false; 480 | } 481 | 482 | void SetRect(int x, int z, int sizeX, int sizeZ, Transform prefab, int direction) 483 | { 484 | RecordDeepUndo(); 485 | for (int xx = 0; xx < sizeX; xx++) 486 | for (int zz = 0; zz < sizeZ; zz++) 487 | SetTile(x + xx, z + zz, prefab, direction); 488 | } 489 | 490 | #endregion 491 | 492 | #region Undo handling 493 | 494 | void OnUndoRedo() 495 | { 496 | UpdatePositions(); 497 | updateConnections = true; 498 | } 499 | 500 | void RecordUndo() 501 | { 502 | updateConnections = true; 503 | #if UNITY_4_3 504 | Undo.RecordObject(target, "TileMap Changed"); 505 | #else 506 | Undo.RegisterUndo(target, "TileMap Changed"); 507 | #endif 508 | } 509 | 510 | void RecordDeepUndo() 511 | { 512 | updateConnections = true; 513 | #if UNITY_4_3 514 | Undo.RegisterFullObjectHierarchyUndo(target); 515 | #else 516 | Undo.RegisterSceneUndo("TileMap Changed"); 517 | #endif 518 | } 519 | 520 | #endregion 521 | 522 | #region Properties 523 | 524 | Event e 525 | { 526 | get { return Event.current; } 527 | } 528 | 529 | bool drawPathMap 530 | { 531 | get { return EditorPrefs.GetBool("TileMapEditor_drawPathMap", true); } 532 | set { EditorPrefs.SetBool("TileMapEditor_drawPathMap", value); } 533 | } 534 | 535 | #endregion 536 | 537 | #region Menu items 538 | 539 | [MenuItem("GameObject/Create Other/TileMap")] 540 | static void CreateTileMap() 541 | { 542 | var obj = new GameObject("TileMap"); 543 | obj.AddComponent(); 544 | } 545 | 546 | [MenuItem("Assets/Create/TileSet")] 547 | static void CreateTileSet() 548 | { 549 | var asset = ScriptableObject.CreateInstance(); 550 | var path = AssetDatabase.GetAssetPath(Selection.activeObject); 551 | 552 | if (string.IsNullOrEmpty(path)) 553 | path = "Assets"; 554 | else if (Path.GetExtension(path) != "") 555 | path = path.Replace(Path.GetFileName(AssetDatabase.GetAssetPath(Selection.activeObject)), ""); 556 | else 557 | path += "/"; 558 | 559 | var assetPathAndName = AssetDatabase.GenerateUniqueAssetPath(path + "TileSet.asset"); 560 | Debug.Log(assetPathAndName); 561 | AssetDatabase.CreateAsset(asset, assetPathAndName); 562 | AssetDatabase.SaveAssets(); 563 | EditorUtility.FocusProjectWindow(); 564 | Selection.activeObject = asset; 565 | asset.hideFlags = HideFlags.DontSave; 566 | } 567 | 568 | #endregion 569 | } 570 | -------------------------------------------------------------------------------- /Assets/TileEditor/Scripts/Editor/TileMapEditor.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 50d50f3fdb1c31443846516c01932f2b 3 | MonoImporter: 4 | serializedVersion: 2 5 | defaultReferences: [] 6 | executionOrder: 0 7 | icon: {instanceID: 0} 8 | userData: 9 | -------------------------------------------------------------------------------- /Assets/TileEditor/Scripts/PathTile.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | 5 | public class PathTile : MonoBehaviour 6 | { 7 | [HideInInspector] public List connections = new List(); 8 | } 9 | -------------------------------------------------------------------------------- /Assets/TileEditor/Scripts/PathTile.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: f8c8e9c0add0a8c4f8f4198206c6fc6b 3 | MonoImporter: 4 | serializedVersion: 2 5 | defaultReferences: [] 6 | executionOrder: 0 7 | icon: {instanceID: 0} 8 | userData: 9 | -------------------------------------------------------------------------------- /Assets/TileEditor/Scripts/TileMap.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | using System; 5 | 6 | public class TileMap : MonoBehaviour 7 | { 8 | static Queue queue = new Queue(); 9 | static List closed = new List(); 10 | static Dictionary source = new Dictionary(); 11 | 12 | public const int maxColumns = 10000; 13 | 14 | public float tileSize = 1; 15 | public Transform tilePrefab; 16 | public TileSet tileSet; 17 | public bool connectDiagonals; 18 | public bool cutCorners; 19 | 20 | public List hashes = new List(100000); 21 | public List prefabs = new List(100000); 22 | public List directions = new List(100000); 23 | public List instances = new List(100000); 24 | 25 | void Start() 26 | { 27 | UpdateConnections(); 28 | } 29 | 30 | public int GetHash(int x, int z) 31 | { 32 | return (x + TileMap.maxColumns / 2) + (z + TileMap.maxColumns / 2) * TileMap.maxColumns; 33 | } 34 | 35 | public int GetIndex(int x, int z) 36 | { 37 | return hashes.IndexOf(GetHash(x, z)); 38 | } 39 | 40 | public Vector3 GetPosition(int index) 41 | { 42 | index = hashes[index]; 43 | return new Vector3(((index % maxColumns) - (maxColumns / 2)) * tileSize, 0, ((index / maxColumns) - (maxColumns / 2)) * tileSize); 44 | } 45 | public void GetPosition(int index, out int x, out int z) 46 | { 47 | index = hashes[index]; 48 | x = (index % maxColumns) - (maxColumns / 2); 49 | z = (index / maxColumns) - (maxColumns / 2); 50 | } 51 | 52 | public void UpdateConnections() 53 | { 54 | //Build connections 55 | PathTile r, l, f, b; 56 | for (int i = 0; i < instances.Count; i++) 57 | { 58 | var tile = instances[i].GetComponent(); 59 | if (tile != null) 60 | { 61 | int x, z; 62 | GetPosition(i, out x, out z); 63 | tile.connections.Clear(); 64 | r = Connect(tile, x, z, x + 1, z); 65 | l = Connect(tile, x, z, x - 1, z); 66 | f = Connect(tile, x, z, x, z + 1); 67 | b = Connect(tile, x, z, x, z - 1); 68 | if (connectDiagonals) 69 | { 70 | if (cutCorners) 71 | { 72 | Connect(tile, x, z, x + 1, z + 1); 73 | Connect(tile, x, z, x - 1, z - 1); 74 | Connect(tile, x, z, x - 1, z + 1); 75 | Connect(tile, x, z, x + 1, z - 1); 76 | } 77 | else 78 | { 79 | if (r != null && f != null) 80 | Connect(tile, x, z, x + 1, z + 1); 81 | if (l != null && b != null) 82 | Connect(tile, x, z, x - 1, z - 1); 83 | if (l != null && f != null) 84 | Connect(tile, x, z, x - 1, z + 1); 85 | if (r != null && b != null) 86 | Connect(tile, x, z, x + 1, z - 1); 87 | } 88 | } 89 | } 90 | } 91 | } 92 | 93 | PathTile Connect(PathTile tile, int x, int z, int toX, int toZ) 94 | { 95 | var index = GetIndex(toX, toZ); 96 | if (index >= 0) 97 | { 98 | var other = instances[index].GetComponent(); 99 | if (other != null) 100 | { 101 | tile.connections.Add(other); 102 | return other; 103 | } 104 | } 105 | return null; 106 | } 107 | 108 | PathTile GetPathTile(int x, int z) 109 | { 110 | var index = GetIndex(x, z); 111 | if (index >= 0) 112 | return instances[index].GetComponent(); 113 | else 114 | return null; 115 | } 116 | public PathTile GetPathTile(Vector3 position) 117 | { 118 | var x = Mathf.RoundToInt(position.x / tileSize); 119 | var z = Mathf.RoundToInt(position.z / tileSize); 120 | return GetPathTile(x, z); 121 | } 122 | 123 | public bool FindPath(PathTile start, PathTile end, List path, Predicate isWalkable) 124 | { 125 | if (!isWalkable(end)) 126 | return false; 127 | closed.Clear(); 128 | source.Clear(); 129 | queue.Clear(); 130 | closed.Add(start); 131 | source.Add(start, null); 132 | if (isWalkable(start)) 133 | queue.Enqueue(start); 134 | while (queue.Count > 0) 135 | { 136 | var tile = queue.Dequeue(); 137 | if (tile == end) 138 | { 139 | path.Clear(); 140 | while (tile != null) 141 | { 142 | path.Add(tile); 143 | tile = source[tile]; 144 | } 145 | path.Reverse(); 146 | return true; 147 | } 148 | else 149 | { 150 | foreach (var connection in tile.connections) 151 | { 152 | if (!closed.Contains(connection) && isWalkable(connection)) 153 | { 154 | closed.Add(connection); 155 | source.Add(connection, tile); 156 | queue.Enqueue(connection); 157 | } 158 | } 159 | } 160 | } 161 | return false; 162 | } 163 | public bool FindPath(PathTile start, PathTile end, List path) 164 | { 165 | return FindPath(start, end, path, tile => true); 166 | } 167 | public bool FindPath(Vector3 start, Vector3 end, List path, Predicate isWalkable) 168 | { 169 | var startTile = GetPathTile(start); 170 | var endTile = GetPathTile(end); 171 | return startTile != null && endTile != null && FindPath(startTile, endTile, path, isWalkable); 172 | } 173 | public bool FindPath(Vector3 start, Vector3 end, List path) 174 | { 175 | return FindPath(start, end, path, tile => true); 176 | } 177 | } 178 | -------------------------------------------------------------------------------- /Assets/TileEditor/Scripts/TileMap.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 808a45ae61f5b0f4e93a4dc0e357f620 3 | MonoImporter: 4 | serializedVersion: 2 5 | defaultReferences: [] 6 | executionOrder: 0 7 | icon: {instanceID: 0} 8 | userData: 9 | -------------------------------------------------------------------------------- /Assets/TileEditor/Scripts/TileSet.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | 4 | public class TileSet : ScriptableObject 5 | { 6 | public Transform[] prefabs = new Transform[0]; 7 | } 8 | -------------------------------------------------------------------------------- /Assets/TileEditor/Scripts/TileSet.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: fb1c9249b009d79449ed0547b4b25ef4 3 | MonoImporter: 4 | serializedVersion: 2 5 | defaultReferences: [] 6 | executionOrder: 0 7 | icon: {instanceID: 0} 8 | userData: 9 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2013 UnityPatterns 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /ProjectSettings/AudioManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UnityPatterns/TileEditor/936e48f0fdeab3cc1046ef6afad49f2905a7df01/ProjectSettings/AudioManager.asset -------------------------------------------------------------------------------- /ProjectSettings/DynamicsManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UnityPatterns/TileEditor/936e48f0fdeab3cc1046ef6afad49f2905a7df01/ProjectSettings/DynamicsManager.asset -------------------------------------------------------------------------------- /ProjectSettings/EditorBuildSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UnityPatterns/TileEditor/936e48f0fdeab3cc1046ef6afad49f2905a7df01/ProjectSettings/EditorBuildSettings.asset -------------------------------------------------------------------------------- /ProjectSettings/EditorSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UnityPatterns/TileEditor/936e48f0fdeab3cc1046ef6afad49f2905a7df01/ProjectSettings/EditorSettings.asset -------------------------------------------------------------------------------- /ProjectSettings/GraphicsSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UnityPatterns/TileEditor/936e48f0fdeab3cc1046ef6afad49f2905a7df01/ProjectSettings/GraphicsSettings.asset -------------------------------------------------------------------------------- /ProjectSettings/InputManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UnityPatterns/TileEditor/936e48f0fdeab3cc1046ef6afad49f2905a7df01/ProjectSettings/InputManager.asset -------------------------------------------------------------------------------- /ProjectSettings/NavMeshLayers.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UnityPatterns/TileEditor/936e48f0fdeab3cc1046ef6afad49f2905a7df01/ProjectSettings/NavMeshLayers.asset -------------------------------------------------------------------------------- /ProjectSettings/NetworkManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UnityPatterns/TileEditor/936e48f0fdeab3cc1046ef6afad49f2905a7df01/ProjectSettings/NetworkManager.asset -------------------------------------------------------------------------------- /ProjectSettings/Physics2DSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UnityPatterns/TileEditor/936e48f0fdeab3cc1046ef6afad49f2905a7df01/ProjectSettings/Physics2DSettings.asset -------------------------------------------------------------------------------- /ProjectSettings/ProjectSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UnityPatterns/TileEditor/936e48f0fdeab3cc1046ef6afad49f2905a7df01/ProjectSettings/ProjectSettings.asset -------------------------------------------------------------------------------- /ProjectSettings/QualitySettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UnityPatterns/TileEditor/936e48f0fdeab3cc1046ef6afad49f2905a7df01/ProjectSettings/QualitySettings.asset -------------------------------------------------------------------------------- /ProjectSettings/TagManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UnityPatterns/TileEditor/936e48f0fdeab3cc1046ef6afad49f2905a7df01/ProjectSettings/TagManager.asset -------------------------------------------------------------------------------- /ProjectSettings/TimeManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UnityPatterns/TileEditor/936e48f0fdeab3cc1046ef6afad49f2905a7df01/ProjectSettings/TimeManager.asset -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | TileEditor 2 | ========== 3 | 4 | Creating tile-based levels in Unity is much faster and easier with this tile editing extension, which allows you to place and delete tile prefabs on a grid with ease. When you want to find your way through your maze, the built-in pathfinding functionality will handle the navigation for you! 5 | -------------------------------------------------------------------------------- /TileEditor.userprefs: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | --------------------------------------------------------------------------------