├── .gitignore ├── LICENSE.txt ├── README.md └── Scripts ├── Grid.cs ├── GridDimension.cs ├── GridEntry.cs ├── IGrid.cs └── Point.cs /.gitignore: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # This .gitignore file was automatically created by Microsoft(R) Visual Studio. 3 | ################################################################################ 4 | 5 | *.meta 6 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | The MIT License 2 | 3 | Copyright (c) 2010-2017 Google, Inc. http://angularjs.org 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 13 | all 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 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Unity3D Grid System 2 | This is a simple grid system that allows you to uniformly place GameObjects in a 3 dimensional grid. 3 | ## How to use 4 | * Attach the `Grid` script to a GameObject, this will serve as the parent for your grid system. 5 | * Take the object you wish to place on the grid, and a `Point` object, using `SetObject(...)` of the `Grid` component to assign it. 6 | 7 | The following example assumes that `gridGameObject` is the parent GameObject with the attached `Grid` component, and that `myObject` is the GameObject you wish to attach to the grid at the specified point (0, 0, 0) 8 | ``` 9 | Grid grid = gridGameObject.GetComponent(); 10 | grid.SetObject(new Point(0,0,0), myObject); 11 | ``` 12 | ## Problems 13 | * Default value for a `Grid.Dimension.CellDistance` is 0, causing a divide by 0 error, if doing a 1 or 2 dimensional grid, set the appropriate distance to 1. 14 | * There's no editor scripts for adjusting the grid dimensions in a nice editor-y way, and no way to attach objects to a grid non-dynamically. -------------------------------------------------------------------------------- /Scripts/Grid.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using UnityEngine; 3 | 4 | namespace foxfluff.GridSystem 5 | { 6 | /// 7 | /// Grid will create a uniform distribution of GameObjects that allow easy use of attaching 8 | /// other objects, making things like tile based placement much easier 9 | /// 10 | public class Grid : MonoBehaviour, IGrid 11 | { 12 | [SerializeField] private GridDimension length; //x 13 | [SerializeField] private GridDimension width; //y 14 | [SerializeField] private GridDimension height; //z 15 | 16 | public GridDimension Length { get { return length; } } 17 | public GridDimension Width { get { return width; } } 18 | public GridDimension Height { get { return height; } } 19 | 20 | private GridEntry[,,] grid; // should be part of the interface 21 | 22 | public GameObject GetObject(Point coordinate) 23 | { 24 | return grid[coordinate.X, coordinate.Y, coordinate.Z].Entry; 25 | } 26 | 27 | /// 28 | /// Removes the GameObject on the grid. 29 | /// 30 | /// The location of the object that needs to be removed. 31 | public void RemoveObject(Point coordinate) 32 | { 33 | #warning This doesn't destroy the wrapper object. 34 | Destroy(grid[coordinate.X, coordinate.Y, coordinate.Z].Entry); 35 | grid[coordinate.X, coordinate.Y, coordinate.Z] = null; 36 | } 37 | 38 | /// 39 | /// Assign an object to a coordinate point on the grid. 40 | /// 41 | /// The location on the grid where the object should be placed. 42 | /// The GameObject being added to the grid. 43 | public void SetObject(Point coordinate, GameObject gObject) 44 | { 45 | #warning Need to change transform on new objects to be placed properly 46 | GridEntry entry = new GridEntry(gObject); 47 | entry.Wrapper.name = string.Format("({0},{1},{2})", coordinate.X, coordinate.Y, coordinate.Z); 48 | entry.Wrapper.transform.SetParent(transform); 49 | entry.Wrapper.transform.localPosition = calcPosition(coordinate); 50 | 51 | grid[coordinate.X, coordinate.Y, coordinate.Z] = entry; 52 | } 53 | 54 | void Awake() 55 | { 56 | grid = new GridEntry[length.CellCount, width.CellCount, height.CellCount]; 57 | } 58 | 59 | /// 60 | /// Calculate the localPosition for a given Point 61 | /// 62 | /// The location to calculate the position for. 63 | /// 64 | private Vector3 calcPosition(Point coordinate) 65 | { 66 | return new Vector3( 67 | coordinate.X * (length.CellDistance / length.CellCount), 68 | coordinate.Y * (width.CellDistance / width.CellCount), 69 | coordinate.Z * (height.CellDistance / height.CellCount) 70 | ); 71 | } 72 | } 73 | } -------------------------------------------------------------------------------- /Scripts/GridDimension.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using UnityEngine; 3 | 4 | namespace foxfluff.GridSystem 5 | { 6 | [Serializable] 7 | public struct GridDimension 8 | { 9 | [SerializeField] int cellCount; 10 | [SerializeField] float cellDistance; 11 | 12 | public int CellCount { get { return cellCount; } } 13 | public float CellDistance { get { return cellDistance; } } 14 | } 15 | } -------------------------------------------------------------------------------- /Scripts/GridEntry.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | 3 | namespace foxfluff.GridSystem 4 | { 5 | /// 6 | /// GridEntry is a representation of objects attached to a grid, it will 7 | /// track if it's active or not, and what gameObject is attached. 8 | /// 9 | public class GridEntry 10 | { 11 | public GameObject Entry { get; private set; } 12 | public Vector3 Offset { get; private set; } 13 | public bool GetActive { get; private set; } 14 | public GameObject Wrapper { get; private set; } 15 | 16 | /// 17 | /// Created a new entry for an object in a Grid 18 | /// 19 | /// The GameObject being attached to a point in the grid. 20 | public GridEntry(GameObject entry = null) 21 | { 22 | Entry = entry; 23 | Wrapper = new GameObject(); 24 | if (Entry) 25 | { 26 | SetActive(true); 27 | Entry.transform.SetParent(Wrapper.transform); 28 | } 29 | } 30 | 31 | /// 32 | /// Sets the active state of the entry. 33 | /// 34 | /// The active state of the object. 35 | public void SetActive(bool active) 36 | { 37 | GetActive = active; 38 | Entry.SetActive(active); 39 | } 40 | 41 | /// 42 | /// Updates the transform offset of the object 43 | /// 44 | /// The new offset for the object relative to the coordinates position 45 | public void UpdateOffset(Vector3 position) 46 | { 47 | // must first calculate the relative move since there won't be any reference to the parent grid 48 | 49 | } 50 | } 51 | } -------------------------------------------------------------------------------- /Scripts/IGrid.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | 3 | namespace foxfluff.GridSystem 4 | { 5 | public interface IGrid 6 | { 7 | GameObject GetObject(Point coordinate); 8 | void SetObject(Point coordinate, GameObject gObject); 9 | void RemoveObject(Point coordinate); 10 | } 11 | } -------------------------------------------------------------------------------- /Scripts/Point.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | 4 | namespace foxfluff.GridSystem 5 | { 6 | public struct Point 7 | { 8 | public int X { get; private set; } 9 | public int Y { get; private set; } 10 | public int Z { get; private set; } 11 | 12 | public Point(int X, int Y) 13 | { 14 | this.X = X; 15 | this.Y = Y; 16 | this.Z = 0; 17 | } 18 | 19 | public Point(int X, int Y, int Z) 20 | { 21 | this.X = X; 22 | this.Y = Y; 23 | this.Z = Z; 24 | } 25 | } 26 | } --------------------------------------------------------------------------------