├── GridGenerator.cs ├── GridTile.cs ├── README.md └── example.jpg /GridGenerator.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | 5 | public class GridGenerator : MonoBehaviour 6 | { 7 | private GridTile[,] grid; 8 | 9 | private List gridTiles; 10 | private List xValues; 11 | private List yValues; 12 | 13 | public GridTile[,] Grid { 14 | get { 15 | return grid; 16 | } 17 | } 18 | 19 | void Start () 20 | { 21 | gridTiles = new List (); 22 | xValues = new List (); 23 | yValues = new List (); 24 | foreach (Transform child in transform) { 25 | bool wall = false; 26 | if (child.tag == "Wall") { 27 | wall = true; 28 | } 29 | gridTiles.Add (new GridTile (child.position.x, child.position.y, wall)); 30 | if (!xValues.Contains (child.position.x)) { 31 | xValues.Add (child.position.x); 32 | } 33 | if (!yValues.Contains (child.position.y)) { 34 | yValues.Add (child.position.y); 35 | } 36 | } 37 | xValues.Sort (); 38 | yValues.Sort (); 39 | yValues.Reverse (); 40 | float[] xValuesSorted = xValues.ToArray (); 41 | float[] yValuesSorted = yValues.ToArray (); 42 | grid = new GridTile[xValues.Count, yValues.Count]; 43 | string result = "Grid:\n"; 44 | for (int y = 0; y < yValuesSorted.Length; y++) { 45 | for (int x = 0; x < xValuesSorted.Length; x++) { 46 | foreach (GridTile tile in gridTiles) { 47 | if (tile.x == xValuesSorted [x] && tile.y == yValuesSorted [y]) { 48 | grid [x, y] = tile; 49 | if (tile.wall) { 50 | result += "1 "; 51 | } else { 52 | result += "0 "; 53 | } 54 | } 55 | } 56 | } 57 | result += "\n"; 58 | } 59 | Debug.Log (result); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /GridTile.cs: -------------------------------------------------------------------------------- 1 | public class GridTile 2 | { 3 | 4 | public float x; 5 | public float y; 6 | public bool wall; 7 | 8 | public GridTile (float x, float y, bool wall) 9 | { 10 | this.x = x; 11 | this.y = y; 12 | this.wall = wall; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Unity Grid Generator 2 | ==================== 3 | 4 | A simple Unity script, that computes a grid for tile-based 2D games. 5 | 6 | ## Usage 7 | 8 | Align all your tiles in a grid and assign them as childs to an empty game object, which is representing the tile-map. 9 | 10 | ![Example](example.jpg?raw=true "Example") 11 | 12 | Mark every wall or obstacle with the tag "Wall". 13 | 14 | Add GridGenerator.cs to your tile-map object. 15 | 16 | Start the game. You should see a message like this in the console: 17 | 18 | ``` 19 | Grid: 20 | 1 1 1 1 1 1 1 1 1 1 1 1 21 | 1 1 1 1 1 1 1 1 1 1 1 1 22 | 1 0 0 0 0 0 0 0 0 0 0 1 23 | 1 0 0 0 0 0 0 1 0 0 0 1 24 | 1 0 0 1 0 0 1 1 1 1 1 1 25 | 1 0 1 1 1 0 1 1 1 1 1 1 26 | 1 0 1 1 1 0 1 0 0 0 0 1 27 | 1 0 0 1 0 0 1 0 0 1 0 1 28 | 1 0 0 1 0 0 0 0 0 1 0 1 29 | 1 0 0 1 0 0 0 0 0 0 0 1 30 | 1 1 1 1 1 1 1 1 1 1 1 1 31 | ``` 32 | 33 | GridGenerator.cs has a getter for the Grid: 34 | 35 | ```csharp 36 | public GridTile[,] Grid { 37 | get { 38 | return grid; 39 | } 40 | } 41 | ``` 42 | 43 | That's it! Have fun! :-) 44 | -------------------------------------------------------------------------------- /example.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fgladisch/unity-grid-generator/dc8af9f915bb42249ec6b14a4d8605267a712345/example.jpg --------------------------------------------------------------------------------