├── HexGrid ├── Assets │ ├── Lines.mat │ ├── Lines.mat.meta │ ├── Editor.meta │ ├── Tile.cs.meta │ ├── Editor │ │ ├── GridInspector.cs.meta │ │ └── GridInspector.cs │ ├── Grid.cs.meta │ ├── Tile.cs │ └── Grid.cs └── ProjectSettings │ ├── ProjectVersion.txt │ ├── AudioManager.asset │ ├── InputManager.asset │ ├── NavMeshAreas.asset │ ├── TagManager.asset │ ├── TimeManager.asset │ ├── EditorSettings.asset │ ├── NetworkManager.asset │ ├── DynamicsManager.asset │ ├── GraphicsSettings.asset │ ├── Physics2DSettings.asset │ ├── ProjectSettings.asset │ ├── QualitySettings.asset │ └── EditorBuildSettings.asset ├── .gitignore ├── LICENSE └── README.md /HexGrid/Assets/Lines.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Amaranthos/UnityHexGrid/HEAD/HexGrid/Assets/Lines.mat -------------------------------------------------------------------------------- /HexGrid/ProjectSettings/ProjectVersion.txt: -------------------------------------------------------------------------------- 1 | m_EditorVersion: 2019.2.4f1 2 | m_EditorVersionWithRevision: 2019.2.4f1 (c63b2af89a85) 3 | -------------------------------------------------------------------------------- /HexGrid/ProjectSettings/AudioManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Amaranthos/UnityHexGrid/HEAD/HexGrid/ProjectSettings/AudioManager.asset -------------------------------------------------------------------------------- /HexGrid/ProjectSettings/InputManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Amaranthos/UnityHexGrid/HEAD/HexGrid/ProjectSettings/InputManager.asset -------------------------------------------------------------------------------- /HexGrid/ProjectSettings/NavMeshAreas.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Amaranthos/UnityHexGrid/HEAD/HexGrid/ProjectSettings/NavMeshAreas.asset -------------------------------------------------------------------------------- /HexGrid/ProjectSettings/TagManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Amaranthos/UnityHexGrid/HEAD/HexGrid/ProjectSettings/TagManager.asset -------------------------------------------------------------------------------- /HexGrid/ProjectSettings/TimeManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Amaranthos/UnityHexGrid/HEAD/HexGrid/ProjectSettings/TimeManager.asset -------------------------------------------------------------------------------- /HexGrid/ProjectSettings/EditorSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Amaranthos/UnityHexGrid/HEAD/HexGrid/ProjectSettings/EditorSettings.asset -------------------------------------------------------------------------------- /HexGrid/ProjectSettings/NetworkManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Amaranthos/UnityHexGrid/HEAD/HexGrid/ProjectSettings/NetworkManager.asset -------------------------------------------------------------------------------- /HexGrid/ProjectSettings/DynamicsManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Amaranthos/UnityHexGrid/HEAD/HexGrid/ProjectSettings/DynamicsManager.asset -------------------------------------------------------------------------------- /HexGrid/ProjectSettings/GraphicsSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Amaranthos/UnityHexGrid/HEAD/HexGrid/ProjectSettings/GraphicsSettings.asset -------------------------------------------------------------------------------- /HexGrid/ProjectSettings/Physics2DSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Amaranthos/UnityHexGrid/HEAD/HexGrid/ProjectSettings/Physics2DSettings.asset -------------------------------------------------------------------------------- /HexGrid/ProjectSettings/ProjectSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Amaranthos/UnityHexGrid/HEAD/HexGrid/ProjectSettings/ProjectSettings.asset -------------------------------------------------------------------------------- /HexGrid/ProjectSettings/QualitySettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Amaranthos/UnityHexGrid/HEAD/HexGrid/ProjectSettings/QualitySettings.asset -------------------------------------------------------------------------------- /HexGrid/ProjectSettings/EditorBuildSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Amaranthos/UnityHexGrid/HEAD/HexGrid/ProjectSettings/EditorBuildSettings.asset -------------------------------------------------------------------------------- /HexGrid/Assets/Lines.mat.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: a2bb29413886117438d548505a01b202 3 | timeCreated: 1443742991 4 | licenseType: Free 5 | NativeFormatImporter: 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /HexGrid/Assets/Editor.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 7396975e85150de459c8734bc4e6c9fc 3 | folderAsset: yes 4 | timeCreated: 1443315367 5 | licenseType: Free 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /HexGrid/Assets/Tile.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 63cc89bce8d0a1a4fa44f00bf66211b0 3 | timeCreated: 1443314798 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /HexGrid/Assets/Editor/GridInspector.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: f37f06b402085124a8d3fb8441d75b59 3 | timeCreated: 1443314825 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | [Ll]ibrary/ 2 | [Tt]emp/ 3 | [Oo]bj/ 4 | [Bb]uild/ 5 | 6 | # Autogenerated VS/MD solution and project files 7 | *.csproj 8 | *.unityproj 9 | *.sln 10 | *.suo 11 | *.tmp 12 | *.user 13 | *.userprefs 14 | *.pidb 15 | *.booproj 16 | 17 | # Unity3D generated meta files 18 | *.pidb.meta 19 | 20 | # Unity3D Generated File On Crash Reports 21 | sysinfo.txt 22 | 23 | *.unity 24 | *.unity.meta -------------------------------------------------------------------------------- /HexGrid/Assets/Grid.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 06ae7873d91888d4081a67018b59eb1c 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: 7 | - hexMaterial: {instanceID: 0} 8 | - lineMaterial: {fileID: 2100000, guid: a2bb29413886117438d548505a01b202, type: 2} 9 | executionOrder: 0 10 | icon: {instanceID: 0} 11 | userData: 12 | assetBundleName: 13 | assetBundleVariant: 14 | -------------------------------------------------------------------------------- /HexGrid/Assets/Editor/GridInspector.cs: -------------------------------------------------------------------------------- 1 | using UnityEditor; 2 | using UnityEngine; 3 | using System.Collections; 4 | 5 | [CustomEditor(typeof(Grid))] 6 | public class GridInspector : Editor { 7 | 8 | public override void OnInspectorGUI() { 9 | base.OnInspectorGUI(); 10 | Grid grid = target as Grid; 11 | 12 | if(GUILayout.Button("Generate Hex Grid")) 13 | grid.GenerateGrid(); 14 | 15 | if(GUILayout.Button("Clear Hex Grid")) 16 | grid.ClearGrid(); 17 | } 18 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Joshua Hodkinson 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, 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, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # UnityHexGrid 2 | A hex grid generation tool, for use in unity. Much of the hex logic is drawn from [Red Blob Games](http://www.redblobgames.com/grids/hexagons/). I made this because I initially struggled with the implementation so now you don't have to. Feel free to leave any feedback or suggestions, I will consider implementing suggestions that I think are generally useful to all users. 3 | 4 | ## Installation 5 | Download unity package from releases 6 | 7 | In unity Assets > Import Package > Custom Package 8 | 9 | Navigate to downloaded file and select it 10 | 11 | Create an empty gameObject `ctrl+shift+n` and add `Grid` script to it 12 | 13 | ## Generating a Grid in edit mode 14 | Click Generate Grid in the inspector ensuring that relevant settings are selected 15 | 16 | ## Generating a Grid at runtime 17 | Ensure relevant settings are set and call `GenerateGrid()` on an instance of Grid 18 | 19 | ```cs 20 | public Material hexMaterial; //Assigned in inspector 21 | public Material lineMaterial; //Assigned in inspector 22 | 23 | private Grid grid; 24 | 25 | private void Start() { 26 | //Set grid settings 27 | grid.mapShape = MapShape.Rectangle; 28 | grid.mapWidth = 5; 29 | grid.mapHeight = 5; 30 | grid.hexOrientation = HexOrientation.Flat; 31 | grid.hexRadius = 1; 32 | grid.hexMaterial = hexMaterial; 33 | grid.addColliders = true; 34 | grid.drawOutlines = true; 35 | grid.lineMaterial = lineMaterial; 36 | 37 | //Gen Grid 38 | grid.GenerateGrid(); 39 | } 40 | ``` 41 | 42 | ## Access tiles at runtime 43 | 44 | Call `Tiles` on an instance of Grid, returns a `Dictionary` where the string is constructed from the tile's coordinates. 45 | 46 | ```cs 47 | private Grid grid; 48 | 49 | private void Start() { 50 | var tiles = grid.Tiles; 51 | } 52 | 53 | ``` 54 | 55 | ## Grid Settings 56 | * `mapShape` determines the overall shape of the map, available options are 57 | * Rectangle 58 | 59 | ![Rectangle](http://i.imgur.com/I5eIjlu.jpg) 60 | * Hexagon 61 | 62 | ![Hexagon](http://i.imgur.com/pvCvkuT.jpg) 63 | * Parrallelogram 64 | 65 | ![Parrallelogram](http://i.imgur.com/ZtASYn0.jpg) 66 | * Triangle 67 | 68 | ![Triangle](http://i.imgur.com/uOEkZKF.jpg) 69 | * `mapWidth` an `int`, controls the number tiles wide the map is, for hexagonal shape the larger of `mapWidth` and `mapHeight` is picked and used as a radius. 70 | * `mapHeight` an `int`, controls the number of tiles high the map is 71 | * `hexOrientation` the orientation of the individual hexes, available options are 72 | * Pointy 73 | ![Pointy](http://i.imgur.com/CGWnE1M.jpg) 74 | * Flat 75 | ![Flat](http://i.imgur.com/es0TKVS.jpg) 76 | * `hexRadius` a `float`, controls the radius of hex tile meshes, in unity units, measured from the centre of the hex to a corner, all corners are equidistant from the centre 77 | * `hexMaterial` a `Material`, applied to the hex tile meshes, if not specified defaults to unity's diffuse material 78 | * `addColliders` a `bool`, when true grid generation will add a mesh collider to the tiles using the same mesh as the tiles 79 | * `drawOutlines` a `bool`, when true grid generation will add line renderers to the tiles and draw outlines for the meshes 80 | * `lineMaterial` a `Material`, applied to the line renderers for drawing outlines, if you want outlines add the `Lines` material, included in the package, to the inspector field. Apparently I couldn't load the default Sprites/Default material via code 81 | -------------------------------------------------------------------------------- /HexGrid/Assets/Tile.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections.Generic; 3 | 4 | public class Tile : MonoBehaviour { 5 | public CubeIndex index; 6 | 7 | public static Vector3 Corner(Vector3 origin, float radius, int corner, HexOrientation orientation){ 8 | float angle = 60 * corner; 9 | if(orientation == HexOrientation.Pointy) 10 | angle += 30; 11 | angle *= Mathf.PI / 180; 12 | return new Vector3(origin.x + radius * Mathf.Cos(angle), 0.0f, origin.z + radius * Mathf.Sin(angle)); 13 | } 14 | 15 | public static void GetHexMesh(float radius, HexOrientation orientation, ref Mesh mesh) { 16 | mesh = new Mesh(); 17 | 18 | List verts = new List(); 19 | List tris = new List(); 20 | List uvs = new List(); 21 | 22 | for (int i = 0; i < 6; i++) 23 | verts.Add(Corner(Vector3.zero, radius, i, orientation)); 24 | 25 | tris.Add(0); 26 | tris.Add(2); 27 | tris.Add(1); 28 | 29 | tris.Add(0); 30 | tris.Add(5); 31 | tris.Add(2); 32 | 33 | tris.Add(2); 34 | tris.Add(5); 35 | tris.Add(3); 36 | 37 | tris.Add(3); 38 | tris.Add(5); 39 | tris.Add(4); 40 | 41 | //UVs are wrong, I need to find an equation for calucalting them 42 | uvs.Add(new Vector2(0.5f, 1f)); 43 | uvs.Add(new Vector2(1, 0.75f)); 44 | uvs.Add(new Vector2(1, 0.25f)); 45 | uvs.Add(new Vector2(0.5f, 0)); 46 | uvs.Add(new Vector2(0, 0.25f)); 47 | uvs.Add(new Vector2(0, 0.75f)); 48 | 49 | mesh.vertices = verts.ToArray(); 50 | mesh.triangles = tris.ToArray(); 51 | mesh.uv = uvs.ToArray(); 52 | 53 | mesh.name = "Hexagonal Plane"; 54 | 55 | mesh.RecalculateNormals(); 56 | } 57 | 58 | #region Coordinate Conversion Functions 59 | public static OffsetIndex CubeToEvenFlat(CubeIndex c) { 60 | OffsetIndex o; 61 | o.row = c.x; 62 | o.col = c.z + (c.x + (c.x&1)) / 2; 63 | return o; 64 | } 65 | 66 | public static CubeIndex EvenFlatToCube(OffsetIndex o){ 67 | CubeIndex c; 68 | c.x = o.col; 69 | c.z = o.row - (o.col + (o.col&1)) / 2; 70 | c.y = -c.x - c.z; 71 | return c; 72 | } 73 | 74 | public static OffsetIndex CubeToOddFlat(CubeIndex c) { 75 | OffsetIndex o; 76 | o.col = c.x; 77 | o.row = c.z + (c.x - (c.x&1)) / 2; 78 | return o; 79 | } 80 | 81 | public static CubeIndex OddFlatToCube(OffsetIndex o){ 82 | CubeIndex c; 83 | c.x = o.col; 84 | c.z = o.row - (o.col - (o.col&1)) / 2; 85 | c.y = -c.x - c.z; 86 | return c; 87 | } 88 | 89 | public static OffsetIndex CubeToEvenPointy(CubeIndex c) { 90 | OffsetIndex o; 91 | o.row = c.z; 92 | o.col = c.x + (c.z + (c.z&1)) / 2; 93 | return o; 94 | } 95 | 96 | public static CubeIndex EvenPointyToCube(OffsetIndex o){ 97 | CubeIndex c; 98 | c.x = o.col - (o.row + (o.row&1)) / 2; 99 | c.z = o.row; 100 | c.y = -c.x - c.z; 101 | return c; 102 | } 103 | 104 | public static OffsetIndex CubeToOddPointy(CubeIndex c) { 105 | OffsetIndex o; 106 | o.row = c.z; 107 | o.col = c.x + (c.z - (c.z&1)) / 2; 108 | return o; 109 | } 110 | 111 | public static CubeIndex OddPointyToCube(OffsetIndex o){ 112 | CubeIndex c; 113 | c.x = o.col - (o.row - (o.row&1)) / 2; 114 | c.z = o.row; 115 | c.y = -c.x - c.z; 116 | return c; 117 | } 118 | 119 | public static Tile operator+ (Tile one, Tile two){ 120 | Tile ret = new Tile(); 121 | ret.index = one.index + two.index; 122 | return ret; 123 | } 124 | 125 | public void LineColour(Color colour) { 126 | LineRenderer lines = GetComponent(); 127 | if(lines) 128 | { 129 | lines.startColor = colour; 130 | lines.endColor = colour; 131 | } 132 | } 133 | 134 | public void LineColour(Color start, Color end){ 135 | LineRenderer lines = GetComponent(); 136 | if(lines) 137 | { 138 | lines.startColor = start; 139 | lines.endColor = end; 140 | } 141 | } 142 | 143 | public void LineWidth(float width){ 144 | LineRenderer lines = GetComponent(); 145 | if(lines) 146 | { 147 | lines.startWidth = width; 148 | lines.endWidth = width; 149 | } 150 | } 151 | 152 | public void LineWidth(float start, float end){ 153 | LineRenderer lines = GetComponent(); 154 | if(lines) 155 | { 156 | lines.startWidth = start; 157 | lines.endWidth = end; 158 | } 159 | } 160 | #endregion 161 | 162 | #region A* Herustic Variables 163 | public int MoveCost { get; set; } 164 | public int GCost { get; set; } 165 | public int HCost { get; set; } 166 | public int FCost { get { return GCost + HCost; } } 167 | public Tile Parent { get; set; } 168 | #endregion 169 | } 170 | 171 | [System.Serializable] 172 | public struct OffsetIndex { 173 | public int row; 174 | public int col; 175 | 176 | public OffsetIndex(int row, int col){ 177 | this.row = row; this.col = col; 178 | } 179 | } 180 | 181 | [System.Serializable] 182 | public struct CubeIndex { 183 | public int x; 184 | public int y; 185 | public int z; 186 | 187 | public CubeIndex(int x, int y, int z){ 188 | this.x = x; this.y = y; this.z = z; 189 | } 190 | 191 | public CubeIndex(int x, int z) { 192 | this.x = x; this.z = z; this.y = -x-z; 193 | } 194 | 195 | public static CubeIndex operator+ (CubeIndex one, CubeIndex two){ 196 | return new CubeIndex(one.x + two.x, one.y + two.y, one.z + two.z); 197 | } 198 | 199 | public override bool Equals (object obj) { 200 | if(obj == null) 201 | return false; 202 | CubeIndex o = (CubeIndex)obj; 203 | if((System.Object)o == null) 204 | return false; 205 | return((x == o.x) && (y == o.y) && (z == o.z)); 206 | } 207 | 208 | public override int GetHashCode () { 209 | return(x.GetHashCode() ^ (y.GetHashCode() + (int)(Mathf.Pow(2, 32) / (1 + Mathf.Sqrt(5))/2) + (x.GetHashCode() << 6) + (x.GetHashCode() >> 2))); 210 | } 211 | 212 | public override string ToString () { 213 | return string.Format("[" + x + "," + y + "," + z + "]"); 214 | } 215 | } -------------------------------------------------------------------------------- /HexGrid/Assets/Grid.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections.Generic; 3 | using System; 4 | using System.Linq; 5 | 6 | public class Grid : MonoBehaviour { 7 | public static Grid inst; 8 | 9 | //Map settings 10 | public MapShape mapShape = MapShape.Rectangle; 11 | public int mapWidth; 12 | public int mapHeight; 13 | 14 | //Hex Settings 15 | public HexOrientation hexOrientation = HexOrientation.Flat; 16 | public float hexRadius = 1; 17 | public Material hexMaterial; 18 | 19 | //Generation Options 20 | public bool addColliders = true; 21 | public bool drawOutlines = true; 22 | public Material lineMaterial; 23 | 24 | //Internal variables 25 | private Dictionary grid = new Dictionary(); 26 | private Mesh hexMesh = null; 27 | private CubeIndex[] directions = 28 | new CubeIndex[] { 29 | new CubeIndex(1, -1, 0), 30 | new CubeIndex(1, 0, -1), 31 | new CubeIndex(0, 1, -1), 32 | new CubeIndex(-1, 1, 0), 33 | new CubeIndex(-1, 0, 1), 34 | new CubeIndex(0, -1, 1) 35 | }; 36 | 37 | #region Getters and Setters 38 | public Dictionary Tiles { 39 | get {return grid;} 40 | } 41 | #endregion 42 | 43 | #region Public Methods 44 | public void GenerateGrid() { 45 | //Generating a new grid, clear any remants and initialise values 46 | ClearGrid(); 47 | GetMesh(); 48 | 49 | //Generate the grid shape 50 | switch(mapShape) { 51 | case MapShape.Hexagon: 52 | GenHexShape(); 53 | break; 54 | 55 | case MapShape.Rectangle: 56 | GenRectShape(); 57 | break; 58 | 59 | case MapShape.Parrallelogram: 60 | GenParrallShape(); 61 | break; 62 | 63 | case MapShape.Triangle: 64 | GenTriShape(); 65 | break; 66 | 67 | default: 68 | break; 69 | } 70 | } 71 | 72 | public void ClearGrid() { 73 | Debug.Log ("Clearing grid..."); 74 | foreach(var tile in grid) 75 | DestroyImmediate(tile.Value.gameObject, false); 76 | 77 | grid.Clear(); 78 | } 79 | 80 | public Tile TileAt(CubeIndex index){ 81 | if(grid.ContainsKey(index.ToString())) 82 | return grid[index.ToString()]; 83 | return null; 84 | } 85 | 86 | public Tile TileAt(int x, int y, int z){ 87 | return TileAt(new CubeIndex(x,y,z)); 88 | } 89 | 90 | public Tile TileAt(int x, int z){ 91 | return TileAt(new CubeIndex(x,z)); 92 | } 93 | 94 | public List Neighbours(Tile tile) { 95 | List ret = new List(); 96 | 97 | if (tile == null) 98 | return ret; 99 | 100 | CubeIndex o; 101 | 102 | for(int i = 0; i < 6; i++) { 103 | o = tile.index + directions[i]; 104 | if(grid.ContainsKey(o.ToString())) 105 | ret.Add(grid[o.ToString()]); 106 | } 107 | return ret; 108 | } 109 | 110 | public List Neighbours(CubeIndex index){ 111 | return Neighbours(TileAt(index)); 112 | } 113 | 114 | public List Neighbours(int x, int y, int z){ 115 | return Neighbours(TileAt(x,y,z)); 116 | } 117 | 118 | public List Neighbours(int x, int z){ 119 | return Neighbours(TileAt(x,z)); 120 | } 121 | 122 | public List TilesInRange(Tile center, int range){ 123 | //Return tiles range steps from center, http://www.redblobgames.com/grids/hexagons/#range 124 | List ret = new List(); 125 | CubeIndex o; 126 | 127 | for(int dx = -range; dx <= range; dx++){ 128 | for(int dy = Mathf.Max(-range, -dx-range); dy <= Mathf.Min(range, -dx+range); dy++){ 129 | o = new CubeIndex(dx, dy, -dx-dy) + center.index; 130 | if(grid.ContainsKey(o.ToString())) 131 | ret.Add(grid[o.ToString()]); 132 | } 133 | } 134 | return ret; 135 | } 136 | 137 | public List TilesInRange(CubeIndex index, int range){ 138 | return TilesInRange(TileAt(index), range); 139 | } 140 | 141 | public List TilesInRange(int x, int y, int z, int range){ 142 | return TilesInRange(TileAt(x,y,z), range); 143 | } 144 | 145 | public List TilesInRange(int x, int z, int range){ 146 | return TilesInRange(TileAt(x,z), range); 147 | } 148 | 149 | public int Distance(CubeIndex a, CubeIndex b){ 150 | return Mathf.Abs(a.x - b.x) + Mathf.Abs(a.y - b.y) + Mathf.Abs(a.z - b.z); 151 | } 152 | 153 | public int Distance(Tile a, Tile b){ 154 | return Distance(a.index, b.index); 155 | } 156 | #endregion 157 | 158 | #region Private Methods 159 | private void Awake() { 160 | if(!inst) 161 | inst = this; 162 | 163 | GenerateGrid(); 164 | } 165 | 166 | private void GetMesh() { 167 | hexMesh = null; 168 | Tile.GetHexMesh(hexRadius, hexOrientation, ref hexMesh); 169 | } 170 | 171 | private void GenHexShape() { 172 | Debug.Log ("Generating hexagonal shaped grid..."); 173 | 174 | Tile tile; 175 | Vector3 pos = Vector3.zero; 176 | 177 | int mapSize = Mathf.Max(mapWidth, mapHeight); 178 | 179 | for (int q = -mapSize; q <= mapSize; q++){ 180 | int r1 = Mathf.Max(-mapSize, -q-mapSize); 181 | int r2 = Mathf.Min(mapSize, -q+mapSize); 182 | for(int r = r1; r <= r2; r++){ 183 | switch(hexOrientation){ 184 | case HexOrientation.Flat: 185 | pos.x = hexRadius * 3.0f/2.0f * q; 186 | pos.z = hexRadius * Mathf.Sqrt(3.0f) * (r + q/2.0f); 187 | break; 188 | 189 | case HexOrientation.Pointy: 190 | pos.x = hexRadius * Mathf.Sqrt(3.0f) * (q + r/2.0f); 191 | pos.z = hexRadius * 3.0f/2.0f * r; 192 | break; 193 | } 194 | 195 | tile = CreateHexGO(pos,("Hex[" + q + "," + r + "," + (-q-r).ToString() + "]")); 196 | tile.index = new CubeIndex(q,r,-q-r); 197 | grid.Add(tile.index.ToString(), tile); 198 | } 199 | } 200 | } 201 | 202 | private void GenRectShape() { 203 | Debug.Log ("Generating rectangular shaped grid..."); 204 | 205 | Tile tile; 206 | Vector3 pos = Vector3.zero; 207 | 208 | switch(hexOrientation){ 209 | case HexOrientation.Flat: 210 | for(int q = 0; q < mapWidth; q++){ 211 | int qOff = q>>1; 212 | for (int r = -qOff; r < mapHeight - qOff; r++){ 213 | pos.x = hexRadius * 3.0f/2.0f * q; 214 | pos.z = hexRadius * Mathf.Sqrt(3.0f) * (r + q/2.0f); 215 | 216 | tile = CreateHexGO(pos,("Hex[" + q + "," + r + "," + (-q-r).ToString() + "]")); 217 | tile.index = new CubeIndex(q,r,-q-r); 218 | grid.Add(tile.index.ToString(), tile); 219 | } 220 | } 221 | break; 222 | 223 | case HexOrientation.Pointy: 224 | for(int r = 0; r < mapHeight; r++){ 225 | int rOff = r>>1; 226 | for (int q = -rOff; q < mapWidth - rOff; q++){ 227 | pos.x = hexRadius * Mathf.Sqrt(3.0f) * (q + r/2.0f); 228 | pos.z = hexRadius * 3.0f/2.0f * r; 229 | 230 | tile = CreateHexGO(pos,("Hex[" + q + "," + r + "," + (-q-r).ToString() + "]")); 231 | tile.index = new CubeIndex(q,r,-q-r); 232 | grid.Add(tile.index.ToString(), tile); 233 | } 234 | } 235 | break; 236 | } 237 | } 238 | 239 | private void GenParrallShape() { 240 | Debug.Log ("Generating parrellelogram shaped grid..."); 241 | 242 | Tile tile; 243 | Vector3 pos = Vector3.zero; 244 | 245 | for (int q = 0; q <= mapWidth; q++){ 246 | for(int r = 0; r <= mapHeight; r++){ 247 | switch(hexOrientation){ 248 | case HexOrientation.Flat: 249 | pos.x = hexRadius * 3.0f/2.0f * q; 250 | pos.z = hexRadius * Mathf.Sqrt(3.0f) * (r + q/2.0f); 251 | break; 252 | 253 | case HexOrientation.Pointy: 254 | pos.x = hexRadius * Mathf.Sqrt(3.0f) * (q + r/2.0f); 255 | pos.z = hexRadius * 3.0f/2.0f * r; 256 | break; 257 | } 258 | 259 | tile = CreateHexGO(pos,("Hex[" + q + "," + r + "," + (-q-r).ToString() + "]")); 260 | tile.index = new CubeIndex(q,r,-q-r); 261 | grid.Add(tile.index.ToString(), tile); 262 | } 263 | } 264 | } 265 | 266 | private void GenTriShape() { 267 | Debug.Log ("Generating triangular shaped grid..."); 268 | 269 | Tile tile; 270 | Vector3 pos = Vector3.zero; 271 | 272 | int mapSize = Mathf.Max(mapWidth, mapHeight); 273 | 274 | for (int q = 0; q <= mapSize; q++){ 275 | for(int r = 0; r <= mapSize - q; r++){ 276 | switch(hexOrientation){ 277 | case HexOrientation.Flat: 278 | pos.x = hexRadius * 3.0f/2.0f * q; 279 | pos.z = hexRadius * Mathf.Sqrt(3.0f) * (r + q/2.0f); 280 | break; 281 | 282 | case HexOrientation.Pointy: 283 | pos.x = hexRadius * Mathf.Sqrt(3.0f) * (q + r/2.0f); 284 | pos.z = hexRadius * 3.0f/2.0f * r; 285 | break; 286 | } 287 | 288 | tile = CreateHexGO(pos,("Hex[" + q + "," + r + "," + (-q-r).ToString() + "]")); 289 | tile.index = new CubeIndex(q,r,-q-r); 290 | grid.Add(tile.index.ToString(), tile); 291 | } 292 | } 293 | } 294 | 295 | private Tile CreateHexGO(Vector3 postion, string name) { 296 | GameObject go = new GameObject(name, typeof(MeshFilter), typeof(MeshRenderer), typeof(Tile)); 297 | 298 | if(addColliders) 299 | go.AddComponent(); 300 | 301 | if(drawOutlines) 302 | go.AddComponent(); 303 | 304 | go.transform.position = postion; 305 | go.transform.parent = this.transform; 306 | 307 | Tile tile = go.GetComponent(); 308 | MeshFilter fil = go.GetComponent(); 309 | MeshRenderer ren = go.GetComponent(); 310 | 311 | fil.sharedMesh = hexMesh; 312 | 313 | ren.material = (hexMaterial)? hexMaterial : UnityEditor.AssetDatabase.GetBuiltinExtraResource("Default-Diffuse.mat"); 314 | 315 | if(addColliders){ 316 | MeshCollider col = go.GetComponent(); 317 | col.sharedMesh = hexMesh; 318 | } 319 | 320 | if(drawOutlines) { 321 | LineRenderer lines = go.GetComponent(); 322 | lines.lightProbeUsage = UnityEngine.Rendering.LightProbeUsage.Off; 323 | lines.receiveShadows = false; 324 | 325 | lines.startWidth = 0.1f; 326 | lines.endWidth = 0.1f; 327 | lines.startColor = Color.black; 328 | lines.endColor = Color.black; 329 | lines.material = lineMaterial; 330 | 331 | lines.positionCount = 7; 332 | 333 | for(int vert = 0; vert <= 6; vert++) 334 | lines.SetPosition(vert, Tile.Corner(tile.transform.position, hexRadius, vert, hexOrientation)); 335 | } 336 | 337 | return tile; 338 | } 339 | #endregion 340 | } 341 | 342 | [System.Serializable] 343 | public enum MapShape { 344 | Rectangle, 345 | Hexagon, 346 | Parrallelogram, 347 | Triangle 348 | } 349 | 350 | [System.Serializable] 351 | public enum HexOrientation { 352 | Pointy, 353 | Flat 354 | } --------------------------------------------------------------------------------