Drag the prefab “Grids2D” from “Resources/Prefabs” folder to your scene.
Select the GameObject created to show custom properties and customize it to your preferences.
Custom Editor Properties
This section allows you to configure the grid irrespective of its appearance or usage. Basically, these properties define the number of cells, territories, and their shapes.
Topology: this parameter defines the grid overall shape. You can choose between irregular (which uses a highly optimized Voronoi tessellation algorithm), boxed or hexagonal type.
Territories and Cells: each grid has a number of cells and OPTIONALLY a number of territories. A territory contains one or more cells, so you will always have more cells than territories (or same number).
Note that adding territories will make the system slower with a high number of cells. To prevent lag during runtime while creating the neccesary meshes you can pre-warm the geometry using the API. More on that later.
Columns and Rows: specify the number of cellsin the grid. These two parameters are not available in the irregular topology and instead a single number of cells is shown.
- Curvature: if your grid has 100 or less cells, you can apply an optional curvature. Note that using this parameter will multiply by 3 the number of segments of the grid.
- Relaxation: this option will make irregular shapes (Voronoi) more homogeneous. Basically, it iterates over the voronoi calculation algorithm weighing and modulating the separation between cells centers. Each iteration will redo the Voronoi calculation and even it’s been optimized that means that mesh generation times will go up with each relaxation level.
- Visibility Mask: allows you to assign a texture whose alpha channel determines if the cell at that position is visible or not (alpha = 0 means cell won’t be visible).
- Seed: this parameter will allow you to recreate the grid with same disposition of cells. This option basically affects the random functionality.
- Territories: enables territory generation. A territory is a group of cells. You can specify the number of territories and optionally, a region texture.
- Region Texture: assign a color texture to define territories shape or cell ownership. One territory for each different color will be created. Ensure you use solid colors (no smooth or gradients) in the texture. If you use Gimp, you can switch to Indexed Color and set a maximum number of colors, then switch back to RGBA and export the texture in PNG format. Optionally specify which color will be used as neutral, meaning those cells won’t belong to any territory.
When using a territories texture, check the Import settings of the texture:
Texture type: Advanced
Filter: disabled (disable any bilinear or other kind of filtering)
Compression: Automatic Truecolor
Max size: the max width or height of your texture.
- Hide Objects: let you quickly hide the grid.
- Depth Offset: this parameter control how much offset is applied to the shaders used by Grids 2D System. This value affects the Z-Buffer position of the pixels of both highlight surfaces and grid mesh.
- Camera: the camera to be used to compute certain aspects like interaction. The asset will pick the main camera by default.
This section controls the look and feel of the grid system and it should be self-explanatory. However, it’s important to know that activating either “Show Territories” or “Colorize Territories” (or set the highlight mode to Territory as well) will enable the territory mesh generation. So, if you don’t use the territory functionality, make sure all options regarding territories are unchecked to save time and memory.
If you want to make the grid background transparent, you need to change the material of Grids2D to “CanvasBackgroundClear” (material is found inside Grids2D/Resources/Materials folder) and set your camera’s Clear Flags setting to Solid Color.
- Show Territories: displays or hides the territories borders.
- Highlight Color: the highlight color for territories when the pointer is over any of them.
- Colorize Territories: if you activate this option, the asset will automatically color territories with random colors.
- Alpha: transparency of the territories.
- Outer Borders: shows the borders of territories which are not adjacent to other territories (like grid borders).
- Internal Territories: allows territories to be contained inside other territories. This option will enable highlighting “enclaves” or minor territories that are surrounded by others.
- Canvas Texture: if you assign a texture to this option, when you texture a territory or cell using the same texture, the asset will render the part of the texture that’s contained inside the territory or cell as if the texture were filling the complete grid instead of only the area of the territory or cell. Check Demo4_Textures demo scene for an example of this feature.
- Selection Mode: choose between None, Cells or Territories.
- Overlay Mode: choose between Ground or Overlay modes. The overlay mode will make the highlight surfaces to be displayed on top of everything. The default option, Ground, will make the highlight surfaces to appear below any content put over the grid.
- Respect Other UI: when enabled, interaction with grid will be disabled when pointer is over an UI element. Note that only uGUI elements are supported (those under a Canvas root element, no immeditate GUI created in OnGUI).
This section controls the path finding behaviour.
- Algorithm: choose among available algorithms:
- MaxDXDY: use estimations based on horizontal/vertical distance to target. Produces more natural paths althouth not optimal.
- Manhattan: produces jaggy paths.
- Diagonal Shortcut: favours diagonals in path.
- Euclidean: estimation based on the straight distance to target. Produces the optimal path.
- Euclidean Non SQR: similar but don’t uses sqrt which makes it faster.
- Custom1: experimental method.
- Max Search Cost: the maximum cost for the returned path.
- Max Steps: the maximum number of steps for any path.
- Use Diagonals: if diagonals between cells can be used.
- Heavy DIagonals: add an extra cost for diagonals, making them less frequent.
The Path Finding algorithm is very fast but the asset also includes an optimized variant of the algorithm for grids that have column count power of 2 (4, 8, 16, 32, 64, 256, …).
Check demo scene 12 for example on using Path Finding with Terrain Grid System. Also read the available APIs for using path finding feature below.
Grids2D provides you with 4 options to customize paths:
- CellSetCanCross: calling this method you can simply specify if a cell blocks any path or not.
- CellSetGroup: use this method to assign a cell to a custom group. You can later specify which groups are allowed when computing a path calling FindPath method.
- CellSetCrossCost: defines a custom crossing cost when entering a cell.
- CellSetSideCrossCost: defines a custom crossing cost for a givel cell and a specific edge with option to consider moving direction (ie. entering or exiting the cell through that edge).
By default, all cells belong to the Universal group (which has a code number of -1). You can assign a different group to any number of cells using CellSetGroup (or wonder which group a cell belongs to calling CellGetGroup).
The cell group can be used as a parameter when calling to FindPath function in path-finding or CellGetLineOfSight. Groups are useful to ignore certain type of cells which might be occupied by some characters that should not be considered either in the route or in the line of sight.
This section allows you to customize the cells in Editor time.
With the Scene View selected (not Game View), you can use the mouse to select any cell and change their properties.
Export Settings will create add a “Grid2DConfig” script which serves as a container for all the settings and will load them when activated.
Reset will clear all cell settings and reverts them to default values.
Textures array is a convenient way to load several textures and use them quickly to paint several cells. Just click the “T” small buton to enable the “paint mode”. Once enabled, you can click several cells to assign the chosen texture.
You can have several “Grid2DConfig” scripts added. To load a specific configuration just enable the desired Grid2DConfig component or call “LoadConfiguration()” method of that script.